]> 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
e9a3b6380fd803dcc478b9a7e6375fe3f8eb91d1
[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 Argeo GmbH
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.ArrayList;
19 import java.util.Calendar;
20 import java.util.GregorianCalendar;
21 import java.util.List;
22
23 import javax.jcr.Node;
24 import javax.jcr.NodeIterator;
25 import javax.jcr.Repository;
26 import javax.jcr.RepositoryException;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.argeo.jcr.JcrUtils;
31 import org.argeo.slc.SlcException;
32 import org.argeo.slc.execution.ExecutionProcess;
33 import org.argeo.slc.execution.ExecutionStep;
34 import org.argeo.slc.execution.RealizedFlow;
35 import org.argeo.slc.jcr.SlcNames;
36 import org.argeo.slc.jcr.SlcTypes;
37
38 /** Execution process implementation based on a JCR node. */
39 public class JcrExecutionProcess implements ExecutionProcess, SlcNames {
40 private final static Log log = LogFactory.getLog(JcrExecutionProcess.class);
41 private final Node node;
42
43 private Long nextLogLine = 1l;
44
45 public JcrExecutionProcess(Node node) {
46 this.node = node;
47 }
48
49 public synchronized String getUuid() {
50 try {
51 return node.getProperty(SLC_UUID).getString();
52 } catch (RepositoryException e) {
53 throw new SlcException("Cannot get uuid for " + node, e);
54 }
55 }
56
57 public synchronized String getStatus() {
58 try {
59 return node.getProperty(SLC_STATUS).getString();
60 } catch (RepositoryException e) {
61 log.error("Cannot get status: " + e);
62 // we should re-throw exception because this information can
63 // probably used for monitoring in case there are already unexpected
64 // exceptions
65 return UNKOWN;
66 }
67 }
68
69 public synchronized void setStatus(String status) {
70 try {
71 node.setProperty(SLC_STATUS, status);
72 // last modified properties needs to be manually updated
73 // see https://issues.apache.org/jira/browse/JCR-2233
74 JcrUtils.updateLastModified(node);
75 node.getSession().save();
76 } catch (RepositoryException e) {
77 JcrUtils.discardUnderlyingSessionQuietly(node);
78 // we should re-throw exception because this information can
79 // probably used for monitoring in case there are already unexpected
80 // exceptions
81 log.error("Cannot set status " + status + ": " + e);
82 }
83 }
84
85 /**
86 * Synchronized in order to make sure that there is no concurrent
87 * modification of {@link #nextLogLine}.
88 */
89 public synchronized void addSteps(List<ExecutionStep> steps) {
90 try {
91 steps: for (ExecutionStep step : steps) {
92 String type;
93 if (step.getType().equals(ExecutionStep.TRACE))
94 type = SlcTypes.SLC_LOG_TRACE;
95 else if (step.getType().equals(ExecutionStep.DEBUG))
96 type = SlcTypes.SLC_LOG_DEBUG;
97 else if (step.getType().equals(ExecutionStep.INFO))
98 type = SlcTypes.SLC_LOG_INFO;
99 else if (step.getType().equals(ExecutionStep.WARNING))
100 type = SlcTypes.SLC_LOG_WARNING;
101 else if (step.getType().equals(ExecutionStep.ERROR))
102 type = SlcTypes.SLC_LOG_ERROR;
103 else
104 // skip
105 continue steps;
106
107 String relPath = SLC_LOG + '/'
108 + step.getThread().replace('/', '_') + '/'
109 + step.getLocation().replace('.', '/');
110 String path = node.getPath() + '/' + relPath;
111 // clean special character
112 // TODO factorize in JcrUtils
113 path = path.replace('@', '_');
114
115 Node location = JcrUtils.mkdirs(node.getSession(), path);
116 Node logEntry = location.addNode(Long.toString(nextLogLine),
117 type);
118 logEntry.setProperty(SLC_MESSAGE, step.getLog());
119 Calendar calendar = new GregorianCalendar();
120 calendar.setTime(step.getTimestamp());
121 logEntry.setProperty(SLC_TIMESTAMP, calendar);
122
123 // System.out.println("Logged " + logEntry.getPath());
124
125 nextLogLine++;
126 }
127
128 // last modified properties needs to be manually updated
129 // see https://issues.apache.org/jira/browse/JCR-2233
130 JcrUtils.updateLastModified(node);
131
132 node.getSession().save();
133 } catch (Exception e) {
134 JcrUtils.discardUnderlyingSessionQuietly(node);
135 e.printStackTrace();
136 }
137 }
138
139 // public Node getNode() {
140 // return node;
141 // }
142
143 public List<RealizedFlow> getRealizedFlows() {
144 try {
145 List<RealizedFlow> realizedFlows = new ArrayList<RealizedFlow>();
146 Node rootRealizedFlowNode = node.getNode(SLC_FLOW);
147 // we just manage one level for the time being
148 NodeIterator nit = rootRealizedFlowNode.getNodes(SLC_FLOW);
149 while (nit.hasNext()) {
150 Node realizedFlowNode = nit.nextNode();
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 }