Improve Argeo Init shutdown sequence.
[lgpl/argeo-commons.git] / org.argeo.init / src / org / argeo / init / osgi / OsgiRuntimeContext.java
index 7f44f6b2746dccc914312180b493d56e7328ce3c..373e3d6719115030884a2ed770b7dbf6667724d9 100644 (file)
@@ -11,6 +11,7 @@ import java.util.function.Consumer;
 
 import org.argeo.init.RuntimeContext;
 import org.argeo.init.logging.ThinLoggerFinder;
+import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleException;
 import org.osgi.framework.Constants;
@@ -73,25 +74,43 @@ public class OsgiRuntimeContext implements RuntimeContext {
        }
 
        public void stop(BundleContext bundleContext) {
-               if (loggingConfigurationSr != null)
-                       loggingConfigurationSr.unregister();
-               if (logEntryPublisherSr != null)
-                       logEntryPublisherSr.unregister();
-
+//             if (loggingConfigurationSr != null)
+//                     try {
+//                             loggingConfigurationSr.unregister();
+//                     } catch (Exception e) {
+//                             // silent
+//                     }
+//             if (logEntryPublisherSr != null)
+//                     try {
+//                             logEntryPublisherSr.unregister();
+//                     } catch (Exception e) {
+//                             // silent
+//                     }
        }
 
        @Override
        public void waitForStop(long timeout) throws InterruptedException {
                if (framework == null)
                        throw new IllegalStateException("Framework is not initialised");
-               stop(framework.getBundleContext());
+
                framework.waitForStop(timeout);
        }
 
-       @Override
        public void close() throws Exception {
+               // TODO make shutdown of dynamic service more robust
+               Bundle scrBundle = osgiBoot.getBundlesBySymbolicName().get("org.apache.felix.scr");
+               if (scrBundle != null) {
+                       scrBundle.stop();
+                       while (!(scrBundle.getState() <= Bundle.RESOLVED)) {
+                               Thread.sleep(500);
+                       }
+                       Thread.sleep(1000);
+               }
+
+               stop(framework.getBundleContext());
                if (framework != null)
                        framework.stop();
+
        }
 
 }