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
;
20 * Synchronizes the execution runtime with JCR. For the time being the state is
21 * completely reset from one start to another.
23 public class JcrExecutionModulesListener
implements ExecutionModulesListener
{
24 private final static Log log
= LogFactory
25 .getLog(JcrExecutionModulesListener
.class);
27 private String modulesPath
= "/slc/modules";
29 private Session session
;
33 // clean up previous state
34 if (session
.nodeExists(modulesPath
))
35 session
.getNode(modulesPath
).remove();
36 JcrUtils
.mkdirs(session
, modulesPath
);
38 } catch (RepositoryException e
) {
39 throw new SlcException(
40 "Cannot initialize JCR execution module listener", e
);
42 JcrUtils
.discardQuietly(session
);
46 public void dispose() {
48 // clean up previous state
49 if (session
.nodeExists(modulesPath
))
50 session
.getNode(modulesPath
).remove();
52 } catch (RepositoryException e
) {
53 throw new SlcException(
54 "Cannot dispose JCR execution module listener", e
);
56 JcrUtils
.discardQuietly(session
);
60 public void executionModuleAdded(Module module
,
61 ExecutionContext executionContext
) {
63 Node base
= session
.getNode(modulesPath
);
64 Node moduleName
= base
.hasNode(module
.getName()) ? base
65 .getNode(module
.getName()) : base
.addNode(module
.getName());
66 Node moduleVersion
= moduleName
.hasNode(module
.getVersion()) ? moduleName
67 .getNode(module
.getVersion()) : moduleName
.addNode(module
70 } catch (RepositoryException e
) {
71 throw new SlcException("Cannot add module " + module
, e
);
76 public void executionModuleRemoved(Module module
,
77 ExecutionContext executionContext
) {
79 Node base
= session
.getNode(modulesPath
);
80 if (base
.hasNode(module
.getName())) {
81 Node moduleName
= base
.getNode(module
.getName());
82 if (moduleName
.hasNode(module
.getVersion()))
83 moduleName
.getNode(module
.getVersion()).remove();
84 if (!moduleName
.hasNodes())
88 } catch (RepositoryException e
) {
89 throw new SlcException("Cannot remove module " + module
, e
);
93 public void executionFlowAdded(Module module
, ExecutionFlow executionFlow
) {
94 String path
= getExecutionFlowPath(module
, executionFlow
);
95 log
.debug("path=" + path
);
98 if (!session
.nodeExists(path
)) {
99 Node base
= session
.getNode(modulesPath
);
100 Node moduleNode
= base
.getNode(module
.getName() + '/'
101 + module
.getVersion());
102 String relativePath
= getExecutionFlowRelativePath(executionFlow
);
103 log
.debug("relativePath='" + relativePath
+ "'");
104 Iterator
<String
> names
= Arrays
.asList(relativePath
.split("/"))
106 Node currNode
= moduleNode
;
107 while (names
.hasNext()) {
108 String name
= names
.next();
109 if (currNode
.hasNode(name
))
110 currNode
= currNode
.getNode(name
);
113 currNode
= currNode
.addNode(name
);
115 flowNode
= currNode
.addNode(name
);
120 flowNode
= session
.getNode(path
);
122 } catch (RepositoryException e
) {
123 throw new SlcException("Cannot add flow " + executionFlow
124 + " from module " + module
, e
);
129 public void executionFlowRemoved(Module module
, ExecutionFlow executionFlow
) {
130 String path
= getExecutionFlowPath(module
, executionFlow
);
132 if (session
.nodeExists(path
)) {
133 Node flowNode
= session
.getNode(path
);
137 } catch (RepositoryException e
) {
138 throw new SlcException("Cannot remove flow " + executionFlow
139 + " from module " + module
, e
);
143 protected String
getExecutionFlowPath(Module module
,
144 ExecutionFlow executionFlow
) {
145 String relativePath
= getExecutionFlowRelativePath(executionFlow
);
146 return modulesPath
+ '/' + module
.getName() + '/' + module
.getVersion()
147 + '/' + relativePath
;
150 /** @return the relative path, never starts with '/' */
151 @SuppressWarnings("deprecation")
152 protected String
getExecutionFlowRelativePath(ExecutionFlow executionFlow
) {
153 String relativePath
= executionFlow
.getPath() == null ? executionFlow
154 .getName() : executionFlow
.getPath() + '/'
155 + executionFlow
.getName();
156 // we assume that it is more than one char long
157 if (relativePath
.charAt(0) == '/')
158 relativePath
= relativePath
.substring(1);
162 public void setSession(Session session
) {
163 this.session
= session
;