]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/execution/JcrProcessThread.java
support.jcr : extend SlcName with addtional Diff Result properties
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.jcr / src / main / java / org / argeo / slc / jcr / execution / JcrProcessThread.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.jcr.execution;
17
18 import java.util.HashMap;
19 import java.util.Map;
20
21 import javax.jcr.Node;
22 import javax.jcr.NodeIterator;
23 import javax.jcr.Property;
24 import javax.jcr.RepositoryException;
25
26 import org.argeo.ArgeoException;
27 import org.argeo.slc.SlcException;
28 import org.argeo.slc.core.execution.DefaultExecutionSpec;
29 import org.argeo.slc.core.execution.PrimitiveSpecAttribute;
30 import org.argeo.slc.core.execution.PrimitiveUtils;
31 import org.argeo.slc.core.execution.ProcessThread;
32 import org.argeo.slc.core.execution.RefSpecAttribute;
33 import org.argeo.slc.execution.ExecutionFlowDescriptor;
34 import org.argeo.slc.execution.ExecutionModulesManager;
35 import org.argeo.slc.execution.ExecutionProcess;
36 import org.argeo.slc.execution.ExecutionSpecAttribute;
37 import org.argeo.slc.jcr.SlcJcrUtils;
38 import org.argeo.slc.jcr.SlcNames;
39 import org.argeo.slc.jcr.SlcTypes;
40 import org.argeo.slc.process.RealizedFlow;
41
42 /** Where the actual execution takes place */
43 public class JcrProcessThread extends ProcessThread implements SlcNames {
44
45 public JcrProcessThread(ThreadGroup processesThreadGroup,
46 ExecutionModulesManager executionModulesManager,
47 JcrExecutionProcess process) {
48 super(processesThreadGroup, executionModulesManager, process);
49 }
50
51 @Override
52 protected void process() throws InterruptedException {
53 try {
54 Node rootRealizedFlowNode = getNode().getNode(SLC_FLOW);
55 // we just manage one level for the time being
56 NodeIterator nit = rootRealizedFlowNode.getNodes(SLC_FLOW);
57 while (nit.hasNext()) {
58 Node realizedFlowNode = nit.nextNode();
59
60 // set status on realized flow
61 realizedFlowNode.setProperty(SLC_STATUS,
62 ExecutionProcess.RUNNING);
63 realizedFlowNode.getSession().save();
64 try {
65 execute(realizedFlowNode);
66
67 // set status on realized flow
68 realizedFlowNode.setProperty(SLC_STATUS,
69 ExecutionProcess.COMPLETED);
70 realizedFlowNode.getSession().save();
71 } catch (RepositoryException e) {
72 throw e;
73 } catch (InterruptedException e) {
74 // set status on realized flow
75 realizedFlowNode.setProperty(SLC_STATUS,
76 ExecutionProcess.KILLED);
77 realizedFlowNode.getSession().save();
78 throw e;
79 } catch (RuntimeException e) {
80 // set status on realized flow
81 realizedFlowNode.setProperty(SLC_STATUS,
82 ExecutionProcess.ERROR);
83 realizedFlowNode.getSession().save();
84 throw e;
85 }
86 }
87 } catch (RepositoryException e) {
88 throw new ArgeoException("Cannot process " + getNode(), e);
89 }
90 }
91
92 /** Configure the realized flows */
93 protected void execute(Node realizedFlowNode) throws RepositoryException,
94 InterruptedException {
95 if (realizedFlowNode.hasNode(SLC_ADDRESS)) {
96 String flowPath = realizedFlowNode.getNode(SLC_ADDRESS)
97 .getProperty(Property.JCR_PATH).getString();
98 // TODO: convert to local path if remote
99
100 Node flowNode = realizedFlowNode.getSession().getNode(flowPath);
101 String flowName = flowNode.getProperty(SLC_NAME).getString();
102
103 Node executionModuleNode = flowNode.getSession().getNode(
104 SlcJcrUtils.modulePath(flowPath));
105 String executionModuleName = executionModuleNode.getProperty(
106 SLC_NAME).getString();
107 String executionModuleVersion = executionModuleNode.getProperty(
108 SLC_VERSION).getString();
109
110 RealizedFlow realizedFlow = new RealizedFlow();
111 realizedFlow.setModuleName(executionModuleName);
112 realizedFlow.setModuleVersion(executionModuleVersion);
113
114 // retrieve execution spec
115 DefaultExecutionSpec executionSpec = new DefaultExecutionSpec();
116 Map<String, ExecutionSpecAttribute> attrs = readExecutionSpecAttributes(realizedFlowNode);
117 executionSpec.setAttributes(attrs);
118
119 // set execution spec name
120 if (flowNode.hasProperty(SlcNames.SLC_SPEC)) {
121 Node executionSpecNode = flowNode.getProperty(SLC_SPEC)
122 .getNode();
123 executionSpec.setBeanName(executionSpecNode.getProperty(
124 SLC_NAME).getString());
125 }
126
127 // explicitly retrieve values
128 Map<String, Object> values = new HashMap<String, Object>();
129 for (String attrName : attrs.keySet()) {
130 ExecutionSpecAttribute attr = attrs.get(attrName);
131 Object value = attr.getValue();
132 values.put(attrName, value);
133 }
134
135 ExecutionFlowDescriptor efd = new ExecutionFlowDescriptor(flowName,
136 values, executionSpec);
137 realizedFlow.setFlowDescriptor(efd);
138
139 //
140 // EXECUTE THE FLOW
141 //
142 execute(realizedFlow, true);
143 //
144 }
145 }
146
147 protected Map<String, ExecutionSpecAttribute> readExecutionSpecAttributes(
148 Node node) {
149 try {
150 Map<String, ExecutionSpecAttribute> attrs = new HashMap<String, ExecutionSpecAttribute>();
151 for (NodeIterator nit = node.getNodes(); nit.hasNext();) {
152 Node specAttrNode = nit.nextNode();
153 if (specAttrNode
154 .isNodeType(SlcTypes.SLC_PRIMITIVE_SPEC_ATTRIBUTE)) {
155 String type = specAttrNode.getProperty(SLC_TYPE)
156 .getString();
157 Object value = null;
158 if (specAttrNode.hasProperty(SLC_VALUE)) {
159 String valueStr = specAttrNode.getProperty(SLC_VALUE)
160 .getString();
161 value = PrimitiveUtils.convert(type, valueStr);
162 }
163 PrimitiveSpecAttribute specAttr = new PrimitiveSpecAttribute(
164 type, value);
165 attrs.put(specAttrNode.getName(), specAttr);
166 } else if (specAttrNode
167 .isNodeType(SlcTypes.SLC_REF_SPEC_ATTRIBUTE)) {
168 if (!specAttrNode.hasProperty(SLC_VALUE)) {
169 continue;
170 }
171 Integer value = (int) specAttrNode.getProperty(SLC_VALUE)
172 .getLong();
173 RefSpecAttribute specAttr = new RefSpecAttribute();
174 NodeIterator children = specAttrNode.getNodes();
175 int index = 0;
176 String id = null;
177 while (children.hasNext()) {
178 Node child = children.nextNode();
179 if (index == value)
180 id = child.getName();
181 index++;
182 }
183 specAttr.setValue(id);
184 attrs.put(specAttrNode.getName(), specAttr);
185 }
186 // throw new SlcException("Unsupported spec attribute "
187 // + specAttrNode);
188 }
189 return attrs;
190 } catch (RepositoryException e) {
191 throw new SlcException("Cannot read spec attributes from " + node,
192 e);
193 }
194 }
195
196 protected Node getNode() {
197 return ((JcrExecutionProcess) getProcess()).getNode();
198 }
199 }