]> 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 module meta data
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.jcr / src / main / java / org / argeo / slc / jcr / execution / JcrExecutionModulesListener.java
index 88d3ae17e9ad1e4f801eadce054ed987ab8f96d8..8d6d96784ab64562ea0e2e78b3647d01c9188b3e 100644 (file)
@@ -1,3 +1,18 @@
+/*
+ * Copyright (C) 2007-2012 Mathieu Baudier
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *         http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 package org.argeo.slc.jcr.execution;
 
 import java.util.Arrays;
@@ -7,6 +22,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;
@@ -21,6 +37,7 @@ import org.argeo.slc.core.execution.RefSpecAttribute;
 import org.argeo.slc.core.execution.RefValueChoice;
 import org.argeo.slc.deploy.ModuleDescriptor;
 import org.argeo.slc.execution.ExecutionFlowDescriptor;
+import org.argeo.slc.execution.ExecutionModuleDescriptor;
 import org.argeo.slc.execution.ExecutionModulesListener;
 import org.argeo.slc.execution.ExecutionModulesManager;
 import org.argeo.slc.execution.ExecutionSpec;
@@ -43,6 +60,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
@@ -54,51 +72,80 @@ public class JcrExecutionModulesListener implements ExecutionModulesListener,
         * LIFECYCLE
         */
        public void init() {
-               clearAgent();
-               if (modulesManager != null) {
-                       List<ModuleDescriptor> moduleDescriptors = modulesManager
-                                       .listModules();
-                       String executionModules = System
-                                       .getProperty(SLC_EXECUTION_MODULES_PROPERTY);
-                       if (executionModules != null)
-                               try {
-                                       Node agentNode = session.getNode(agent.getNodePath());
+               try {
+                       session = repository.login();
+                       clearAgent();
+                       if (modulesManager != null) {
+                               Node agentNode = session.getNode(agent.getNodePath());
+
+                               List<ModuleDescriptor> moduleDescriptors = modulesManager
+                                               .listModules();
+
+                               // scan SLC-ExecutionModule metadata
+                               for (ModuleDescriptor md : moduleDescriptors) {
+                                       if (md.getMetadata().containsKey(
+                                                       ExecutionModuleDescriptor.SLC_EXECUTION_MODULE)) {
+                                               String moduleNodeName = SlcJcrUtils
+                                                               .getModuleNodeName(md);
+                                               Node moduleNode = agentNode.hasNode(moduleNodeName) ? agentNode
+                                                               .getNode(moduleNodeName) : agentNode
+                                                               .addNode(moduleNodeName);
+                                               moduleNode.addMixin(SlcTypes.SLC_EXECUTION_MODULE);
+                                               moduleNode.setProperty(SLC_NAME, md.getName());
+                                               moduleNode.setProperty(SLC_VERSION, md.getVersion());
+                                               moduleNode.setProperty(Property.JCR_TITLE,
+                                                               md.getTitle());
+                                               moduleNode.setProperty(Property.JCR_DESCRIPTION,
+                                                               md.getDescription());
+                                               moduleNode.setProperty(SLC_STARTED, md.getStarted());
+                                       }
+                               }
+
+                               // scan execution modules property
+                               String executionModules = System
+                                               .getProperty(SLC_EXECUTION_MODULES_PROPERTY);
+                               if (executionModules != null) {
                                        for (String executionModule : executionModules.split(",")) {
-                                               for (ModuleDescriptor moduleDescriptor : moduleDescriptors) {
+                                               allModules: for (ModuleDescriptor md : moduleDescriptors) {
                                                        String moduleNodeName = SlcJcrUtils
-                                                                       .getModuleNodeName(moduleDescriptor);
-                                                       if (moduleDescriptor.getName().equals(
-                                                                       executionModule)) {
+                                                                       .getModuleNodeName(md);
+                                                       if (md.getName().equals(executionModule)) {
                                                                Node moduleNode = agentNode
                                                                                .hasNode(moduleNodeName) ? agentNode
                                                                                .getNode(moduleNodeName) : agentNode
                                                                                .addNode(moduleNodeName);
                                                                moduleNode
                                                                                .addMixin(SlcTypes.SLC_EXECUTION_MODULE);
-                                                               moduleNode.setProperty(SLC_NAME,
-                                                                               moduleDescriptor.getName());
+                                                               moduleNode.setProperty(SLC_NAME, md.getName());
                                                                moduleNode.setProperty(SLC_VERSION,
-                                                                               moduleDescriptor.getVersion());
+                                                                               md.getVersion());
                                                                moduleNode.setProperty(Property.JCR_TITLE,
-                                                                               moduleDescriptor.getTitle());
+                                                                               md.getTitle());
                                                                moduleNode.setProperty(
                                                                                Property.JCR_DESCRIPTION,
-                                                                               moduleDescriptor.getDescription());
-                                                               moduleNode.setProperty(SLC_STARTED, false);
+                                                                               md.getDescription());
+                                                               moduleNode.setProperty(SLC_STARTED,
+                                                                               md.getStarted());
+                                                               break allModules;
                                                        }
                                                }
                                        }
-                                       session.save();
-                               } catch (RepositoryException e) {
-                                       JcrUtils.discardQuietly(session);
-                                       throw new SlcException("Cannot initialize modules", e);
+
+                                       // save if needed
+                                       if (session.hasPendingChanges())
+                                               session.save();
                                }
+                       }
+               } 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() {
@@ -285,8 +332,14 @@ public class JcrExecutionModulesListener implements ExecutionModulesListener,
                                attrNode.addMixin(SlcTypes.SLC_REF_SPEC_ATTRIBUTE);
                                RefSpecAttribute rsa = (RefSpecAttribute) esa;
                                attrNode.setProperty(SLC_TYPE, rsa.getTargetClassName());
+                               Object value = rsa.getValue();
                                if (rsa.getChoices() != null) {
+                                       Integer index = null;
+                                       int count = 0;
                                        for (RefValueChoice choice : rsa.getChoices()) {
+                                               String name = choice.getName();
+                                               if (value != null && name.equals(value.toString()))
+                                                       index = count;
                                                Node choiceNode = attrNode.addNode(choice.getName());
                                                choiceNode.addMixin(NodeType.MIX_TITLE);
                                                choiceNode.setProperty(Property.JCR_TITLE,
@@ -295,7 +348,11 @@ public class JcrExecutionModulesListener implements ExecutionModulesListener,
                                                                && !choice.getDescription().trim().equals(""))
                                                        choiceNode.setProperty(Property.JCR_DESCRIPTION,
                                                                        choice.getDescription());
+                                               count++;
                                        }
+
+                                       if (index != null)
+                                               attrNode.setProperty(SLC_VALUE, index);
                                }
                        }
                }
@@ -308,7 +365,7 @@ public class JcrExecutionModulesListener implements ExecutionModulesListener,
                        Node moduleNode = agentNode.getNode(SlcJcrUtils
                                        .getModuleNodeName(module));
                        String relativePath = getExecutionFlowRelativePath(executionFlow);
-                       if (!moduleNode.hasNode(relativePath))
+                       if (moduleNode.hasNode(relativePath))
                                moduleNode.getNode(relativePath).remove();
                        agentNode.getSession().save();
                } catch (RepositoryException e) {
@@ -342,9 +399,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) {