]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.jcr/src/org/argeo/slc/jcr/execution/JcrExecutionProcess.java
Remove license header.
[gpl/argeo-slc.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.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;
26
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;
31
32 private Long nextLogLine = 1l;
33
34 public JcrExecutionProcess(Node node) {
35 this.node = node;
36 }
37
38 public synchronized String getUuid() {
39 try {
40 return node.getProperty(SLC_UUID).getString();
41 } catch (RepositoryException e) {
42 throw new SlcException("Cannot get uuid for " + node, e);
43 }
44 }
45
46 public synchronized String getStatus() {
47 try {
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
53 // exceptions
54 return UNKOWN;
55 }
56 }
57
58 public synchronized void setStatus(String status) {
59 try {
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
69 // exceptions
70 log.error("Cannot set status " + status + ": " + e);
71 }
72 }
73
74 /**
75 * Synchronized in order to make sure that there is no concurrent
76 * modification of {@link #nextLogLine}.
77 */
78 public synchronized void addSteps(List<ExecutionStep> steps) {
79 try {
80 steps: for (ExecutionStep step : steps) {
81 String type;
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;
92 else
93 // skip
94 continue steps;
95
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('@', '_');
103
104 Node location = JcrUtils.mkdirs(node.getSession(), path);
105 Node logEntry = location.addNode(Long.toString(nextLogLine),
106 type);
107 logEntry.setProperty(SLC_MESSAGE, step.getLog());
108 Calendar calendar = new GregorianCalendar();
109 calendar.setTime(step.getTimestamp());
110 logEntry.setProperty(SLC_TIMESTAMP, calendar);
111
112 // System.out.println("Logged " + logEntry.getPath());
113
114 nextLogLine++;
115 }
116
117 // last modified properties needs to be manually updated
118 // see https://issues.apache.org/jira/browse/JCR-2233
119 JcrUtils.updateLastModified(node);
120
121 node.getSession().save();
122 } catch (Exception e) {
123 JcrUtils.discardUnderlyingSessionQuietly(node);
124 e.printStackTrace();
125 }
126 }
127
128 // public Node getNode() {
129 // return node;
130 // }
131
132 public List<RealizedFlow> getRealizedFlows() {
133 try {
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();
140
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(
148 moduleNameVersion);
149 }
150
151 RealizedFlow realizedFlow = new JcrRealizedFlow(
152 realizedFlowNode);
153 if (realizedFlow != null)
154 realizedFlows.add(realizedFlow);
155 }
156 return realizedFlows;
157 } catch (RepositoryException e) {
158 throw new SlcException("Cannot get realized flows", e);
159 }
160 }
161
162 public String getNodePath() {
163 try {
164 return node.getPath();
165 } catch (RepositoryException e) {
166 throw new SlcException("Cannot get process node path for " + node,
167 e);
168 }
169 }
170
171 public Repository getRepository() {
172 try {
173 return node.getSession().getRepository();
174 } catch (RepositoryException e) {
175 throw new SlcException("Cannot get process JCR repository for "
176 + node, e);
177 }
178 }
179 }