1 package org
.argeo
.slc
.jcr
.execution
;
3 import java
.util
.Arrays
;
4 import java
.util
.Iterator
;
7 import javax
.jcr
.RepositoryException
;
8 import javax
.jcr
.Session
;
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
.deploy
.Module
;
15 import org
.argeo
.slc
.execution
.ExecutionContext
;
16 import org
.argeo
.slc
.execution
.ExecutionFlow
;
17 import org
.argeo
.slc
.execution
.ExecutionModulesListener
;
18 import org
.argeo
.slc
.jcr
.SlcJcrConstants
;
19 import org
.argeo
.slc
.jcr
.SlcNames
;
20 import org
.argeo
.slc
.runtime
.SlcAgent
;
23 * Synchronizes the local execution runtime with a JCR repository. For the time
24 * being the state is completely reset from one start to another.
26 public class JcrExecutionModulesListener
implements ExecutionModulesListener
{
27 private final static Log log
= LogFactory
28 .getLog(JcrExecutionModulesListener
.class);
30 private Session session
;
31 private SlcAgent agent
;
35 String modulesPath
= getExecutionModulesPath();
36 // clean up previous state
37 if (session
.nodeExists(modulesPath
))
38 session
.getNode(modulesPath
).remove();
39 JcrUtils
.mkdirs(session
, modulesPath
);
41 } catch (RepositoryException e
) {
42 throw new SlcException(
43 "Cannot initialize JCR execution module listener", e
);
45 JcrUtils
.discardQuietly(session
);
49 public void dispose() {
51 String modulesPath
= getExecutionModulesPath();
52 // clean up previous state
53 if (session
.nodeExists(modulesPath
))
54 session
.getNode(modulesPath
).remove();
56 } catch (RepositoryException e
) {
57 throw new SlcException(
58 "Cannot dispose JCR execution module listener", e
);
60 JcrUtils
.discardQuietly(session
);
64 public void executionModuleAdded(Module module
,
65 ExecutionContext executionContext
) {
67 Node base
= session
.getNode(getExecutionModulesPath());
68 Node moduleName
= base
.hasNode(module
.getName()) ? base
69 .getNode(module
.getName()) : base
.addNode(module
.getName());
70 Node moduleVersion
= moduleName
.hasNode(module
.getVersion()) ? moduleName
71 .getNode(module
.getVersion()) : moduleName
.addNode(module
74 } catch (RepositoryException e
) {
75 throw new SlcException("Cannot add module " + module
, e
);
80 public void executionModuleRemoved(Module module
,
81 ExecutionContext executionContext
) {
83 Node base
= session
.getNode(getExecutionModulesPath());
84 if (base
.hasNode(module
.getName())) {
85 Node moduleName
= base
.getNode(module
.getName());
86 if (moduleName
.hasNode(module
.getVersion()))
87 moduleName
.getNode(module
.getVersion()).remove();
88 if (!moduleName
.hasNodes())
92 } catch (RepositoryException e
) {
93 throw new SlcException("Cannot remove module " + module
, e
);
97 public void executionFlowAdded(Module module
, ExecutionFlow executionFlow
) {
98 String path
= getExecutionFlowPath(module
, executionFlow
);
99 log
.debug("path=" + path
);
102 if (!session
.nodeExists(path
)) {
103 Node base
= session
.getNode(getExecutionModulesPath());
104 Node moduleNode
= base
.getNode(module
.getName() + '/'
105 + module
.getVersion());
106 String relativePath
= getExecutionFlowRelativePath(executionFlow
);
107 log
.debug("relativePath='" + relativePath
+ "'");
108 Iterator
<String
> names
= Arrays
.asList(relativePath
.split("/"))
110 Node currNode
= moduleNode
;
111 while (names
.hasNext()) {
112 String name
= names
.next();
113 if (currNode
.hasNode(name
))
114 currNode
= currNode
.getNode(name
);
117 currNode
= currNode
.addNode(name
);
119 flowNode
= currNode
.addNode(name
);
124 flowNode
= session
.getNode(path
);
126 } catch (RepositoryException e
) {
127 throw new SlcException("Cannot add flow " + executionFlow
128 + " from module " + module
, e
);
133 public void executionFlowRemoved(Module module
, ExecutionFlow executionFlow
) {
134 String path
= getExecutionFlowPath(module
, executionFlow
);
136 if (session
.nodeExists(path
)) {
137 Node flowNode
= session
.getNode(path
);
141 } catch (RepositoryException e
) {
142 throw new SlcException("Cannot remove flow " + executionFlow
143 + " from module " + module
, e
);
147 protected String
getExecutionFlowPath(Module module
,
148 ExecutionFlow executionFlow
) {
149 String relativePath
= getExecutionFlowRelativePath(executionFlow
);
150 return getExecutionModulesPath() + '/' + module
.getName() + '/'
151 + module
.getVersion() + '/' + relativePath
;
154 /** @return the relative path, never starts with '/' */
155 @SuppressWarnings("deprecation")
156 protected String
getExecutionFlowRelativePath(ExecutionFlow executionFlow
) {
157 String relativePath
= executionFlow
.getPath() == null ? executionFlow
158 .getName() : executionFlow
.getPath() + '/'
159 + executionFlow
.getName();
160 // we assume that it is more than one char long
161 if (relativePath
.charAt(0) == '/')
162 relativePath
= relativePath
.substring(1);
166 protected String
getExecutionModulesPath() {
167 return SlcJcrConstants
.VM_AGENT_FACTORY_PATH
+ '/'
168 + agent
.getAgentUuid() + '/' + SlcNames
.SLC_EXECUTION_MODULES
;
171 public void setSession(Session session
) {
172 this.session
= session
;
175 public void setAgent(SlcAgent agent
) {