1 package org
.argeo
.slc
.jcr
.execution
;
3 import java
.util
.HashMap
;
7 import javax
.jcr
.NodeIterator
;
8 import javax
.jcr
.Property
;
9 import javax
.jcr
.RepositoryException
;
10 import javax
.jcr
.Session
;
12 import org
.argeo
.jcr
.JcrUtils
;
13 import org
.argeo
.slc
.SlcException
;
14 import org
.argeo
.slc
.SlcNames
;
15 import org
.argeo
.slc
.SlcTypes
;
16 import org
.argeo
.slc
.execution
.ExecutionFlowDescriptor
;
17 import org
.argeo
.slc
.execution
.ExecutionSpecAttribute
;
18 import org
.argeo
.slc
.execution
.RealizedFlow
;
19 import org
.argeo
.slc
.execution
.RefSpecAttribute
;
20 import org
.argeo
.slc
.jcr
.SlcJcrUtils
;
21 import org
.argeo
.slc
.primitive
.PrimitiveSpecAttribute
;
22 import org
.argeo
.slc
.primitive
.PrimitiveUtils
;
23 import org
.argeo
.slc
.runtime
.DefaultExecutionSpec
;
25 public class JcrRealizedFlow
extends RealizedFlow
implements SlcNames
{
26 private static final long serialVersionUID
= -3709453850260712001L;
29 public JcrRealizedFlow(Node node
) {
31 this.path
= node
.getPath();
33 } catch (RepositoryException e
) {
34 throw new SlcException("Cannot initialize from " + node
, e
);
38 protected void loadFromNode(Node realizedFlowNode
) throws RepositoryException
{
39 if (realizedFlowNode
.hasNode(SLC_ADDRESS
)) {
40 String flowPath
= realizedFlowNode
.getNode(SLC_ADDRESS
).getProperty(Property
.JCR_PATH
).getString();
41 // TODO: convert to local path if remote
42 // FIXME start related module
43 Session agentSession
= realizedFlowNode
.getSession().getRepository().login();
45 Node flowNode
= agentSession
.getNode(flowPath
);
46 String flowName
= flowNode
.getProperty(SLC_NAME
).getString();
47 String description
= null;
48 if (flowNode
.hasProperty(Property
.JCR_DESCRIPTION
))
49 description
= flowNode
.getProperty(Property
.JCR_DESCRIPTION
).getString();
51 Node executionModuleNode
= flowNode
.getSession().getNode(SlcJcrUtils
.modulePath(flowPath
));
52 String executionModuleName
= executionModuleNode
.getProperty(SLC_NAME
).getString();
53 String executionModuleVersion
= executionModuleNode
.getProperty(SLC_VERSION
).getString();
55 RealizedFlow realizedFlow
= this;
56 realizedFlow
.setModuleName(executionModuleName
);
57 realizedFlow
.setModuleVersion(executionModuleVersion
);
59 // retrieve execution spec
60 DefaultExecutionSpec executionSpec
= new DefaultExecutionSpec();
61 Map
<String
, ExecutionSpecAttribute
> attrs
= readExecutionSpecAttributes(realizedFlowNode
);
62 executionSpec
.setAttributes(attrs
);
64 // set execution spec name
65 if (flowNode
.hasProperty(SlcNames
.SLC_SPEC
)) {
66 Node executionSpecNode
= flowNode
.getProperty(SLC_SPEC
).getNode();
67 executionSpec
.setName(executionSpecNode
.getProperty(SLC_NAME
).getString());
70 // explicitly retrieve values
71 Map
<String
, Object
> values
= new HashMap
<String
, Object
>();
72 for (String attrName
: attrs
.keySet()) {
73 ExecutionSpecAttribute attr
= attrs
.get(attrName
);
74 Object value
= attr
.getValue();
75 values
.put(attrName
, value
);
78 ExecutionFlowDescriptor efd
= new ExecutionFlowDescriptor(flowName
, description
, values
, executionSpec
);
79 realizedFlow
.setFlowDescriptor(efd
);
82 JcrUtils
.logoutQuietly(agentSession
);
85 throw new SlcException("Unsupported realized flow " + realizedFlowNode
);
89 protected Map
<String
, ExecutionSpecAttribute
> readExecutionSpecAttributes(Node node
) {
91 Map
<String
, ExecutionSpecAttribute
> attrs
= new HashMap
<String
, ExecutionSpecAttribute
>();
92 for (NodeIterator nit
= node
.getNodes(); nit
.hasNext();) {
93 Node specAttrNode
= nit
.nextNode();
94 if (specAttrNode
.isNodeType(SlcTypes
.SLC_PRIMITIVE_SPEC_ATTRIBUTE
)) {
95 String type
= specAttrNode
.getProperty(SLC_TYPE
).getString();
97 if (specAttrNode
.hasProperty(SLC_VALUE
)) {
98 String valueStr
= specAttrNode
.getProperty(SLC_VALUE
).getString();
99 value
= PrimitiveUtils
.convert(type
, valueStr
);
101 PrimitiveSpecAttribute specAttr
= new PrimitiveSpecAttribute(type
, value
);
102 attrs
.put(specAttrNode
.getName(), specAttr
);
103 } else if (specAttrNode
.isNodeType(SlcTypes
.SLC_REF_SPEC_ATTRIBUTE
)) {
104 if (!specAttrNode
.hasProperty(SLC_VALUE
)) {
107 Integer value
= (int) specAttrNode
.getProperty(SLC_VALUE
).getLong();
108 RefSpecAttribute specAttr
= new RefSpecAttribute();
109 NodeIterator children
= specAttrNode
.getNodes();
112 while (children
.hasNext()) {
113 Node child
= children
.nextNode();
115 id
= child
.getName();
118 specAttr
.setValue(id
);
119 attrs
.put(specAttrNode
.getName(), specAttr
);
121 // throw new SlcException("Unsupported spec attribute "
125 } catch (RepositoryException e
) {
126 throw new SlcException("Cannot read spec attributes from " + node
, e
);
130 public String
getPath() {