1 package org
.argeo
.slc
.jcr
.execution
;
3 import java
.util
.ArrayList
;
4 import java
.util
.Calendar
;
5 import java
.util
.GregorianCalendar
;
9 import javax
.jcr
.NodeIterator
;
10 import javax
.jcr
.Property
;
11 import javax
.jcr
.Repository
;
12 import javax
.jcr
.RepositoryException
;
14 import org
.apache
.commons
.logging
.Log
;
15 import org
.apache
.commons
.logging
.LogFactory
;
16 import org
.argeo
.jcr
.JcrUtils
;
17 import org
.argeo
.slc
.NameVersion
;
18 import org
.argeo
.slc
.SlcException
;
19 import org
.argeo
.slc
.SlcNames
;
20 import org
.argeo
.slc
.SlcTypes
;
21 import org
.argeo
.slc
.execution
.ExecutionProcess
;
22 import org
.argeo
.slc
.execution
.ExecutionStep
;
23 import org
.argeo
.slc
.execution
.RealizedFlow
;
24 import org
.argeo
.slc
.jcr
.SlcJcrUtils
;
25 import org
.argeo
.slc
.runtime
.ProcessThread
;
27 /** Execution process implementation based on a JCR node. */
28 public class JcrExecutionProcess
implements ExecutionProcess
, SlcNames
{
29 private final static Log log
= LogFactory
.getLog(JcrExecutionProcess
.class);
30 private final Node node
;
32 private Long nextLogLine
= 1l;
34 public JcrExecutionProcess(Node node
) {
38 public synchronized String
getUuid() {
40 return node
.getProperty(SLC_UUID
).getString();
41 } catch (RepositoryException e
) {
42 throw new SlcException("Cannot get uuid for " + node
, e
);
46 public synchronized String
getStatus() {
48 return node
.getProperty(SLC_STATUS
).getString();
49 } catch (RepositoryException e
) {
50 log
.error("Cannot get status: " + e
);
51 // we should re-throw exception because this information can
52 // probably used for monitoring in case there are already unexpected
58 public synchronized void setStatus(String status
) {
60 node
.setProperty(SLC_STATUS
, status
);
61 // last modified properties needs to be manually updated
62 // see https://issues.apache.org/jira/browse/JCR-2233
63 JcrUtils
.updateLastModified(node
);
64 node
.getSession().save();
65 } catch (RepositoryException e
) {
66 JcrUtils
.discardUnderlyingSessionQuietly(node
);
67 // we should re-throw exception because this information can
68 // probably used for monitoring in case there are already unexpected
70 log
.error("Cannot set status " + status
+ ": " + e
);
75 * Synchronized in order to make sure that there is no concurrent
76 * modification of {@link #nextLogLine}.
78 public synchronized void addSteps(List
<ExecutionStep
> steps
) {
80 steps
: for (ExecutionStep step
: steps
) {
82 if (step
.getType().equals(ExecutionStep
.TRACE
))
83 type
= SlcTypes
.SLC_LOG_TRACE
;
84 else if (step
.getType().equals(ExecutionStep
.DEBUG
))
85 type
= SlcTypes
.SLC_LOG_DEBUG
;
86 else if (step
.getType().equals(ExecutionStep
.INFO
))
87 type
= SlcTypes
.SLC_LOG_INFO
;
88 else if (step
.getType().equals(ExecutionStep
.WARNING
))
89 type
= SlcTypes
.SLC_LOG_WARNING
;
90 else if (step
.getType().equals(ExecutionStep
.ERROR
))
91 type
= SlcTypes
.SLC_LOG_ERROR
;
96 String relPath
= SLC_LOG
+ '/'
97 + step
.getThread().replace('/', '_') + '/'
98 + step
.getLocation().replace('.', '/');
99 String path
= node
.getPath() + '/' + relPath
;
100 // clean special character
101 // TODO factorize in JcrUtils
102 path
= path
.replace('@', '_');
104 Node location
= JcrUtils
.mkdirs(node
.getSession(), path
);
105 Node logEntry
= location
.addNode(Long
.toString(nextLogLine
),
107 logEntry
.setProperty(SLC_MESSAGE
, step
.getLog());
108 Calendar calendar
= new GregorianCalendar();
109 calendar
.setTime(step
.getTimestamp());
110 logEntry
.setProperty(SLC_TIMESTAMP
, calendar
);
112 // System.out.println("Logged " + logEntry.getPath());
117 // last modified properties needs to be manually updated
118 // see https://issues.apache.org/jira/browse/JCR-2233
119 JcrUtils
.updateLastModified(node
);
121 node
.getSession().save();
122 } catch (Exception e
) {
123 JcrUtils
.discardUnderlyingSessionQuietly(node
);
128 // public Node getNode() {
132 public List
<RealizedFlow
> getRealizedFlows() {
134 List
<RealizedFlow
> realizedFlows
= new ArrayList
<RealizedFlow
>();
135 Node rootRealizedFlowNode
= node
.getNode(SLC_FLOW
);
136 // we just manage one level for the time being
137 NodeIterator nit
= rootRealizedFlowNode
.getNodes(SLC_FLOW
);
138 while (nit
.hasNext()) {
139 Node realizedFlowNode
= nit
.nextNode();
141 if (realizedFlowNode
.hasNode(SLC_ADDRESS
)) {
142 String flowPath
= realizedFlowNode
.getNode(SLC_ADDRESS
)
143 .getProperty(Property
.JCR_PATH
).getString();
144 NameVersion moduleNameVersion
= SlcJcrUtils
145 .moduleNameVersion(flowPath
);
146 ((ProcessThread
) Thread
.currentThread())
147 .getExecutionModulesManager().start(
151 RealizedFlow realizedFlow
= new JcrRealizedFlow(
153 if (realizedFlow
!= null)
154 realizedFlows
.add(realizedFlow
);
156 return realizedFlows
;
157 } catch (RepositoryException e
) {
158 throw new SlcException("Cannot get realized flows", e
);
162 public String
getNodePath() {
164 return node
.getPath();
165 } catch (RepositoryException e
) {
166 throw new SlcException("Cannot get process node path for " + node
,
171 public Repository
getRepository() {
173 return node
.getSession().getRepository();
174 } catch (RepositoryException e
) {
175 throw new SlcException("Cannot get process JCR repository for "