]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/OsgiExecutionModulesManager.java
Some more UI functionalities :
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.osgi / src / main / java / org / argeo / slc / osgi / OsgiExecutionModulesManager.java
index 15a3f0de9bbcb27b56d75ad22fdb558e1975ce0a..d46ad48c5b3b09b71247dbcea719ec1a28d2a947 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2007-2012 Mathieu Baudier
+ * Copyright (C) 2007-2012 Argeo GmbH
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -45,9 +45,11 @@ import org.argeo.slc.execution.ExecutionFlowDescriptor;
 import org.argeo.slc.execution.ExecutionFlowDescriptorConverter;
 import org.argeo.slc.execution.ExecutionModuleDescriptor;
 import org.argeo.slc.execution.ExecutionModulesListener;
-import org.argeo.slc.process.RealizedFlow;
+import org.argeo.slc.execution.RealizedFlow;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleEvent;
 import org.osgi.framework.BundleException;
+import org.osgi.framework.BundleListener;
 import org.osgi.framework.Constants;
 import org.osgi.framework.launch.Framework;
 import org.springframework.context.ApplicationContext;
@@ -55,7 +57,8 @@ import org.springframework.osgi.service.importer.OsgiServiceLifecycleListener;
 
 /** Execution modules manager implementation based on an OSGi runtime. */
 public class OsgiExecutionModulesManager extends
-               AbstractExecutionModulesManager implements OsgiServiceLifecycleListener {
+               AbstractExecutionModulesManager implements
+               OsgiServiceLifecycleListener, BundleListener {
 
        private final static Log log = LogFactory
                        .getLog(OsgiExecutionModulesManager.class);
@@ -68,9 +71,11 @@ public class OsgiExecutionModulesManager extends
 
        private List<ExecutionModulesListener> executionModulesListeners = new ArrayList<ExecutionModulesListener>();
 
-       private Boolean registerFlowsToJmx = true;
+       private Boolean registerFlowsToJmx = false;
 
        public void init() throws Exception {
+               bundlesManager.getBundleContext().addBundleListener(this);
+
                final String module = System.getProperty(UNIQUE_LAUNCH_MODULE_PROPERTY);
                final String flow = System.getProperty(UNIQUE_LAUNCH_FLOW_PROPERTY);
                if (module != null) {
@@ -78,48 +83,53 @@ public class OsgiExecutionModulesManager extends
                        new Thread("Unique Flow") {
                                @Override
                                public void run() {
-                                       if (log.isDebugEnabled())
-                                               log.debug("Launch unique flow " + flow
-                                                               + " from module " + module);
-                                       try {
-                                               OsgiBundle osgiBundle = bundlesManager
-                                                               .findFromPattern(module);
-                                               Bundle moduleBundle = bundlesManager
-                                                               .findRelatedBundle(osgiBundle);
-                                               bundlesManager.startSynchronous(moduleBundle);
-                                               RealizedFlow lastLaunch = findRealizedFlow(module, flow);
-                                               if (lastLaunch == null)
-                                                       throw new SlcException("Cannot find launch for "
-                                                                       + module + " " + flow);
-                                               execute(lastLaunch);
-                                       } catch (Exception e) {
-                                               log.error("Error in unique flow " + flow
-                                                               + " from module " + module, e);
-                                       } finally {
-                                               if (log.isDebugEnabled())
-                                                       log.debug("Shutdown OSGi runtime...");
-                                               Framework framework = (Framework) bundlesManager
-                                                               .getBundleContext().getBundle(0);
-                                               try {
-                                                       // shutdown framework
-                                                       framework.stop();
-                                                       // wait 1 min for shutdown
-                                                       framework.waitForStop(60 * 1000);
-                                                       // close VM
-                                                       System.exit(0);
-                                               } catch (Exception e) {
-                                                       e.printStackTrace();
-                                                       System.exit(1);
-                                               }
-                                       }
+                                       executeFlowAndExit(module, null, flow);
                                }
                        }.start();
                }
-
        }
 
        public void destroy() {
+               bundlesManager.getBundleContext().removeBundleListener(this);
+       }
 
+       /** Executes a single flow and <b>stops the JVM</b> */
+       protected void executeFlowAndExit(final String module,
+                       final String version, final String flow) {
+               if (log.isDebugEnabled())
+                       log.debug("Launch unique flow " + flow + " from module " + module);
+               try {
+                       OsgiBundle osgiBundle = bundlesManager.findFromPattern(module);
+                       if (osgiBundle == null)
+                               throw new SlcException("No OSGi bundle found for " + module);
+                       Bundle moduleBundle = bundlesManager.findRelatedBundle(osgiBundle);
+                       bundlesManager.startSynchronous(moduleBundle);
+                       RealizedFlow lastLaunch = findRealizedFlow(module, flow);
+                       if (lastLaunch == null)
+                               throw new SlcException("Cannot find launch for " + module + " "
+                                               + flow);
+                       execute(lastLaunch);
+               } catch (Exception e) {
+                       log.error(
+                                       "Error in unique flow " + flow + " from module " + module,
+                                       e);
+               } finally {
+                       if (log.isDebugEnabled())
+                               log.debug("Shutdown OSGi runtime...");
+                       Framework framework = (Framework) bundlesManager.getBundleContext()
+                                       .getBundle(0);
+                       try {
+                               // shutdown framework
+                               framework.stop();
+                               // wait 1 min for shutdown
+                               framework.waitForStop(60 * 1000);
+                               // close VM
+                               System.exit(0);
+                       } catch (Exception e) {
+                               e.printStackTrace();
+                               System.exit(1);
+                       }
+               }
        }
 
        public synchronized ExecutionModuleDescriptor getExecutionModuleDescriptor(
@@ -181,28 +191,41 @@ public class OsgiExecutionModulesManager extends
 
        protected ExecutionFlow findExecutionFlow(String moduleName,
                        String moduleVersion, String flowName) {
-               String filter = "(&(Bundle-SymbolicName=" + moduleName
-                               + ")(org.springframework.osgi.bean.name=" + flowName + "))";
+               String filter = moduleVersion == null || moduleVersion.equals("0.0.0") ? "(&(Bundle-SymbolicName="
+                               + moduleName
+                               + ")(org.springframework.osgi.bean.name="
+                               + flowName + "))"
+                               : "(&(Bundle-SymbolicName=" + moduleName + ")(Bundle-Version="
+                                               + moduleVersion
+                                               + ")(org.springframework.osgi.bean.name=" + flowName
+                                               + "))";
                return bundlesManager.getSingleServiceStrict(ExecutionFlow.class,
                                filter, true);
        }
 
        protected ExecutionContext findExecutionContext(String moduleName,
                        String moduleVersion) {
-               String filter = "(&(Bundle-SymbolicName=" + moduleName
-                               + ")(Bundle-Version=" + moduleVersion + "))";
+               String filter = moduleFilter(moduleName, moduleVersion);
                return bundlesManager.getSingleServiceStrict(ExecutionContext.class,
                                filter, true);
        }
 
        protected ExecutionFlowDescriptorConverter findExecutionFlowDescriptorConverter(
                        String moduleName, String moduleVersion) {
-               String filter = "(&(Bundle-SymbolicName=" + moduleName
-                               + ")(Bundle-Version=" + moduleVersion + "))";
+               String filter = moduleFilter(moduleName, moduleVersion);
                return bundlesManager.getSingleService(
                                ExecutionFlowDescriptorConverter.class, filter, false);
        }
 
+       /** Only based on symbolic name if version is null or "0.0.0" */
+       protected String moduleFilter(String moduleName, String moduleVersion) {
+               return moduleVersion == null || moduleVersion.equals("0.0.0") ? "(Bundle-SymbolicName="
+                               + moduleName + ")"
+                               : "(&(Bundle-SymbolicName=" + moduleName + ")(Bundle-Version="
+                                               + moduleVersion + "))";
+
+       }
+
        /**
         * Builds a minimal realized flow, based on the provided information
         * (typically from the command line).
@@ -283,11 +306,12 @@ public class OsgiExecutionModulesManager extends
                try {
                        Bundle bundle = bundlesManager.findRelatedBundle(new OsgiBundle(
                                        nameVersion));
+                       if (bundle == null)
+                               throw new SlcException("Counld not find bundle for "
+                                               + nameVersion);
+
                        bundlesManager.startSynchronous(bundle);
-                       boolean isSpringInstrumented = bundle.getEntryPaths(
-                                       "/META-INF/spring").hasMoreElements()
-                                       || bundle.getHeaders().get("Spring-Context") == null;
-                       if (isSpringInstrumented) {
+                       if (isSpringInstrumented(bundle)) {
                                // Wait for Spring application context to be ready
                                String filter = "(Bundle-SymbolicName="
                                                + bundle.getSymbolicName() + ")";
@@ -305,6 +329,18 @@ public class OsgiExecutionModulesManager extends
                }
        }
 
+       /** Do it calmly in order to avoid NPE */
+       private Boolean isSpringInstrumented(Bundle bundle) {
+               Dictionary<?, ?> headers = bundle.getHeaders();
+               if (headers != null && headers.get("Spring-Context") != null)
+                       return true;
+               Enumeration<?> springEntryPaths = bundle
+                               .getEntryPaths("/META-INF/spring");
+               if (springEntryPaths != null && springEntryPaths.hasMoreElements())
+                       return true;
+               return false;
+       }
+
        public void stop(NameVersion nameVersion) {
                try {
                        Bundle bundle = bundlesManager.findRelatedBundle(new OsgiBundle(
@@ -383,8 +419,10 @@ public class OsgiExecutionModulesManager extends
                if (log.isTraceEnabled())
                        log.trace("Registered execution context from " + osgiBundle);
                // Notify
+               ModuleDescriptor md = osgiBundle.getModuleDescriptor();
+               md.setStarted(true);
                for (ExecutionModulesListener listener : executionModulesListeners)
-                       listener.executionModuleAdded(osgiBundle.getModuleDescriptor());
+                       listener.executionModuleAdded(md);
        }
 
        /** Unregisters an execution context. */
@@ -396,9 +434,10 @@ public class OsgiExecutionModulesManager extends
                        if (log.isTraceEnabled())
                                log.trace("Removed execution context from " + osgiBundle);
                        // Notify
+                       ModuleDescriptor md = osgiBundle.getModuleDescriptor();
+                       md.setStarted(false);
                        for (ExecutionModulesListener listener : executionModulesListeners)
-                               listener.executionModuleRemoved(osgiBundle
-                                               .getModuleDescriptor());
+                               listener.executionModuleRemoved(md);
                }
        }
 
@@ -473,6 +512,27 @@ public class OsgiExecutionModulesManager extends
                executionModulesListeners.remove(executionModulesListener);
        }
 
+       /*
+        * INTERFACE IMPLEMENTATIONS
+        */
+
+       public void bundleChanged(BundleEvent evt) {
+               Bundle bundle = evt.getBundle();
+               if (bundle.getHeaders().get(
+                               ExecutionModuleDescriptor.SLC_EXECUTION_MODULE) != null) {
+                       OsgiBundle osgiBundle = new OsgiBundle(bundle);
+                       if (evt.getType() == BundleEvent.INSTALLED)
+                               for (ExecutionModulesListener listener : executionModulesListeners)
+                                       listener.executionModuleAdded(osgiBundle
+                                                       .getModuleDescriptor());
+                       else if (evt.getType() == BundleEvent.UNINSTALLED)
+                               for (ExecutionModulesListener listener : executionModulesListeners)
+                                       listener.executionModuleRemoved(osgiBundle
+                                                       .getModuleDescriptor());
+               }
+
+       }
+
        @SuppressWarnings({ "rawtypes" })
        public synchronized void bind(Object service, Map properties)
                        throws Exception {