]> 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
Fix special characters in JCR logging
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.jcr / src / main / java / org / argeo / slc / jcr / execution / JcrExecutionModulesListener.java
index 90b86f3d327549e76cb2693726795d102532e6c7..1f2a2bb3c79d49cf94b996019c1ab09b1c5b9e16 100644 (file)
@@ -7,6 +7,7 @@ import java.util.List;
 import javax.jcr.Node;
 import javax.jcr.NodeIterator;
 import javax.jcr.Property;
+import javax.jcr.Repository;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.nodetype.NodeType;
@@ -16,6 +17,7 @@ import org.apache.commons.logging.LogFactory;
 import org.argeo.jcr.JcrUtils;
 import org.argeo.slc.SlcException;
 import org.argeo.slc.core.execution.PrimitiveSpecAttribute;
+import org.argeo.slc.core.execution.PrimitiveValue;
 import org.argeo.slc.core.execution.RefSpecAttribute;
 import org.argeo.slc.core.execution.RefValueChoice;
 import org.argeo.slc.deploy.ModuleDescriptor;
@@ -34,7 +36,7 @@ import org.argeo.slc.jcr.SlcTypes;
  */
 public class JcrExecutionModulesListener implements ExecutionModulesListener,
                SlcNames {
-       private final static String SLC_EXECUTION_MODRULES_PROPERTY = "slc.executionModules";
+       private final static String SLC_EXECUTION_MODULES_PROPERTY = "slc.executionModules";
 
        private final static Log log = LogFactory
                        .getLog(JcrExecutionModulesListener.class);
@@ -42,6 +44,7 @@ public class JcrExecutionModulesListener implements ExecutionModulesListener,
 
        private ExecutionModulesManager modulesManager;
 
+       private Repository repository;
        /**
         * We don't use a thread bound session because many different threads will
         * call this critical component and we don't want to login each time. We
@@ -53,14 +56,15 @@ public class JcrExecutionModulesListener implements ExecutionModulesListener,
         * LIFECYCLE
         */
        public void init() {
-               clearAgent();
-               if (modulesManager != null) {
-                       List<ModuleDescriptor> moduleDescriptors = modulesManager
-                                       .listModules();
-                       String executionModules = System
-                                       .getProperty(SLC_EXECUTION_MODRULES_PROPERTY);
-                       if (executionModules != null)
-                               try {
+               try {
+                       session = repository.login();
+                       clearAgent();
+                       if (modulesManager != null) {
+                               List<ModuleDescriptor> moduleDescriptors = modulesManager
+                                               .listModules();
+                               String executionModules = System
+                                               .getProperty(SLC_EXECUTION_MODULES_PROPERTY);
+                               if (executionModules != null) {
                                        Node agentNode = session.getNode(agent.getNodePath());
                                        for (String executionModule : executionModules.split(",")) {
                                                for (ModuleDescriptor moduleDescriptor : moduleDescriptors) {
@@ -88,16 +92,18 @@ public class JcrExecutionModulesListener implements ExecutionModulesListener,
                                                }
                                        }
                                        session.save();
-                               } catch (RepositoryException e) {
-                                       JcrUtils.discardQuietly(session);
-                                       throw new SlcException("Cannot initialize modules", e);
                                }
+                       }
+               } catch (RepositoryException e) {
+                       JcrUtils.discardQuietly(session);
+                       JcrUtils.logoutQuietly(session);
+                       throw new SlcException("Cannot initialize modules", e);
                }
        }
 
-       public void dispose() {
+       public void destroy() {
                clearAgent();
-               session.logout();
+               JcrUtils.logoutQuietly(session);
        }
 
        protected synchronized void clearAgent() {
@@ -207,17 +213,24 @@ public class JcrExecutionModulesListener implements ExecutionModulesListener,
                                                        SlcTypes.SLC_EXECUTION_FLOW);
                        }
                }
+
+               // name, description
                flowNode.setProperty(SLC_NAME, efd.getName());
                String[] tokens = relativePath.split("/");
-               flowNode.setProperty(Property.JCR_TITLE, tokens[tokens.length - 1]);
+               String endName = tokens[tokens.length - 1];
+               flowNode.setProperty(Property.JCR_TITLE, endName);
                if (efd.getDescription() != null
-                               && efd.getDescription().trim().equals(""))
+                               && !efd.getDescription().trim().equals("")) {
                        flowNode.setProperty(Property.JCR_DESCRIPTION, efd.getDescription());
+               } else {
+                       flowNode.setProperty(Property.JCR_DESCRIPTION, endName);
+               }
 
                // execution spec
                ExecutionSpec executionSpec = efd.getExecutionSpec();
                String esName = executionSpec.getName();
                if (!(esName == null || esName.equals(ExecutionSpec.INTERNAL_NAME))) {
+                       // reference spec node
                        Node executionSpecsNode = moduleNode.hasNode(SLC_EXECUTION_SPECS) ? moduleNode
                                        .getNode(SLC_EXECUTION_SPECS) : moduleNode
                                        .addNode(SLC_EXECUTION_SPECS);
@@ -232,8 +245,23 @@ public class JcrExecutionModulesListener implements ExecutionModulesListener,
                        mapExecutionSpec(executionSpecNode, executionSpec);
                        flowNode.setProperty(SLC_SPEC, executionSpecNode);
                } else {
+                       // internal spec node
                        mapExecutionSpec(flowNode, executionSpec);
                }
+
+               // values
+               for (String attr : efd.getValues().keySet()) {
+                       ExecutionSpecAttribute esa = executionSpec.getAttributes()
+                                       .get(attr);
+                       if (esa instanceof PrimitiveSpecAttribute) {
+                               PrimitiveSpecAttribute psa = (PrimitiveSpecAttribute) esa;
+                               Node valueNode = flowNode.addNode(attr);
+                               valueNode.setProperty(SLC_TYPE, psa.getType());
+                               SlcJcrUtils.setPrimitiveAsProperty(valueNode, SLC_VALUE,
+                                               (PrimitiveValue) efd.getValues().get(attr));
+                       }
+               }
+
                return flowNode;
        }
 
@@ -319,9 +347,8 @@ public class JcrExecutionModulesListener implements ExecutionModulesListener,
                this.agent = agent;
        }
 
-       /** Expects a non-shared session with admin authorization */
-       public void setSession(Session session) {
-               this.session = session;
+       public void setRepository(Repository repository) {
+               this.repository = repository;
        }
 
        public void setModulesManager(ExecutionModulesManager modulesManager) {