Improve init
[lgpl/argeo-commons.git] / org.argeo.init / src / org / argeo / init / osgi / Activator.java
index 3e0d8e1179c251d1299ed99657c354c07030cf44..b85b248b9e17c94f01be20070cb86e69f3a79dd4 100644 (file)
@@ -2,7 +2,9 @@ package org.argeo.init.osgi;
 
 import java.lang.System.Logger;
 import java.lang.System.Logger.Level;
+import java.util.Objects;
 
+import org.argeo.init.Service;
 import org.argeo.init.logging.ThinLoggerFinder;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
@@ -17,28 +19,45 @@ public class Activator implements BundleActivator {
                // must be called first
                ThinLoggerFinder.lazyInit();
        }
-       Logger logger = System.getLogger(Activator.class.getName());
+       private final static Logger logger = System.getLogger(Activator.class.getName());
 
        private Long checkpoint = null;
 
+       private boolean argeoInit = false;
+       /** Not null if we created it ourselves. */
+       private OsgiRuntimeContext runtimeContext;
+
        public void start(final BundleContext bundleContext) throws Exception {
-               logger.log(Level.DEBUG, () -> "Argeo init via OSGi activator");
-
-               // admin thread
-               Thread adminThread = new AdminThread(bundleContext);
-               adminThread.start();
-
-               // bootstrap
-               OsgiBoot osgiBoot = new OsgiBoot(bundleContext);
-               if (checkpoint == null) {
-                       osgiBoot.bootstrap();
-                       checkpoint = System.currentTimeMillis();
-               } else {
-                       osgiBoot.update();
-                       checkpoint = System.currentTimeMillis();
+               // The OSGi runtime was created by us, and therefore already initialized
+               argeoInit = Boolean.parseBoolean(bundleContext.getProperty(Service.PROP_ARGEO_INIT_MAIN));
+               if (!argeoInit) {
+                       if (runtimeContext == null) {
+                               runtimeContext = new OsgiRuntimeContext(bundleContext);
+                               logger.log(Level.DEBUG, () -> "Argeo init via OSGi activator");
+                       }
+
+                       // admin thread
+//             Thread adminThread = new AdminThread(bundleContext);
+//             adminThread.start();
+
+                       // bootstrap
+//             OsgiBoot osgiBoot = new OsgiBoot(bundleContext);
+                       if (checkpoint == null) {
+//                     osgiBoot.bootstrap();
+                               checkpoint = System.currentTimeMillis();
+                       } else {
+                               runtimeContext.update();
+                               checkpoint = System.currentTimeMillis();
+                       }
                }
        }
 
        public void stop(BundleContext context) throws Exception {
+               if (!argeoInit) {
+                       Objects.nonNull(runtimeContext);
+                       runtimeContext.stop(context);
+                       runtimeContext = null;
+               }
        }
+
 }