]> git.argeo.org Git - gpl/argeo-jcr.git/blob - org.argeo.slc.jcr/src/org/argeo/slc/jcr/execution/JcrExecutionProcess.java
Releasing
[gpl/argeo-jcr.git] / org.argeo.slc.jcr / src / org / argeo / slc / jcr / execution / JcrExecutionProcess.java
1 package org.argeo.slc.jcr.execution;
2
3 import java.util.ArrayList;
4 import java.util.Calendar;
5 import java.util.GregorianCalendar;
6 import java.util.List;
7
8 import javax.jcr.Node;
9 import javax.jcr.NodeIterator;
10 import javax.jcr.Property;
11 import javax.jcr.Repository;
12 import javax.jcr.RepositoryException;
13
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;
25
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;
30
31 private Long nextLogLine = 1l;
32
33 public JcrExecutionProcess(Node node) {
34 this.node = node;
35 }
36
37 public synchronized String getUuid() {
38 try {
39 return node.getProperty(SLC_UUID).getString();
40 } catch (RepositoryException e) {
41 throw new SlcException("Cannot get uuid for " + node, e);
42 }
43 }
44
45 public synchronized String getStatus() {
46 try {
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
52 // exceptions
53 return UNKOWN;
54 }
55 }
56
57 public synchronized void setStatus(String status) {
58 try {
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
68 // exceptions
69 log.error("Cannot set status " + status + ": " + e);
70 }
71 }
72
73 /**
74 * Synchronized in order to make sure that there is no concurrent modification
75 * of {@link #nextLogLine}.
76 */
77 public synchronized void addSteps(List<ExecutionStep> steps) {
78 try {
79 steps: for (ExecutionStep step : steps) {
80 String type;
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;
91 else
92 // skip
93 continue steps;
94
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('@', '_');
101
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);
108
109 // System.out.println("Logged " + logEntry.getPath());
110
111 nextLogLine++;
112 }
113
114 // last modified properties needs to be manually updated
115 // see https://issues.apache.org/jira/browse/JCR-2233
116 JcrUtils.updateLastModified(node);
117
118 node.getSession().save();
119 } catch (Exception e) {
120 JcrUtils.discardUnderlyingSessionQuietly(node);
121 e.printStackTrace();
122 }
123 }
124
125 // public Node getNode() {
126 // return node;
127 // }
128
129 public List<RealizedFlow> getRealizedFlows() {
130 try {
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();
137
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);
142 }
143
144 RealizedFlow realizedFlow = new JcrRealizedFlow(realizedFlowNode);
145 if (realizedFlow != null)
146 realizedFlows.add(realizedFlow);
147 }
148 return realizedFlows;
149 } catch (RepositoryException e) {
150 throw new SlcException("Cannot get realized flows", e);
151 }
152 }
153
154 public String getNodePath() {
155 try {
156 return node.getPath();
157 } catch (RepositoryException e) {
158 throw new SlcException("Cannot get process node path for " + node, e);
159 }
160 }
161
162 public Repository getRepository() {
163 try {
164 return node.getSession().getRepository();
165 } catch (RepositoryException e) {
166 throw new SlcException("Cannot get process JCR repository for " + node, e);
167 }
168 }
169 }