+ synchronized (serviceRecipes) {
+ stoppedServiceRecipes = true;
+ for (StaticServiceReferenceRecipe recipe: serviceRecipes) {
+ recipe.stop();
+ }
+
+ serviceRecipes.clear();
+ }
+ }
+
+ protected void restartContainer() {
+ if (restarting.compareAndSet(false, true)) {
+ BlueprintContainerRestartService restartService = getOSGiService(BlueprintContainerRestartService.class);
+ if (restartService != null) {
+ log.debug("{}: Restarting container", logName());
+ restartService.restartContainerAndDependents(container().getBundleContext().getBundle());
+ }
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ @Nullable
+ protected <T> T getOSGiService(final Class<T> serviceInterface) {
+ try {
+ ServiceReference<T> serviceReference =
+ container().getBundleContext().getServiceReference(serviceInterface);
+ if (serviceReference == null) {
+ log.warn("{}: {} reference not found", logName(), serviceInterface.getSimpleName());
+ return null;
+ }
+
+ T service = (T)container().getService(serviceReference);
+ if (service == null) {
+ // This could happen on shutdown if the service was already unregistered so we log as debug.
+ log.debug("{}: {} was not found", logName(), serviceInterface.getSimpleName());
+ }
+
+ return service;
+ } catch (final IllegalStateException 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 {}", logName(), serviceInterface.getSimpleName(), e);