]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/OsgiExecutionModulesManager.java
Unique launch
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.osgi / src / main / java / org / argeo / slc / osgi / OsgiExecutionModulesManager.java
index ed4b7faea33bcb7d80e84d2211f241fb3c33e741..c5d23589ef03ee09c5a53429359a7e36a412567d 100644 (file)
@@ -46,6 +46,7 @@ import org.argeo.slc.execution.ExecutionModuleDescriptor;
 import org.argeo.slc.execution.ExecutionModulesListener;
 import org.argeo.slc.process.RealizedFlow;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleException;
 import org.osgi.framework.Constants;
 import org.springframework.osgi.service.importer.OsgiServiceLifecycleListener;
 
@@ -66,6 +67,47 @@ public class OsgiExecutionModulesManager extends
 
        private Boolean registerFlowsToJmx = true;
 
+       public void init() throws Exception {
+               final String module = System.getProperty(UNIQUE_LAUNCH_MODULE_PROPERTY);
+               final String flow = System.getProperty(UNIQUE_LAUNCH_FLOW_PROPERTY);
+               if (module != null) {
+                       // launch a flow and stops
+                       new Thread("Unique Flow") {
+                               @Override
+                               public void run() {
+                                       try {
+                                               bundlesManager.startSynchronous(bundlesManager
+                                                               .findRelatedBundle(bundlesManager
+                                                                               .findFromPattern(module)));
+                                               RealizedFlow lastLaunch = findRealizedFlow(module, flow);
+                                               if (lastLaunch == null)
+                                                       throw new SlcException("Cannot find launch for "
+                                                                       + module + " " + flow);
+                                               execute(lastLaunch);
+                                       } catch (Exception e) {
+                                               throw new SlcException(
+                                                               "Error when executing unique flow " + flow
+                                                                               + " on " + module, e);
+                                       } finally {
+                                               try {
+                                                       bundlesManager.getBundleContext().getBundle(0)
+                                                                       .stop();
+                                                       System.exit(0);
+                                               } catch (Exception e) {
+                                                       log.error("Cannot shutdown equinox.", e);
+                                                       System.exit(1);
+                                               }
+                                       }
+                               }
+                       }.start();
+               }
+
+       }
+
+       public void destroy() {
+
+       }
+
        public synchronized ExecutionModuleDescriptor getExecutionModuleDescriptor(
                        String moduleName, String version) {
                ExecutionModuleDescriptor md = new ExecutionModuleDescriptor();
@@ -141,11 +183,10 @@ public class OsgiExecutionModulesManager extends
 
        protected ExecutionFlowDescriptorConverter findExecutionFlowDescriptorConverter(
                        String moduleName, String moduleVersion) {
-
                String filter = "(&(Bundle-SymbolicName=" + moduleName
                                + ")(Bundle-Version=" + moduleVersion + "))";
                return bundlesManager.getSingleService(
-                               ExecutionFlowDescriptorConverter.class, filter);
+                               ExecutionFlowDescriptorConverter.class, filter, false);
        }
 
        /**
@@ -224,6 +265,26 @@ public class OsgiExecutionModulesManager extends
                return lst;
        }
 
+       public void start(NameVersion nameVersion) {
+               try {
+                       Bundle bundle = bundlesManager.findRelatedBundle(new OsgiBundle(
+                                       nameVersion));
+                       bundlesManager.startSynchronous(bundle);
+               } catch (BundleException e) {
+                       throw new SlcException("Cannot start " + nameVersion, e);
+               }
+       }
+
+       public void stop(NameVersion nameVersion) {
+               try {
+                       Bundle bundle = bundlesManager.findRelatedBundle(new OsgiBundle(
+                                       nameVersion));
+                       bundlesManager.stopSynchronous(bundle);
+               } catch (BundleException e) {
+                       throw new SlcException("Cannot stop " + nameVersion, e);
+               }
+       }
+
        protected void setMetadataFromBundle(ModuleDescriptor md, Bundle bundle) {
                Bundle bdl = bundle;
                if (bdl == null) {
@@ -322,7 +383,7 @@ public class OsgiExecutionModulesManager extends
                        Set<ExecutionFlow> flows = executionFlows.get(osgiBundle);
                        flows.remove(executionFlow);
                        if (log.isTraceEnabled())
-                               log.debug("Removed " + executionFlow + " from " + osgiBundle);
+                               log.trace("Removed " + executionFlow + " from " + osgiBundle);
                        if (flows.size() == 0) {
                                executionFlows.remove(osgiBundle);
                                if (log.isTraceEnabled())