]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/OsgiExecutionModulesManager.java
Revert explicit security context propagation
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.osgi / src / main / java / org / argeo / slc / osgi / OsgiExecutionModulesManager.java
index ed4b7faea33bcb7d80e84d2211f241fb3c33e741..f2fc44f810f19bbf6d90617442030d7cc4c868ce 100644 (file)
@@ -46,7 +46,9 @@ 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.osgi.framework.launch.Framework;
 import org.springframework.osgi.service.importer.OsgiServiceLifecycleListener;
 
 /** Execution modules manager implementation based on an OSGi runtime. */
@@ -66,6 +68,58 @@ 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() {
+                                       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);
+                                               }
+                                       }
+                               }
+                       }.start();
+               }
+
+       }
+
+       public void destroy() {
+
+       }
+
        public synchronized ExecutionModuleDescriptor getExecutionModuleDescriptor(
                        String moduleName, String version) {
                ExecutionModuleDescriptor md = new ExecutionModuleDescriptor();
@@ -128,7 +182,7 @@ public class OsgiExecutionModulesManager extends
                String filter = "(&(Bundle-SymbolicName=" + moduleName
                                + ")(org.springframework.osgi.bean.name=" + flowName + "))";
                return bundlesManager.getSingleServiceStrict(ExecutionFlow.class,
-                               filter);
+                               filter, true);
        }
 
        protected ExecutionContext findExecutionContext(String moduleName,
@@ -136,16 +190,15 @@ public class OsgiExecutionModulesManager extends
                String filter = "(&(Bundle-SymbolicName=" + moduleName
                                + ")(Bundle-Version=" + moduleVersion + "))";
                return bundlesManager.getSingleServiceStrict(ExecutionContext.class,
-                               filter);
+                               filter, true);
        }
 
        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 +277,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 +395,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())
@@ -435,7 +508,7 @@ public class OsgiExecutionModulesManager extends
                String path = executionFlow.getPath();
                String name = executionFlow.getName();
                if (path == null && name.indexOf('/') >= 0) {
-                       path = name.substring(0, name.lastIndexOf('/') - 1);
+                       path = name.substring(0, name.lastIndexOf('/'));
                        name = name.substring(name.lastIndexOf('/'));
                }