1 package org
.argeo
.slc
.jcr
.execution
;
3 import java
.util
.Arrays
;
4 import java
.util
.Iterator
;
7 import javax
.jcr
.Property
;
8 import javax
.jcr
.RepositoryException
;
9 import javax
.jcr
.Session
;
11 import org
.apache
.commons
.logging
.Log
;
12 import org
.apache
.commons
.logging
.LogFactory
;
13 import org
.argeo
.jcr
.JcrUtils
;
14 import org
.argeo
.slc
.SlcException
;
15 import org
.argeo
.slc
.deploy
.ModuleDescriptor
;
16 import org
.argeo
.slc
.execution
.ExecutionFlowDescriptor
;
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
.jcr
.SlcTypes
;
21 import org
.argeo
.slc
.runtime
.SlcAgent
;
24 * Synchronizes the local execution runtime with a JCR repository. For the time
25 * being the state is completely reset from one start to another.
27 public class JcrExecutionModulesListener
implements ExecutionModulesListener
{
28 private final static Log log
= LogFactory
29 .getLog(JcrExecutionModulesListener
.class);
31 private Session session
;
32 private SlcAgent agent
;
36 String modulesPath
= getExecutionModulesPath();
37 // clean up previous state
38 if (session
.nodeExists(modulesPath
))
39 session
.getNode(modulesPath
).remove();
40 JcrUtils
.mkdirs(session
, modulesPath
);
42 } catch (RepositoryException e
) {
43 throw new SlcException(
44 "Cannot initialize JCR execution module listener", e
);
46 JcrUtils
.discardQuietly(session
);
50 public void dispose() {
52 String modulesPath
= getExecutionModulesPath();
53 // clean up previous state
54 if (session
.nodeExists(modulesPath
))
55 session
.getNode(modulesPath
).remove();
57 } catch (RepositoryException e
) {
58 throw new SlcException(
59 "Cannot dispose JCR execution module listener", e
);
61 JcrUtils
.discardQuietly(session
);
65 public void executionModuleAdded(ModuleDescriptor moduleDescriptor
) {
67 Node base
= session
.getNode(getExecutionModulesPath());
68 Node moduleName
= base
.hasNode(moduleDescriptor
.getName()) ? base
69 .getNode(moduleDescriptor
.getName()) : base
70 .addNode(moduleDescriptor
.getName());
71 Node moduleVersion
= moduleName
.hasNode(moduleDescriptor
72 .getVersion()) ? moduleName
.getNode(moduleDescriptor
73 .getVersion()) : moduleName
.addNode(moduleDescriptor
75 moduleVersion
.addMixin(SlcTypes
.SLC_MODULE
);
76 moduleVersion
.setProperty(SlcNames
.SLC_NAME
,
77 moduleDescriptor
.getName());
78 moduleVersion
.setProperty(SlcNames
.SLC_VERSION
,
79 moduleDescriptor
.getVersion());
80 moduleVersion
.setProperty(Property
.JCR_TITLE
,
81 moduleDescriptor
.getTitle());
82 moduleVersion
.setProperty(Property
.JCR_DESCRIPTION
,
83 moduleDescriptor
.getDescription());
85 } catch (RepositoryException e
) {
86 throw new SlcException("Cannot add module " + moduleDescriptor
, e
);
91 public void executionModuleRemoved(ModuleDescriptor moduleDescriptor
) {
93 Node base
= session
.getNode(getExecutionModulesPath());
94 if (base
.hasNode(moduleDescriptor
.getName())) {
95 Node moduleName
= base
.getNode(moduleDescriptor
.getName());
96 if (moduleName
.hasNode(moduleDescriptor
.getVersion()))
97 moduleName
.getNode(moduleDescriptor
.getVersion()).remove();
98 if (!moduleName
.hasNodes())
102 } catch (RepositoryException e
) {
103 throw new SlcException("Cannot remove module " + moduleDescriptor
,
108 public void executionFlowAdded(ModuleDescriptor module
,
109 ExecutionFlowDescriptor executionFlow
) {
110 String path
= getExecutionFlowPath(module
, executionFlow
);
112 Node flowNode
= null;
113 if (!session
.nodeExists(path
)) {
114 Node base
= session
.getNode(getExecutionModulesPath());
115 Node moduleNode
= base
.getNode(module
.getName() + '/'
116 + module
.getVersion());
117 String relativePath
= getExecutionFlowRelativePath(executionFlow
);
118 Iterator
<String
> names
= Arrays
.asList(relativePath
.split("/"))
120 Node currNode
= moduleNode
;
121 while (names
.hasNext()) {
122 String name
= names
.next();
123 if (currNode
.hasNode(name
))
124 currNode
= currNode
.getNode(name
);
127 currNode
= currNode
.addNode(name
);
129 flowNode
= currNode
.addNode(name
);
132 flowNode
.addMixin(SlcTypes
.SLC_EXECUTION_FLOW
);
133 flowNode
.setProperty(SlcNames
.SLC_NAME
, executionFlow
.getName());
136 flowNode
= session
.getNode(path
);
139 if (log
.isTraceEnabled())
140 log
.trace("Flow " + executionFlow
+ " added to JCR");
141 } catch (RepositoryException e
) {
142 throw new SlcException("Cannot add flow " + executionFlow
143 + " from module " + module
, e
);
148 public void executionFlowRemoved(ModuleDescriptor module
,
149 ExecutionFlowDescriptor executionFlow
) {
150 String path
= getExecutionFlowPath(module
, executionFlow
);
152 if (session
.nodeExists(path
)) {
153 Node flowNode
= session
.getNode(path
);
157 } catch (RepositoryException e
) {
158 throw new SlcException("Cannot remove flow " + executionFlow
159 + " from module " + module
, e
);
163 protected String
getExecutionFlowPath(ModuleDescriptor module
,
164 ExecutionFlowDescriptor executionFlow
) {
165 String relativePath
= getExecutionFlowRelativePath(executionFlow
);
166 return getExecutionModulesPath() + '/' + module
.getName() + '/'
167 + module
.getVersion() + '/' + relativePath
;
170 /** @return the relative path, never starts with '/' */
171 @SuppressWarnings("deprecation")
172 protected String
getExecutionFlowRelativePath(
173 ExecutionFlowDescriptor executionFlow
) {
174 String relativePath
= executionFlow
.getPath() == null ? executionFlow
175 .getName() : executionFlow
.getPath() + '/'
176 + executionFlow
.getName();
177 // we assume that it is more than one char long
178 if (relativePath
.charAt(0) == '/')
179 relativePath
= relativePath
.substring(1);
183 protected String
getExecutionModulesPath() {
184 return SlcJcrConstants
.VM_AGENT_FACTORY_PATH
+ '/'
185 + agent
.getAgentUuid() + '/' + SlcNames
.SLC_EXECUTION_MODULES
;
188 public void setSession(Session session
) {
189 this.session
= session
;
192 public void setAgent(SlcAgent agent
) {