2 * Copyright (C) 2007-2012 Mathieu Baudier
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.jcr
.execution
;
18 import java
.util
.HashMap
;
21 import javax
.jcr
.Node
;
22 import javax
.jcr
.NodeIterator
;
23 import javax
.jcr
.Property
;
24 import javax
.jcr
.RepositoryException
;
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
.execution
.RealizedFlow
;
38 import org
.argeo
.slc
.jcr
.SlcJcrUtils
;
39 import org
.argeo
.slc
.jcr
.SlcNames
;
40 import org
.argeo
.slc
.jcr
.SlcTypes
;
42 /** Where the actual execution takes place */
43 public class JcrProcessThread
extends ProcessThread
implements SlcNames
{
45 public JcrProcessThread(ThreadGroup processesThreadGroup
,
46 ExecutionModulesManager executionModulesManager
,
47 JcrExecutionProcess process
) {
48 super(processesThreadGroup
, executionModulesManager
, process
);
52 protected void process() throws InterruptedException
{
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();
60 // set status on realized flow
61 realizedFlowNode
.setProperty(SLC_STATUS
,
62 ExecutionProcess
.RUNNING
);
63 realizedFlowNode
.getSession().save();
65 execute(realizedFlowNode
);
67 // set status on realized flow
68 realizedFlowNode
.setProperty(SLC_STATUS
,
69 ExecutionProcess
.COMPLETED
);
70 realizedFlowNode
.getSession().save();
71 } catch (RepositoryException e
) {
73 } catch (InterruptedException e
) {
74 // set status on realized flow
75 realizedFlowNode
.setProperty(SLC_STATUS
,
76 ExecutionProcess
.KILLED
);
77 realizedFlowNode
.getSession().save();
79 } catch (RuntimeException e
) {
80 // set status on realized flow
81 realizedFlowNode
.setProperty(SLC_STATUS
,
82 ExecutionProcess
.ERROR
);
83 realizedFlowNode
.getSession().save();
87 } catch (RepositoryException e
) {
88 throw new ArgeoException("Cannot process " + getNode(), e
);
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
100 Node flowNode
= realizedFlowNode
.getSession().getNode(flowPath
);
101 String flowName
= flowNode
.getProperty(SLC_NAME
).getString();
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();
110 RealizedFlow realizedFlow
= new RealizedFlow();
111 realizedFlow
.setModuleName(executionModuleName
);
112 realizedFlow
.setModuleVersion(executionModuleVersion
);
114 // retrieve execution spec
115 DefaultExecutionSpec executionSpec
= new DefaultExecutionSpec();
116 Map
<String
, ExecutionSpecAttribute
> attrs
= readExecutionSpecAttributes(realizedFlowNode
);
117 executionSpec
.setAttributes(attrs
);
119 // set execution spec name
120 if (flowNode
.hasProperty(SlcNames
.SLC_SPEC
)) {
121 Node executionSpecNode
= flowNode
.getProperty(SLC_SPEC
)
123 executionSpec
.setBeanName(executionSpecNode
.getProperty(
124 SLC_NAME
).getString());
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
);
135 ExecutionFlowDescriptor efd
= new ExecutionFlowDescriptor(flowName
,
136 values
, executionSpec
);
137 realizedFlow
.setFlowDescriptor(efd
);
142 execute(realizedFlow
, true);
147 protected Map
<String
, ExecutionSpecAttribute
> readExecutionSpecAttributes(
150 Map
<String
, ExecutionSpecAttribute
> attrs
= new HashMap
<String
, ExecutionSpecAttribute
>();
151 for (NodeIterator nit
= node
.getNodes(); nit
.hasNext();) {
152 Node specAttrNode
= nit
.nextNode();
154 .isNodeType(SlcTypes
.SLC_PRIMITIVE_SPEC_ATTRIBUTE
)) {
155 String type
= specAttrNode
.getProperty(SLC_TYPE
)
158 if (specAttrNode
.hasProperty(SLC_VALUE
)) {
159 String valueStr
= specAttrNode
.getProperty(SLC_VALUE
)
161 value
= PrimitiveUtils
.convert(type
, valueStr
);
163 PrimitiveSpecAttribute specAttr
= new PrimitiveSpecAttribute(
165 attrs
.put(specAttrNode
.getName(), specAttr
);
166 } else if (specAttrNode
167 .isNodeType(SlcTypes
.SLC_REF_SPEC_ATTRIBUTE
)) {
168 if (!specAttrNode
.hasProperty(SLC_VALUE
)) {
171 Integer value
= (int) specAttrNode
.getProperty(SLC_VALUE
)
173 RefSpecAttribute specAttr
= new RefSpecAttribute();
174 NodeIterator children
= specAttrNode
.getNodes();
177 while (children
.hasNext()) {
178 Node child
= children
.nextNode();
180 id
= child
.getName();
183 specAttr
.setValue(id
);
184 attrs
.put(specAttrNode
.getName(), specAttr
);
186 // throw new SlcException("Unsupported spec attribute "
190 } catch (RepositoryException e
) {
191 throw new SlcException("Cannot read spec attributes from " + node
,
196 protected Node
getNode() {
197 return ((JcrExecutionProcess
) getProcess()).getNode();