2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.jcr
.execution
;
18 import java
.util
.ArrayList
;
19 import java
.util
.Calendar
;
20 import java
.util
.GregorianCalendar
;
21 import java
.util
.List
;
23 import javax
.jcr
.Node
;
24 import javax
.jcr
.NodeIterator
;
25 import javax
.jcr
.Repository
;
26 import javax
.jcr
.RepositoryException
;
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
;
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
;
43 private Long nextLogLine
= 1l;
45 public JcrExecutionProcess(Node node
) {
49 public synchronized String
getUuid() {
51 return node
.getProperty(SLC_UUID
).getString();
52 } catch (RepositoryException e
) {
53 throw new SlcException("Cannot get uuid for " + node
, e
);
57 public synchronized String
getStatus() {
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
69 public synchronized void setStatus(String status
) {
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
81 log
.error("Cannot set status " + status
+ ": " + e
);
86 * Synchronized in order to make sure that there is no concurrent
87 * modification of {@link #nextLogLine}.
89 public synchronized void addSteps(List
<ExecutionStep
> steps
) {
91 steps
: for (ExecutionStep step
: steps
) {
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
;
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('@', '_');
115 Node location
= JcrUtils
.mkdirs(node
.getSession(), path
);
116 Node logEntry
= location
.addNode(Long
.toString(nextLogLine
),
118 logEntry
.setProperty(SLC_MESSAGE
, step
.getLog());
119 Calendar calendar
= new GregorianCalendar();
120 calendar
.setTime(step
.getTimestamp());
121 logEntry
.setProperty(SLC_TIMESTAMP
, calendar
);
123 // System.out.println("Logged " + logEntry.getPath());
128 // last modified properties needs to be manually updated
129 // see https://issues.apache.org/jira/browse/JCR-2233
130 JcrUtils
.updateLastModified(node
);
132 node
.getSession().save();
133 } catch (Exception e
) {
134 JcrUtils
.discardUnderlyingSessionQuietly(node
);
139 // public Node getNode() {
143 public List
<RealizedFlow
> getRealizedFlows() {
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(
153 if (realizedFlow
!= null)
154 realizedFlows
.add(realizedFlow
);
156 return realizedFlows
;
157 } catch (RepositoryException e
) {
158 throw new SlcException("Cannot get realized flows", e
);
162 public String
getNodePath() {
164 return node
.getPath();
165 } catch (RepositoryException e
) {
166 throw new SlcException("Cannot get process node path for " + node
,
171 public Repository
getRepository() {
173 return node
.getSession().getRepository();
174 } catch (RepositoryException e
) {
175 throw new SlcException("Cannot get process JCR repository for "