]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.init/src/org/argeo/init/osgi/OsgiRuntimeContext.java
Make sure LoggerFinder has been lazily activated.
[lgpl/argeo-commons.git] / org.argeo.init / src / org / argeo / init / osgi / OsgiRuntimeContext.java
index 7f44f6b2746dccc914312180b493d56e7328ce3c..9bb59ef1dc5c87744291fe3213f4c181ced12b51 100644 (file)
@@ -1,5 +1,6 @@
 package org.argeo.init.osgi;
 
+import java.lang.System.LoggerFinder;
 import java.util.Collections;
 import java.util.Hashtable;
 import java.util.Map;
@@ -11,6 +12,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;
@@ -54,6 +56,9 @@ public class OsgiRuntimeContext implements RuntimeContext {
        }
 
        public void start(BundleContext bundleContext) {
+               // Make sure LoggerFinder has been searched for, since it is lazily loaded
+               LoggerFinder.getLoggerFinder();
+
                // logging
                loggingConfigurationSr = bundleContext.registerService(Consumer.class,
                                ThinLoggerFinder.getConfigurationConsumer(),
@@ -73,25 +78,47 @@ 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();
+
+       }
+
+       public Framework getFramework() {
+               return framework;
        }
 
 }