]> git.argeo.org Git - gpl/argeo-slc.git/commitdiff
Improve runtime
authorMathieu Baudier <mbaudier@argeo.org>
Thu, 29 Mar 2012 15:05:13 +0000 (15:05 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Thu, 29 Mar 2012 15:05:13 +0000 (15:05 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@5279 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

eclipse/plugins/org.argeo.slc.client.ui/src/main/java/org/argeo/slc/client/ui/editors/ProcessBuilderPage.java
runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/SlcJcrUtils.java
runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/execution/JcrExecutionModulesListener.java
runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/execution/JcrExecutionProcess.java
runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/execution/JcrProcessThread.java
runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/OsgiExecutionModulesManager.java

index b46b2f1b095e4d418dd62964be031e9a9b97b6de..958be82768183a694ae30d1b508df16bdfd7b02b 100644 (file)
@@ -572,7 +572,11 @@ public class ProcessBuilderPage extends FormPage implements SlcNames {
                                        if (node.hasNode(SLC_ADDRESS)) {
                                                String path = node.getNode(SLC_ADDRESS)
                                                                .getProperty(Property.JCR_PATH).getString();
-                                               return SlcJcrUtils.flowExecutionModuleName(path) + ":"
+                                               Node executionModuleNode = node.getSession().getNode(
+                                                               SlcJcrUtils.modulePath(path));
+                                               String executionModuleName = executionModuleNode
+                                                               .getProperty(SLC_NAME).getString();
+                                               return executionModuleName + ":"
                                                                + SlcJcrUtils.flowRelativePath(path);
                                        }
                                }
index 2ebcb933fb21853a3c20d85e46368e86a28c0a2a..57d8ce55c65563dbb8f22057fce32a2138235ff3 100644 (file)
@@ -46,23 +46,20 @@ public class SlcJcrUtils implements SlcNames {
                return buf.toString();
        }
 
-       /** Module node name based on module name and version */
-       public static String getModuleNodeName(ModuleDescriptor moduleDescriptor) {
-               return moduleDescriptor.getName() + "_" + moduleDescriptor.getVersion();
-       }
-
-       /** Extracts the execution module name of a flow */
-       public static String flowExecutionModuleName(String fullFlowPath) {
+       /** Extracts the path to the related execution module */
+       public static String modulePath(String fullFlowPath) {
                String[] tokens = fullFlowPath.split("/");
-               String moduleNodeName = tokens[AGENT_FACTORY_DEPTH + 2];
-               return moduleNodeName.substring(0, moduleNodeName.lastIndexOf('_'));
+               StringBuffer buf = new StringBuffer(fullFlowPath.length());
+               for (int i = 0; i < AGENT_FACTORY_DEPTH + 3; i++) {
+                       if (!tokens[i].equals(""))
+                               buf.append('/').append(tokens[i]);
+               }
+               return buf.toString();
        }
 
-       /** Extracts the execution module version of a flow */
-       public static String flowExecutionModuleVersion(String fullFlowPath) {
-               String[] tokens = fullFlowPath.split("/");
-               String moduleNodeName = tokens[AGENT_FACTORY_DEPTH + 2];
-               return moduleNodeName.substring(moduleNodeName.lastIndexOf('_') + 1);
+       /** Module node name based on module name and version */
+       public static String getModuleNodeName(ModuleDescriptor moduleDescriptor) {
+               return moduleDescriptor.getName() + "_" + moduleDescriptor.getVersion();
        }
 
        /** Extracts the agent factory of a flow */
@@ -189,20 +186,9 @@ public class SlcJcrUtils implements SlcNames {
                                        + node, e);
                }
        }
-       
+
        /** Prevents instantiation */
        private SlcJcrUtils() {
 
        }
-
-       public static void main(String[] args) {
-               String path = "/slc/agents/vm/default/org.argeo_1.2.3/myPath/myFlow";
-               System.out.println("Flow relative path: " + flowRelativePath(path));
-               System.out.println("Execution Module Name: "
-                               + flowExecutionModuleName(path));
-               System.out.println("Execution Module Version: "
-                               + flowExecutionModuleVersion(path));
-               System.out.println("Agent Factory path: " + flowAgentFactoryPath(path));
-       }
-
 }
index 8d6d96784ab64562ea0e2e78b3647d01c9188b3e..b66f0940541bf7d1fffa5d4af3c171c5ea9aa1b5 100644 (file)
@@ -163,8 +163,13 @@ public class JcrExecutionModulesListener implements ExecutionModulesListener,
        /*
         * EXECUTION MODULES LISTENER
         */
+
        public synchronized void executionModuleAdded(
                        ModuleDescriptor moduleDescriptor) {
+               syncExecutionModule(moduleDescriptor);
+       }
+
+       protected void syncExecutionModule(ModuleDescriptor moduleDescriptor) {
                try {
                        Node agentNode = session.getNode(agent.getNodePath());
                        String moduleNodeName = SlcJcrUtils
@@ -179,13 +184,12 @@ public class JcrExecutionModulesListener implements ExecutionModulesListener,
                                        moduleDescriptor.getTitle());
                        moduleNode.setProperty(Property.JCR_DESCRIPTION,
                                        moduleDescriptor.getDescription());
-                       moduleNode.setProperty(SLC_STARTED, true);
+                       moduleNode.setProperty(SLC_STARTED, moduleDescriptor.getStarted());
                        session.save();
                } catch (RepositoryException e) {
                        JcrUtils.discardQuietly(session);
-                       throw new SlcException("Cannot add module " + moduleDescriptor, e);
+                       throw new SlcException("Cannot sync module " + moduleDescriptor, e);
                }
-
        }
 
        public synchronized void executionModuleRemoved(
index cca76d95ba57d98e8e36116c1bfb9bba04607893..5825351771e207ca03f692f3546dbf904a42c89a 100644 (file)
@@ -106,7 +106,7 @@ public class JcrExecutionProcess implements ExecutionProcess, SlcNames {
                                String path = node.getPath() + '/' + relPath;
                                // clean special character
                                // TODO factorize in JcrUtils
-                               path = path.replace('@', '_').replace(':', '_');
+                               path = path.replace('@', '_');
 
                                Node location = JcrUtils.mkdirs(node.getSession(), path);
                                Node logEntry = location.addNode(Long.toString(nextLogLine),
index 09145863388e386b00bf5cbbaf0350abe2f44eb7..0fcca450432d246b41b699de319b0c7d9ec975a2 100644 (file)
@@ -100,10 +100,12 @@ public class JcrProcessThread extends ProcessThread implements SlcNames {
                        Node flowNode = realizedFlowNode.getSession().getNode(flowPath);
                        String flowName = flowNode.getProperty(SLC_NAME).getString();
 
-                       String executionModuleName = SlcJcrUtils
-                                       .flowExecutionModuleName(flowPath);
-                       String executionModuleVersion = SlcJcrUtils
-                                       .flowExecutionModuleVersion(flowPath);
+                       Node executionModuleNode = flowNode.getSession().getNode(
+                                       SlcJcrUtils.modulePath(flowPath));
+                       String executionModuleName = executionModuleNode.getProperty(
+                                       SLC_NAME).getString();
+                       String executionModuleVersion = executionModuleNode.getProperty(
+                                       SLC_VERSION).getString();
 
                        RealizedFlow realizedFlow = new RealizedFlow();
                        realizedFlow.setModuleName(executionModuleName);
index 15a3f0de9bbcb27b56d75ad22fdb558e1975ce0a..58ae32a655ee9f7f521c98f7473a2a01636b8d03 100644 (file)
@@ -47,7 +47,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.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,51 @@ 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);
+                       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(
@@ -383,8 +391,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 +406,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 +484,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 {