import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashSet;
public void bundleChanged(BundleEvent event) {
// If the system bundle (id 0) is stopping, do an orderly shutdown of all blueprint containers. On
// shutdown the system bundle is stopped first.
public void bundleChanged(BundleEvent event) {
// If the system bundle (id 0) is stopping, do an orderly shutdown of all blueprint containers. On
// shutdown the system bundle is stopped first.
LOG.info("Creating blueprint container for bundle {} with paths {}", bundle, paths);
blueprintExtenderService.createContainer(bundle, paths);
LOG.info("Creating blueprint container for bundle {} with paths {}", bundle, paths);
blueprintExtenderService.createContainer(bundle, paths);
LOG.info("Blueprint container for bundle {} was successfully created",
event.getProperty(EventConstants.BUNDLE));
LOG.info("Blueprint container for bundle {} was successfully created",
event.getProperty(EventConstants.BUNDLE));
// If the container timed out waiting for dependencies, we'll destroy it and start it again. This
// is indicated via a non-null DEPENDENCIES property containing the missing dependencies. The
// default timeout is 5 min and ideally we would set this to infinite but the timeout can only
// be set at the bundle level in the manifest - there's no way to set it globally.
// If the container timed out waiting for dependencies, we'll destroy it and start it again. This
// is indicated via a non-null DEPENDENCIES property containing the missing dependencies. The
// default timeout is 5 min and ideally we would set this to infinite but the timeout can only
// be set at the bundle level in the manifest - there's no way to set it globally.
Bundle bundle = (Bundle) event.getProperty(EventConstants.BUNDLE);
List<Object> paths = findBlueprintPaths(bundle);
Bundle bundle = (Bundle) event.getProperty(EventConstants.BUNDLE);
List<Object> paths = findBlueprintPaths(bundle);
LOG.warn("Blueprint container for bundle {} timed out waiting for dependencies - restarting it",
event.getProperty(EventConstants.BUNDLE));
LOG.warn("Blueprint container for bundle {} timed out waiting for dependencies - restarting it",
event.getProperty(EventConstants.BUNDLE));
@SuppressWarnings({ "rawtypes", "unchecked" })
static List<Object> findBlueprintPaths(Bundle bundle) {
@SuppressWarnings({ "rawtypes", "unchecked" })
static List<Object> findBlueprintPaths(Bundle bundle) {
// Close all CSS modules first.
ConfigSystemService configSystem = getOSGiService(ConfigSystemService.class);
// Close all CSS modules first.
ConfigSystemService configSystem = getOSGiService(ConfigSystemService.class);
configSystem.closeAllConfigModules();
}
LOG.info("Shutting down all blueprint containers...");
Collection<Bundle> containerBundles = new HashSet<>(Arrays.asList(bundleContext.getBundles()));
configSystem.closeAllConfigModules();
}
LOG.info("Shutting down all blueprint containers...");
Collection<Bundle> containerBundles = new HashSet<>(Arrays.asList(bundleContext.getBundles()));
// For each iteration of getBundlesToDestroy, as containers are destroyed, other containers become
// eligible to be destroyed. We loop until we've destroyed them all.
// For each iteration of getBundlesToDestroy, as containers are destroyed, other containers become
// eligible to be destroyed. We loop until we've destroyed them all.
containerBundles.remove(bundle);
BlueprintContainer container = blueprintExtenderService.getContainer(bundle);
containerBundles.remove(bundle);
BlueprintContainer container = blueprintExtenderService.getContainer(bundle);
usage += getServiceUsage(reference);
}
}
LOG.debug("Usage for bundle {} is {}", bundle, usage);
usage += getServiceUsage(reference);
}
}
LOG.debug("Usage for bundle {} is {}", bundle, usage);
- if(!bundlesToDestroy.isEmpty()) {
- Collections.sort(bundlesToDestroy, new Comparator<Bundle>() {
- @Override
- public int compare(Bundle b1, Bundle b2) {
- return (int) (b2.getLastModified() - b1.getLastModified());
- }
- });
+ if (!bundlesToDestroy.isEmpty()) {
+ Collections.sort(bundlesToDestroy, (b1, b2) -> (int) (b2.getLastModified() - b1.getLastModified()));
continue;
}
// Choose 'reference' if it has a lower service ranking or, if the rankings are equal
// which is usually the case, if it has a higher service ID. For the latter the < 0
// check looks backwards but that's how ServiceReference#compareTo is documented to work.
continue;
}
// Choose 'reference' if it has a lower service ranking or, if the rankings are equal
// which is usually the case, if it has a higher service ID. For the latter the < 0
// check looks backwards but that's how ServiceReference#compareTo is documented to work.
private <T> T getOSGiService(Class<T> serviceInterface) {
try {
ServiceReference<T> serviceReference = bundleContext.getServiceReference(serviceInterface);
private <T> T getOSGiService(Class<T> serviceInterface) {
try {
ServiceReference<T> serviceReference = bundleContext.getServiceReference(serviceInterface);
LOG.warn("{} service reference not found", serviceInterface.getSimpleName());
return null;
}
T service = bundleContext.getService(serviceReference);
LOG.warn("{} service reference not found", serviceInterface.getSimpleName());
return null;
}
T service = bundleContext.getService(serviceReference);
// This could happen on shutdown if the service was already unregistered so we log as debug.
LOG.debug("{} service instance was not found", serviceInterface.getSimpleName());
}
return service;
// This could happen on shutdown if the service was already unregistered so we log as debug.
LOG.debug("{} service instance was not found", serviceInterface.getSimpleName());
}
return service;
// This is thrown if the BundleContext is no longer valid which is possible on shutdown so we
// log as debug.
LOG.debug("Error obtaining OSGi service {}", serviceInterface.getSimpleName(), e);
// This is thrown if the BundleContext is no longer valid which is possible on shutdown so we
// log as debug.
LOG.debug("Error obtaining OSGi service {}", serviceInterface.getSimpleName(), e);