]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/execution/JcrExecutionModulesListener.java
Introduce Agent JCR
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.jcr / src / main / java / org / argeo / slc / jcr / execution / JcrExecutionModulesListener.java
index dd91d5dee21718bab4d558c89eee46ca94b3aa17..7a01e6746974d15ceed925990138db0f3d723e2b 100644 (file)
@@ -4,6 +4,7 @@ import java.util.Arrays;
 import java.util.Iterator;
 
 import javax.jcr.Node;
+import javax.jcr.Property;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 
@@ -11,25 +12,28 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.jcr.JcrUtils;
 import org.argeo.slc.SlcException;
-import org.argeo.slc.deploy.Module;
-import org.argeo.slc.execution.ExecutionContext;
-import org.argeo.slc.execution.ExecutionFlow;
+import org.argeo.slc.deploy.ModuleDescriptor;
+import org.argeo.slc.execution.ExecutionFlowDescriptor;
 import org.argeo.slc.execution.ExecutionModulesListener;
+import org.argeo.slc.jcr.SlcJcrConstants;
+import org.argeo.slc.jcr.SlcNames;
+import org.argeo.slc.jcr.SlcTypes;
+import org.argeo.slc.runtime.SlcAgent;
 
 /**
- * Synchronizes the execution runtime with JCR. For the time being the state is
- * completely reset from one start to another.
+ * Synchronizes the local execution runtime with a JCR repository. For the time
+ * being the state is completely reset from one start to another.
  */
 public class JcrExecutionModulesListener implements ExecutionModulesListener {
        private final static Log log = LogFactory
                        .getLog(JcrExecutionModulesListener.class);
 
-       private String modulesPath = "/slc/modules";
-
        private Session session;
+       private SlcAgent agent;
 
        public void init() {
                try {
+                       String modulesPath = getExecutionModulesPath();
                        // clean up previous state
                        if (session.nodeExists(modulesPath))
                                session.getNode(modulesPath).remove();
@@ -45,6 +49,7 @@ public class JcrExecutionModulesListener implements ExecutionModulesListener {
 
        public void dispose() {
                try {
+                       String modulesPath = getExecutionModulesPath();
                        // clean up previous state
                        if (session.nodeExists(modulesPath))
                                session.getNode(modulesPath).remove();
@@ -57,50 +62,59 @@ public class JcrExecutionModulesListener implements ExecutionModulesListener {
                }
        }
 
-       public void executionModuleAdded(Module module,
-                       ExecutionContext executionContext) {
+       public void executionModuleAdded(ModuleDescriptor moduleDescriptor) {
                try {
-                       Node base = session.getNode(modulesPath);
-                       Node moduleName = base.hasNode(module.getName()) ? base
-                                       .getNode(module.getName()) : base.addNode(module.getName());
-                       Node moduleVersion = moduleName.hasNode(module.getVersion()) ? moduleName
-                                       .getNode(module.getVersion()) : moduleName.addNode(module
+                       Node base = session.getNode(getExecutionModulesPath());
+                       Node moduleName = base.hasNode(moduleDescriptor.getName()) ? base
+                                       .getNode(moduleDescriptor.getName()) : base
+                                       .addNode(moduleDescriptor.getName());
+                       Node moduleVersion = moduleName.hasNode(moduleDescriptor
+                                       .getVersion()) ? moduleName.getNode(moduleDescriptor
+                                       .getVersion()) : moduleName.addNode(moduleDescriptor
                                        .getVersion());
+                       moduleVersion.addMixin(SlcTypes.SLC_MODULE);
+                       moduleVersion.setProperty(SlcNames.SLC_NAME,
+                                       moduleDescriptor.getName());
+                       moduleVersion.setProperty(SlcNames.SLC_VERSION,
+                                       moduleDescriptor.getVersion());
+                       moduleVersion.setProperty(Property.JCR_TITLE,
+                                       moduleDescriptor.getTitle());
+                       moduleVersion.setProperty(Property.JCR_DESCRIPTION,
+                                       moduleDescriptor.getDescription());
                        session.save();
                } catch (RepositoryException e) {
-                       throw new SlcException("Cannot add module " + module, e);
+                       throw new SlcException("Cannot add module " + moduleDescriptor, e);
                }
 
        }
 
-       public void executionModuleRemoved(Module module,
-                       ExecutionContext executionContext) {
+       public void executionModuleRemoved(ModuleDescriptor moduleDescriptor) {
                try {
-                       Node base = session.getNode(modulesPath);
-                       if (base.hasNode(module.getName())) {
-                               Node moduleName = base.getNode(module.getName());
-                               if (moduleName.hasNode(module.getVersion()))
-                                       moduleName.getNode(module.getVersion()).remove();
+                       Node base = session.getNode(getExecutionModulesPath());
+                       if (base.hasNode(moduleDescriptor.getName())) {
+                               Node moduleName = base.getNode(moduleDescriptor.getName());
+                               if (moduleName.hasNode(moduleDescriptor.getVersion()))
+                                       moduleName.getNode(moduleDescriptor.getVersion()).remove();
                                if (!moduleName.hasNodes())
                                        moduleName.remove();
                                session.save();
                        }
                } catch (RepositoryException e) {
-                       throw new SlcException("Cannot remove module " + module, e);
+                       throw new SlcException("Cannot remove module " + moduleDescriptor,
+                                       e);
                }
        }
 
-       public void executionFlowAdded(Module module, ExecutionFlow executionFlow) {
+       public void executionFlowAdded(ModuleDescriptor module,
+                       ExecutionFlowDescriptor executionFlow) {
                String path = getExecutionFlowPath(module, executionFlow);
-               log.debug("path=" + path);
                try {
-                       Node flowNode;
+                       Node flowNode = null;
                        if (!session.nodeExists(path)) {
-                               Node base = session.getNode(modulesPath);
+                               Node base = session.getNode(getExecutionModulesPath());
                                Node moduleNode = base.getNode(module.getName() + '/'
                                                + module.getVersion());
                                String relativePath = getExecutionFlowRelativePath(executionFlow);
-                               log.debug("relativePath='" + relativePath + "'");
                                Iterator<String> names = Arrays.asList(relativePath.split("/"))
                                                .iterator();
                                Node currNode = moduleNode;
@@ -115,10 +129,15 @@ public class JcrExecutionModulesListener implements ExecutionModulesListener {
                                                        flowNode = currNode.addNode(name);
                                        }
                                }
+                               flowNode.addMixin(SlcTypes.SLC_EXECUTION_FLOW);
+                               flowNode.setProperty(SlcNames.SLC_NAME, executionFlow.getName());
                                session.save();
                        } else {
                                flowNode = session.getNode(path);
                        }
+
+                       if (log.isTraceEnabled())
+                               log.trace("Flow " + executionFlow + " added to JCR");
                } catch (RepositoryException e) {
                        throw new SlcException("Cannot add flow " + executionFlow
                                        + " from module " + module, e);
@@ -126,7 +145,8 @@ public class JcrExecutionModulesListener implements ExecutionModulesListener {
 
        }
 
-       public void executionFlowRemoved(Module module, ExecutionFlow executionFlow) {
+       public void executionFlowRemoved(ModuleDescriptor module,
+                       ExecutionFlowDescriptor executionFlow) {
                String path = getExecutionFlowPath(module, executionFlow);
                try {
                        if (session.nodeExists(path)) {
@@ -140,16 +160,17 @@ public class JcrExecutionModulesListener implements ExecutionModulesListener {
                }
        }
 
-       protected String getExecutionFlowPath(Module module,
-                       ExecutionFlow executionFlow) {
+       protected String getExecutionFlowPath(ModuleDescriptor module,
+                       ExecutionFlowDescriptor executionFlow) {
                String relativePath = getExecutionFlowRelativePath(executionFlow);
-               return modulesPath + '/' + module.getName() + '/' + module.getVersion()
-                               + '/' + relativePath;
+               return getExecutionModulesPath() + '/' + module.getName() + '/'
+                               + module.getVersion() + '/' + relativePath;
        }
 
        /** @return the relative path, never starts with '/' */
        @SuppressWarnings("deprecation")
-       protected String getExecutionFlowRelativePath(ExecutionFlow executionFlow) {
+       protected String getExecutionFlowRelativePath(
+                       ExecutionFlowDescriptor executionFlow) {
                String relativePath = executionFlow.getPath() == null ? executionFlow
                                .getName() : executionFlow.getPath() + '/'
                                + executionFlow.getName();
@@ -159,8 +180,17 @@ public class JcrExecutionModulesListener implements ExecutionModulesListener {
                return relativePath;
        }
 
+       protected String getExecutionModulesPath() {
+               return SlcJcrConstants.VM_AGENT_FACTORY_PATH + '/'
+                               + agent.getAgentUuid() + '/' + SlcNames.SLC_EXECUTION_MODULES;
+       }
+
        public void setSession(Session session) {
                this.session = session;
        }
 
+       public void setAgent(SlcAgent agent) {
+               this.agent = agent;
+       }
+
 }