]> 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
dd91d5dee21718bab4d558c89eee46ca94b3aa17
[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
19 /**
20 * Synchronizes the execution runtime with JCR. For the time being the state is
21 * completely reset from one start to another.
22 */
23 public class JcrExecutionModulesListener implements ExecutionModulesListener {
24 private final static Log log = LogFactory
25 .getLog(JcrExecutionModulesListener.class);
26
27 private String modulesPath = "/slc/modules";
28
29 private Session session;
30
31 public void init() {
32 try {
33 // clean up previous state
34 if (session.nodeExists(modulesPath))
35 session.getNode(modulesPath).remove();
36 JcrUtils.mkdirs(session, modulesPath);
37 session.save();
38 } catch (RepositoryException e) {
39 throw new SlcException(
40 "Cannot initialize JCR execution module listener", e);
41 } finally {
42 JcrUtils.discardQuietly(session);
43 }
44 }
45
46 public void dispose() {
47 try {
48 // clean up previous state
49 if (session.nodeExists(modulesPath))
50 session.getNode(modulesPath).remove();
51 session.save();
52 } catch (RepositoryException e) {
53 throw new SlcException(
54 "Cannot dispose JCR execution module listener", e);
55 } finally {
56 JcrUtils.discardQuietly(session);
57 }
58 }
59
60 public void executionModuleAdded(Module module,
61 ExecutionContext executionContext) {
62 try {
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
68 .getVersion());
69 session.save();
70 } catch (RepositoryException e) {
71 throw new SlcException("Cannot add module " + module, e);
72 }
73
74 }
75
76 public void executionModuleRemoved(Module module,
77 ExecutionContext executionContext) {
78 try {
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())
85 moduleName.remove();
86 session.save();
87 }
88 } catch (RepositoryException e) {
89 throw new SlcException("Cannot remove module " + module, e);
90 }
91 }
92
93 public void executionFlowAdded(Module module, ExecutionFlow executionFlow) {
94 String path = getExecutionFlowPath(module, executionFlow);
95 log.debug("path=" + path);
96 try {
97 Node flowNode;
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("/"))
105 .iterator();
106 Node currNode = moduleNode;
107 while (names.hasNext()) {
108 String name = names.next();
109 if (currNode.hasNode(name))
110 currNode = currNode.getNode(name);
111 else {
112 if (names.hasNext())
113 currNode = currNode.addNode(name);
114 else
115 flowNode = currNode.addNode(name);
116 }
117 }
118 session.save();
119 } else {
120 flowNode = session.getNode(path);
121 }
122 } catch (RepositoryException e) {
123 throw new SlcException("Cannot add flow " + executionFlow
124 + " from module " + module, e);
125 }
126
127 }
128
129 public void executionFlowRemoved(Module module, ExecutionFlow executionFlow) {
130 String path = getExecutionFlowPath(module, executionFlow);
131 try {
132 if (session.nodeExists(path)) {
133 Node flowNode = session.getNode(path);
134 flowNode.remove();
135 session.save();
136 }
137 } catch (RepositoryException e) {
138 throw new SlcException("Cannot remove flow " + executionFlow
139 + " from module " + module, e);
140 }
141 }
142
143 protected String getExecutionFlowPath(Module module,
144 ExecutionFlow executionFlow) {
145 String relativePath = getExecutionFlowRelativePath(executionFlow);
146 return modulesPath + '/' + module.getName() + '/' + module.getVersion()
147 + '/' + relativePath;
148 }
149
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);
159 return relativePath;
160 }
161
162 public void setSession(Session session) {
163 this.session = session;
164 }
165
166 }