Merge tag 'v2.3.28' into testing
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / osgi / CmsActivator.java
index b09956203cc2eb4afa0bfa60b2a1c22cf1fe9f5c..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,27 +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;
 
-       void init() {
-       }
-
-       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);
@@ -76,14 +64,14 @@ public class CmsActivator implements BundleActivator {
 
        }
 
-       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> void registerService(Class<T> clss, T service, Dictionary<String, ?> properties) {
+//             if (bundleContext != null) {
+//                     bundleContext.registerService(clss, service, properties);
+//             }
+//
+//     }
+//
+       static <T> T getService(Class<T> clss) {
                if (bundleContext != null) {
                        return bundleContext.getService(bundleContext.getServiceReference(clss));
                } else {
@@ -98,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);
+       }
 }