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
.argeo
.api
.cms
.CmsLog
;
15 import org
.argeo
.jcr
.JcrUtils
;
16 import org
.argeo
.slc
.NameVersion
;
17 import org
.argeo
.slc
.SlcException
;
18 import org
.argeo
.slc
.SlcNames
;
19 import org
.argeo
.slc
.SlcTypes
;
20 import org
.argeo
.slc
.execution
.ExecutionProcess
;
21 import org
.argeo
.slc
.execution
.ExecutionStep
;
22 import org
.argeo
.slc
.execution
.RealizedFlow
;
23 import org
.argeo
.slc
.jcr
.SlcJcrUtils
;
24 import org
.argeo
.slc
.runtime
.ProcessThread
;
26 /** Execution process implementation based on a JCR node. */
27 public class JcrExecutionProcess
implements ExecutionProcess
, SlcNames
{
28 private final static CmsLog log
= CmsLog
.getLog(JcrExecutionProcess
.class);
29 private final Node node
;
31 private Long nextLogLine
= 1l;
33 public JcrExecutionProcess(Node node
) {
37 public synchronized String
getUuid() {
39 return node
.getProperty(SLC_UUID
).getString();
40 } catch (RepositoryException e
) {
41 throw new SlcException("Cannot get uuid for " + node
, e
);
45 public synchronized String
getStatus() {
47 return node
.getProperty(SLC_STATUS
).getString();
48 } catch (RepositoryException e
) {
49 log
.error("Cannot get status: " + e
);
50 // we should re-throw exception because this information can
51 // probably used for monitoring in case there are already unexpected
57 public synchronized void setStatus(String status
) {
59 node
.setProperty(SLC_STATUS
, status
);
60 // last modified properties needs to be manually updated
61 // see https://issues.apache.org/jira/browse/JCR-2233
62 JcrUtils
.updateLastModified(node
);
63 node
.getSession().save();
64 } catch (RepositoryException e
) {
65 JcrUtils
.discardUnderlyingSessionQuietly(node
);
66 // we should re-throw exception because this information can
67 // probably used for monitoring in case there are already unexpected
69 log
.error("Cannot set status " + status
+ ": " + e
);
74 * Synchronized in order to make sure that there is no concurrent modification
75 * of {@link #nextLogLine}.
77 public synchronized void addSteps(List
<ExecutionStep
> steps
) {
79 steps
: for (ExecutionStep step
: steps
) {
81 if (step
.getType().equals(ExecutionStep
.TRACE
))
82 type
= SlcTypes
.SLC_LOG_TRACE
;
83 else if (step
.getType().equals(ExecutionStep
.DEBUG
))
84 type
= SlcTypes
.SLC_LOG_DEBUG
;
85 else if (step
.getType().equals(ExecutionStep
.INFO
))
86 type
= SlcTypes
.SLC_LOG_INFO
;
87 else if (step
.getType().equals(ExecutionStep
.WARNING
))
88 type
= SlcTypes
.SLC_LOG_WARNING
;
89 else if (step
.getType().equals(ExecutionStep
.ERROR
))
90 type
= SlcTypes
.SLC_LOG_ERROR
;
95 String relPath
= SLC_LOG
+ '/' + step
.getThread().replace('/', '_') + '/'
96 + step
.getLocation().replace('.', '/');
97 String path
= node
.getPath() + '/' + relPath
;
98 // clean special character
99 // TODO factorize in JcrUtils
100 path
= path
.replace('@', '_');
102 Node location
= JcrUtils
.mkdirs(node
.getSession(), path
);
103 Node logEntry
= location
.addNode(Long
.toString(nextLogLine
), type
);
104 logEntry
.setProperty(SLC_MESSAGE
, step
.getLog());
105 Calendar calendar
= new GregorianCalendar();
106 calendar
.setTime(step
.getTimestamp());
107 logEntry
.setProperty(SLC_TIMESTAMP
, calendar
);
109 // System.out.println("Logged " + logEntry.getPath());
114 // last modified properties needs to be manually updated
115 // see https://issues.apache.org/jira/browse/JCR-2233
116 JcrUtils
.updateLastModified(node
);
118 node
.getSession().save();
119 } catch (Exception e
) {
120 JcrUtils
.discardUnderlyingSessionQuietly(node
);
125 // public Node getNode() {
129 public List
<RealizedFlow
> getRealizedFlows() {
131 List
<RealizedFlow
> realizedFlows
= new ArrayList
<RealizedFlow
>();
132 Node rootRealizedFlowNode
= node
.getNode(SLC_FLOW
);
133 // we just manage one level for the time being
134 NodeIterator nit
= rootRealizedFlowNode
.getNodes(SLC_FLOW
);
135 while (nit
.hasNext()) {
136 Node realizedFlowNode
= nit
.nextNode();
138 if (realizedFlowNode
.hasNode(SLC_ADDRESS
)) {
139 String flowPath
= realizedFlowNode
.getNode(SLC_ADDRESS
).getProperty(Property
.JCR_PATH
).getString();
140 NameVersion moduleNameVersion
= SlcJcrUtils
.moduleNameVersion(flowPath
);
141 ((ProcessThread
) Thread
.currentThread()).getExecutionModulesManager().start(moduleNameVersion
);
144 RealizedFlow realizedFlow
= new JcrRealizedFlow(realizedFlowNode
);
145 if (realizedFlow
!= null)
146 realizedFlows
.add(realizedFlow
);
148 return realizedFlows
;
149 } catch (RepositoryException e
) {
150 throw new SlcException("Cannot get realized flows", e
);
154 public String
getNodePath() {
156 return node
.getPath();
157 } catch (RepositoryException e
) {
158 throw new SlcException("Cannot get process node path for " + node
, e
);
162 public Repository
getRepository() {
164 return node
.getSession().getRepository();
165 } catch (RepositoryException e
) {
166 throw new SlcException("Cannot get process JCR repository for " + node
, e
);