Improve subframework launch
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / osgi / CmsActivator.java
index 59b226a53c61a284ade9a84288cb373c6048831c..94b08da4d316a37d7d614c74904c91c09a72ced3 100644 (file)
@@ -1,9 +1,10 @@
 package org.argeo.cms.internal.osgi;
 
 import java.security.AllPermission;
-import java.util.Dictionary;
 
-import org.argeo.api.cms.CmsLog;
+import org.argeo.api.cms.CmsState;
+import org.argeo.api.uuid.NodeIdSupplier;
+import org.argeo.cms.internal.runtime.CmsStateImpl;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
@@ -19,49 +20,14 @@ import org.osgi.service.permissionadmin.PermissionInfo;
  * bundle (and only it)
  */
 public class CmsActivator implements BundleActivator {
-       private final static CmsLog log = CmsLog.getLog(CmsActivator.class);
+//     private final static CmsLog log = CmsLog.getLog(CmsActivator.class);
+       private final static String PROP_ARGEO_OSGI_PARENT_UUID = "argeo.osgi.parent.uuid";
 
        // TODO make it configurable
        private boolean hardened = false;
 
        private static BundleContext bundleContext;
 
-//     private LogReaderService logReaderService;
-//
-//     private CmsOsgiLogger logger;
-
-       void init() {
-//             Runtime.getRuntime().addShutdownHook(new CmsShutdown());
-//             instance = this;
-//             this.bc = bundleContext;
-//             if (bundleContext != null)
-//                     this.logReaderService = getService(LogReaderService.class);
-//             initArgeoLogger();
-//             this.internalExecutorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
-//
-//             try {
-//                     initSecurity();
-////                   initArgeoLogger();
-//                     initNode();
-//
-//                     if (log.isTraceEnabled())
-//                             log.trace("Kernel bundle started");
-//             } catch (Throwable e) {
-//                     log.error("## FATAL: CMS activator failed", e);
-//             }
-       }
-
-       void destroy() {
-               try {
-                       bundleContext = null;
-//                     this.logReaderService = null;
-               } catch (Exception e) {
-                       log.error("CMS activator shutdown failed", e);
-               }
-
-               new GogoShellKiller().start();
-       }
-
        protected void initSecurity() {
                // code-level permissions
                String osgiSecurity = bundleContext.getProperty(Constants.FRAMEWORK_SECURITY);
@@ -98,20 +64,14 @@ public class CmsActivator implements BundleActivator {
 
        }
 
-//     private void initArgeoLogger() {
-//             logger = new CmsOsgiLogger(logReaderService);
-//             if (bundleContext != null)
-//                     bundleContext.registerService(ArgeoLogger.class, logger, null);
+//     static <T> void registerService(Class<T> clss, T service, Dictionary<String, ?> properties) {
+//             if (bundleContext != null) {
+//                     bundleContext.registerService(clss, service, properties);
+//             }
+//
 //     }
-
-       public static <T> void registerService(Class<T> clss, T service, Dictionary<String, ?> properties) {
-               if (bundleContext != null) {
-                       bundleContext.registerService(clss, service, properties);
-               }
-
-       }
-
-       public static <T> T getService(Class<T> clss) {
+//
+       static <T> T getService(Class<T> clss) {
                if (bundleContext != null) {
                        return bundleContext.getService(bundleContext.getServiceReference(clss));
                } else {
@@ -126,20 +86,32 @@ public class CmsActivator implements BundleActivator {
        @Override
        public void start(BundleContext bc) throws Exception {
                bundleContext = bc;
-
-               init();
-
+               CmsStateImpl cmsState = new CmsStateImpl();
+               cmsState.start();
+               bundleContext.registerService(new String[] { CmsState.class.getName(), NodeIdSupplier.class.getName() },
+                               cmsState, null);
        }
 
        @Override
        public void stop(BundleContext bc) throws Exception {
-
-               destroy();
-               bundleContext = null;
+               try {
+                       CmsStateImpl cmsState = (CmsStateImpl) getService(CmsState.class);
+                       String parentFrameworkuuid = bc.getProperty(PROP_ARGEO_OSGI_PARENT_UUID);
+                       if (parentFrameworkuuid == null)
+                               new GogoShellKiller().start();
+                       cmsState.stop();
+               } finally {
+                       bundleContext = null;
+               }
        }
 
-       public static BundleContext getBundleContext() {
+       static BundleContext getBundleContext() {
                return bundleContext;
        }
 
+       public static String getFrameworkProperty(String key) {
+               if (bundleContext == null)
+                       return null;
+               return getBundleContext().getProperty(key);
+       }
 }