]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/execution/JcrExecutionModulesListener.java
Introduce JCR agent. Client agent not needed anymore.
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.jcr / src / main / java / org / argeo / slc / jcr / execution / JcrExecutionModulesListener.java
1 package org.argeo.slc.jcr.execution;
2
3 import java.util.Arrays;
4 import java.util.Iterator;
5
6 import javax.jcr.Node;
7 import javax.jcr.RepositoryException;
8 import javax.jcr.Session;
9
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;
21
22 /**
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.
25 */
26 public class JcrExecutionModulesListener implements ExecutionModulesListener {
27 private final static Log log = LogFactory
28 .getLog(JcrExecutionModulesListener.class);
29
30 private Session session;
31 private SlcAgent agent;
32
33 public void init() {
34 try {
35 String modulesPath = getExecutionModulesPath();
36 // clean up previous state
37 if (session.nodeExists(modulesPath))
38 session.getNode(modulesPath).remove();
39 JcrUtils.mkdirs(session, modulesPath);
40 session.save();
41 } catch (RepositoryException e) {
42 throw new SlcException(
43 "Cannot initialize JCR execution module listener", e);
44 } finally {
45 JcrUtils.discardQuietly(session);
46 }
47 }
48
49 public void dispose() {
50 try {
51 String modulesPath = getExecutionModulesPath();
52 // clean up previous state
53 if (session.nodeExists(modulesPath))
54 session.getNode(modulesPath).remove();
55 session.save();
56 } catch (RepositoryException e) {
57 throw new SlcException(
58 "Cannot dispose JCR execution module listener", e);
59 } finally {
60 JcrUtils.discardQuietly(session);
61 }
62 }
63
64 public void executionModuleAdded(Module module,
65 ExecutionContext executionContext) {
66 try {
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
72 .getVersion());
73 session.save();
74 } catch (RepositoryException e) {
75 throw new SlcException("Cannot add module " + module, e);
76 }
77
78 }
79
80 public void executionModuleRemoved(Module module,
81 ExecutionContext executionContext) {
82 try {
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())
89 moduleName.remove();
90 session.save();
91 }
92 } catch (RepositoryException e) {
93 throw new SlcException("Cannot remove module " + module, e);
94 }
95 }
96
97 public void executionFlowAdded(Module module, ExecutionFlow executionFlow) {
98 String path = getExecutionFlowPath(module, executionFlow);
99 log.debug("path=" + path);
100 try {
101 Node flowNode;
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("/"))
109 .iterator();
110 Node currNode = moduleNode;
111 while (names.hasNext()) {
112 String name = names.next();
113 if (currNode.hasNode(name))
114 currNode = currNode.getNode(name);
115 else {
116 if (names.hasNext())
117 currNode = currNode.addNode(name);
118 else
119 flowNode = currNode.addNode(name);
120 }
121 }
122 session.save();
123 } else {
124 flowNode = session.getNode(path);
125 }
126 } catch (RepositoryException e) {
127 throw new SlcException("Cannot add flow " + executionFlow
128 + " from module " + module, e);
129 }
130
131 }
132
133 public void executionFlowRemoved(Module module, ExecutionFlow executionFlow) {
134 String path = getExecutionFlowPath(module, executionFlow);
135 try {
136 if (session.nodeExists(path)) {
137 Node flowNode = session.getNode(path);
138 flowNode.remove();
139 session.save();
140 }
141 } catch (RepositoryException e) {
142 throw new SlcException("Cannot remove flow " + executionFlow
143 + " from module " + module, e);
144 }
145 }
146
147 protected String getExecutionFlowPath(Module module,
148 ExecutionFlow executionFlow) {
149 String relativePath = getExecutionFlowRelativePath(executionFlow);
150 return getExecutionModulesPath() + '/' + module.getName() + '/'
151 + module.getVersion() + '/' + relativePath;
152 }
153
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);
163 return relativePath;
164 }
165
166 protected String getExecutionModulesPath() {
167 return SlcJcrConstants.VM_AGENT_FACTORY_PATH + '/'
168 + agent.getAgentUuid() + '/' + SlcNames.SLC_EXECUTION_MODULES;
169 }
170
171 public void setSession(Session session) {
172 this.session = session;
173 }
174
175 public void setAgent(SlcAgent agent) {
176 this.agent = agent;
177 }
178
179 }