]> 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
Refactor JCR utils and home usage
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.jcr / src / main / java / org / argeo / slc / jcr / execution / JcrExecutionProcess.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.jcr.execution;
17
18 import java.util.Calendar;
19 import java.util.GregorianCalendar;
20 import java.util.List;
21
22 import javax.jcr.Node;
23 import javax.jcr.RepositoryException;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.argeo.jcr.JcrUtils;
28 import org.argeo.slc.SlcException;
29 import org.argeo.slc.execution.ExecutionProcess;
30 import org.argeo.slc.execution.ExecutionStep;
31 import org.argeo.slc.jcr.SlcNames;
32 import org.argeo.slc.jcr.SlcTypes;
33
34 /** Execution process implementation based on a JCR node. */
35 public class JcrExecutionProcess implements ExecutionProcess, SlcNames {
36 private Log log = LogFactory.getLog(JcrExecutionProcess.class);
37 private final Node node;
38
39 private Long nextLogLine = 1l;
40
41 public JcrExecutionProcess(Node node) {
42 this.node = node;
43 }
44
45 public String getUuid() {
46 try {
47 return node.getProperty(SLC_UUID).getString();
48 } catch (RepositoryException e) {
49 throw new SlcException("Cannot get uuid for " + node, e);
50 }
51 }
52
53 public String getStatus() {
54 try {
55 return node.getProperty(SLC_STATUS).getString();
56 } catch (RepositoryException e) {
57 log.error("Cannot get status: " + e);
58 // we should re-throw exception because this information can
59 // probably used for monitoring in case there are already unexpected
60 // exceptions
61 return UNKOWN;
62 }
63 }
64
65 public void setStatus(String status) {
66 try {
67 node.setProperty(SLC_STATUS, status);
68 // last modified properties needs to be manually updated
69 // see https://issues.apache.org/jira/browse/JCR-2233
70 JcrUtils.updateLastModified(node);
71 node.getSession().save();
72 } catch (RepositoryException e) {
73 JcrUtils.discardUnderlyingSessionQuietly(node);
74 // we should re-throw exception because this information can
75 // probably used for monitoring in case there are already unexpected
76 // exceptions
77 log.error("Cannot set status " + status + ": " + e);
78 }
79 }
80
81 /**
82 * Synchronized in order to make sure that there is no concurrent
83 * modification of {@link #nextLogLine}.
84 */
85 public synchronized void addSteps(List<ExecutionStep> steps) {
86 try {
87 steps: for (ExecutionStep step : steps) {
88 String type;
89 if (step.getType().equals(ExecutionStep.TRACE))
90 type = SlcTypes.SLC_LOG_TRACE;
91 else if (step.getType().equals(ExecutionStep.DEBUG))
92 type = SlcTypes.SLC_LOG_DEBUG;
93 else if (step.getType().equals(ExecutionStep.INFO))
94 type = SlcTypes.SLC_LOG_INFO;
95 else if (step.getType().equals(ExecutionStep.WARNING))
96 type = SlcTypes.SLC_LOG_WARNING;
97 else if (step.getType().equals(ExecutionStep.ERROR))
98 type = SlcTypes.SLC_LOG_ERROR;
99 else
100 // skip
101 continue steps;
102
103 String relPath = SLC_LOG + '/'
104 + step.getThread().replace('/', '_') + '/'
105 + step.getLocation().replace('.', '/');
106 String path = node.getPath() + '/' + relPath;
107 // clean special character
108 // TODO factorize in JcrUtils
109 path = path.replace('@', '_');
110
111 Node location = JcrUtils.mkdirs(node.getSession(), path);
112 Node logEntry = location.addNode(Long.toString(nextLogLine),
113 type);
114 logEntry.setProperty(SLC_MESSAGE, step.getLog());
115 Calendar calendar = new GregorianCalendar();
116 calendar.setTime(step.getTimestamp());
117 logEntry.setProperty(SLC_TIMESTAMP, calendar);
118
119 // System.out.println("Logged " + logEntry.getPath());
120
121 nextLogLine++;
122 }
123
124 // last modified properties needs to be manually updated
125 // see https://issues.apache.org/jira/browse/JCR-2233
126 JcrUtils.updateLastModified(node);
127
128 node.getSession().save();
129 } catch (Exception e) {
130 JcrUtils.discardUnderlyingSessionQuietly(node);
131 e.printStackTrace();
132 }
133 }
134
135 public Node getNode() {
136 return node;
137 }
138
139 }