]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/execution/JcrExecutionProcess.java
Introduce JCR test result
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.jcr / src / main / java / org / argeo / slc / jcr / execution / JcrExecutionProcess.java
1 package org.argeo.slc.jcr.execution;
2
3 import java.util.Calendar;
4 import java.util.GregorianCalendar;
5 import java.util.List;
6
7 import javax.jcr.Node;
8 import javax.jcr.RepositoryException;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.argeo.jcr.JcrUtils;
13 import org.argeo.slc.SlcException;
14 import org.argeo.slc.execution.ExecutionProcess;
15 import org.argeo.slc.execution.ExecutionStep;
16 import org.argeo.slc.jcr.SlcNames;
17 import org.argeo.slc.jcr.SlcTypes;
18
19 /** Execution process implementation based on a JCR node. */
20 public class JcrExecutionProcess implements ExecutionProcess, SlcNames {
21 private Log log = LogFactory.getLog(JcrExecutionProcess.class);
22 private final Node node;
23
24 private Long nextLogLine = 1l;
25
26 public JcrExecutionProcess(Node node) {
27 this.node = node;
28 }
29
30 public String getUuid() {
31 try {
32 return node.getProperty(SLC_UUID).getString();
33 } catch (RepositoryException e) {
34 throw new SlcException("Cannot get uuid for " + node, e);
35 }
36 }
37
38 public String getStatus() {
39 try {
40 return node.getProperty(SLC_STATUS).getString();
41 } catch (RepositoryException e) {
42 log.error("Cannot get status: " + e);
43 // we should re-throw exception because this information can
44 // probably used for monitoring in case there are already unexpected
45 // exceptions
46 return UNKOWN;
47 }
48 }
49
50 public void setStatus(String status) {
51 try {
52 node.setProperty(SLC_STATUS, status);
53 // last modified properties needs to be manually updated
54 // see https://issues.apache.org/jira/browse/JCR-2233
55 JcrUtils.updateLastModified(node);
56 node.getSession().save();
57 } catch (RepositoryException e) {
58 JcrUtils.discardUnderlyingSessionQuietly(node);
59 // we should re-throw exception because this information can
60 // probably used for monitoring in case there are already unexpected
61 // exceptions
62 log.error("Cannot set status " + status + ": " + e);
63 }
64 }
65
66 /**
67 * Synchronized in order to make sure that there is no concurrent
68 * modification of {@link #nextLogLine}.
69 */
70 public synchronized void addSteps(List<ExecutionStep> steps) {
71 try {
72 steps: for (ExecutionStep step : steps) {
73 String type;
74 if (step.getType().equals(ExecutionStep.TRACE))
75 type = SlcTypes.SLC_LOG_TRACE;
76 else if (step.getType().equals(ExecutionStep.DEBUG))
77 type = SlcTypes.SLC_LOG_DEBUG;
78 else if (step.getType().equals(ExecutionStep.INFO))
79 type = SlcTypes.SLC_LOG_INFO;
80 else if (step.getType().equals(ExecutionStep.WARNING))
81 type = SlcTypes.SLC_LOG_WARNING;
82 else if (step.getType().equals(ExecutionStep.ERROR))
83 type = SlcTypes.SLC_LOG_ERROR;
84 else
85 // skip
86 continue steps;
87
88 String relPath = SLC_LOG + '/' + step.getThread() + '/'
89 + step.getLocation().replace('.', '/');
90 String path = node.getPath() + '/' + relPath;
91 Node location = JcrUtils.mkdirs(node.getSession(), path);
92 Node logEntry = location.addNode(Long.toString(nextLogLine),
93 type);
94 logEntry.setProperty(SLC_MESSAGE, step.getLog());
95 Calendar calendar = new GregorianCalendar();
96 calendar.setTime(step.getTimestamp());
97 logEntry.setProperty(SLC_TIMESTAMP, calendar);
98
99 // System.out.println("Logged " + logEntry.getPath());
100
101 nextLogLine++;
102 }
103
104 // last modified properties needs to be manually updated
105 // see https://issues.apache.org/jira/browse/JCR-2233
106 JcrUtils.updateLastModified(node);
107
108 node.getSession().save();
109 } catch (RepositoryException e) {
110 JcrUtils.discardUnderlyingSessionQuietly(node);
111 e.printStackTrace();
112 }
113 }
114
115 public Node getNode() {
116 return node;
117 }
118
119 }