]> 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
Primitive arguments working
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.jcr / src / main / java / org / argeo / slc / jcr / execution / JcrProcessThread.java
1 package org.argeo.slc.jcr.execution;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import javax.jcr.Node;
7 import javax.jcr.NodeIterator;
8 import javax.jcr.Property;
9 import javax.jcr.RepositoryException;
10
11 import org.argeo.ArgeoException;
12 import org.argeo.slc.SlcException;
13 import org.argeo.slc.core.execution.DefaultExecutionSpec;
14 import org.argeo.slc.core.execution.PrimitiveSpecAttribute;
15 import org.argeo.slc.core.execution.PrimitiveUtils;
16 import org.argeo.slc.core.execution.PrimitiveValue;
17 import org.argeo.slc.core.execution.ProcessThread;
18 import org.argeo.slc.execution.ExecutionFlowDescriptor;
19 import org.argeo.slc.execution.ExecutionModulesManager;
20 import org.argeo.slc.execution.ExecutionSpecAttribute;
21 import org.argeo.slc.jcr.SlcJcrUtils;
22 import org.argeo.slc.jcr.SlcNames;
23 import org.argeo.slc.jcr.SlcTypes;
24 import org.argeo.slc.process.RealizedFlow;
25
26 /** Where the actual execution takes place */
27 public class JcrProcessThread extends ProcessThread implements SlcNames {
28
29 public JcrProcessThread(ExecutionModulesManager executionModulesManager,
30 JcrExecutionProcess process) {
31 super(executionModulesManager, process);
32 }
33
34 @Override
35 protected void process() {
36 try {
37 Node realizedFlowNode = getNode().getNode(SLC_FLOW);
38 // we just manage one level for the time being
39 NodeIterator nit = realizedFlowNode.getNodes(SLC_FLOW);
40 while (nit.hasNext()) {
41 process(nit.nextNode());
42 }
43 } catch (RepositoryException e) {
44 throw new ArgeoException("Cannot process " + getNode(), e);
45 }
46 }
47
48 /** Configure the realized flows */
49 protected void process(Node realizedFlowNode) throws RepositoryException {
50 if (realizedFlowNode.hasNode(SLC_ADDRESS)) {
51 String flowPath = realizedFlowNode.getNode(SLC_ADDRESS)
52 .getProperty(Property.JCR_PATH).getString();
53 // TODO: convert to local path if remote
54
55 Node flowNode = realizedFlowNode.getSession().getNode(flowPath);
56 String flowName = flowNode.getProperty(SLC_NAME).getString();
57
58 String executionModuleName = SlcJcrUtils
59 .flowExecutionModuleName(flowPath);
60 String executionModuleVersion = SlcJcrUtils
61 .flowExecutionModuleVersion(flowPath);
62
63 RealizedFlow realizedFlow = new RealizedFlow();
64 realizedFlow.setModuleName(executionModuleName);
65 realizedFlow.setModuleVersion(executionModuleVersion);
66
67 DefaultExecutionSpec executionSpec = null;
68 if (flowNode.hasProperty(SlcNames.SLC_SPEC)) {
69 Node executionSpecNode = flowNode.getProperty(SLC_SPEC)
70 .getNode();
71 executionSpec = new DefaultExecutionSpec();
72 executionSpec.setBeanName(executionSpecNode.getProperty(
73 SLC_NAME).getString());
74 executionSpec
75 .setAttributes(readExecutionSpecAttributes(executionSpecNode));
76 }
77 // TODO: will with original attr
78 Map<String, ExecutionSpecAttribute> attrs = readExecutionSpecAttributes(realizedFlowNode);
79 Map<String, Object> values = new HashMap<String, Object>();
80 for (String attrName : attrs.keySet()) {
81 ExecutionSpecAttribute attr = attrs.get(attrName);
82 Object value = attr.getValue();
83 values.put(attrName,value);
84 }
85
86 // if(executionSpec!=null)
87 // executionSpec.setAttributes(attrs);
88 ExecutionFlowDescriptor efd = new ExecutionFlowDescriptor(flowName,
89 values, executionSpec);
90 realizedFlow.setFlowDescriptor(efd);
91
92 execute(realizedFlow, true);
93 }
94 }
95
96 protected Map<String, ExecutionSpecAttribute> readExecutionSpecAttributes(
97 Node node) {
98 try {
99 Map<String, ExecutionSpecAttribute> attrs = new HashMap<String, ExecutionSpecAttribute>();
100 for (NodeIterator nit = node.getNodes(); nit.hasNext();) {
101 Node specAttrNode = nit.nextNode();
102 if (specAttrNode
103 .isNodeType(SlcTypes.SLC_PRIMITIVE_SPEC_ATTRIBUTE)) {
104 String type = specAttrNode.getProperty(SLC_TYPE)
105 .getString();
106 if (specAttrNode.hasProperty(SLC_VALUE)) {
107 String valueStr = specAttrNode.getProperty(SLC_VALUE)
108 .getString();
109 Object value = PrimitiveUtils.convert(type, valueStr);
110 PrimitiveSpecAttribute specAttr = new PrimitiveSpecAttribute(
111 type, value);
112 attrs.put(specAttrNode.getName(), specAttr);
113 }
114 }
115
116 }
117 return attrs;
118 } catch (RepositoryException e) {
119 throw new SlcException("Cannot read spec attributes from " + node,
120 e);
121 }
122 }
123
124 protected Node getNode() {
125 return ((JcrExecutionProcess) getProcess()).getNode();
126 }
127 }