]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/DefaultExecutionFlow.java
Introduce JMX agent
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / execution / DefaultExecutionFlow.java
1 package org.argeo.slc.core.execution;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.argeo.slc.SlcException;
11 import org.argeo.slc.core.structure.tree.TreeSPath;
12 import org.argeo.slc.core.structure.tree.TreeSRegistry;
13 import org.argeo.slc.execution.ExecutionFlow;
14 import org.argeo.slc.execution.ExecutionSpec;
15 import org.argeo.slc.execution.ExecutionSpecAttribute;
16 import org.argeo.slc.structure.StructureAware;
17 import org.argeo.slc.structure.StructureRegistry;
18 import org.springframework.beans.factory.BeanNameAware;
19 import org.springframework.beans.factory.InitializingBean;
20 import org.springframework.validation.MapBindingResult;
21
22 public class DefaultExecutionFlow implements ExecutionFlow, InitializingBean,
23 BeanNameAware {
24 private final static Log log = LogFactory
25 .getLog(DefaultExecutionFlow.class);
26
27 private final ExecutionSpec executionSpec;
28 private String name = null;
29 private Map<String, Object> parameters = new HashMap<String, Object>();
30 private List<Runnable> executables = new ArrayList<Runnable>();
31
32 private String path;
33 private StructureRegistry<TreeSPath> registry = new TreeSRegistry();
34
35 private Boolean failOnError = true;
36
37 public DefaultExecutionFlow() {
38 this.executionSpec = new DefaultExecutionSpec();
39 }
40
41 public DefaultExecutionFlow(ExecutionSpec executionSpec) {
42 this.executionSpec = executionSpec;
43 }
44
45 public DefaultExecutionFlow(ExecutionSpec executionSpec,
46 Map<String, Object> parameters) {
47 // be sure to have an execution spec
48 this.executionSpec = (executionSpec == null) ? new DefaultExecutionSpec()
49 : executionSpec;
50
51 // only parameters contained in the executionSpec can be set
52 for (String parameter : parameters.keySet()) {
53 if (!executionSpec.getAttributes().containsKey(parameter)) {
54 throw new SlcException("Parameter " + parameter
55 + " is not defined in the ExecutionSpec");
56 }
57 }
58
59 // set the parameters
60 this.parameters.putAll(parameters);
61
62 // check that all the required parameters are defined
63 MapBindingResult errors = new MapBindingResult(parameters, "execution#"
64 + getName());
65 for (String key : executionSpec.getAttributes().keySet()) {
66 ExecutionSpecAttribute attr = executionSpec.getAttributes()
67 .get(key);
68
69 if (attr.getIsParameter() && !isSetAsParameter(key)) {
70 errors.rejectValue(key, "Parameter not set");
71 break;
72 }
73
74 if (attr.getIsFrozen() && !isSetAsParameter(key)) {
75 errors.rejectValue(key, "Frozen but not set as parameter");
76 break;
77 }
78
79 if (attr.getIsHidden() && !isSetAsParameter(key)) {
80 errors.rejectValue(key, "Hidden but not set as parameter");
81 break;
82 }
83 }
84
85 if (errors.hasErrors())
86 throw new SlcException("Could not prepare execution flow: "
87 + errors.toString());
88
89 }
90
91 public void run() {
92 try {
93 for (Runnable executable : executables) {
94 doExecuteRunnable(executable);
95 }
96 } catch (RuntimeException e) {
97 if (failOnError)
98 throw e;
99 else {
100 log.error("Execution flow failed,"
101 + " but process did not fail"
102 + " because failOnError property"
103 + " is set to false: " + e);
104 if (log.isTraceEnabled())
105 e.printStackTrace();
106 }
107 }
108 }
109
110 public void doExecuteRunnable(Runnable runnable) {
111 runnable.run();
112 }
113
114 @SuppressWarnings(value = { "unchecked" })
115 public void afterPropertiesSet() throws Exception {
116 if (path != null) {
117 for (Runnable executable : executables) {
118 if (executable instanceof StructureAware) {
119 ((StructureAware<TreeSPath>) executable).notifyCurrentPath(
120 registry, new TreeSPath(path));
121 } else if (executable instanceof DefaultExecutionFlow) {
122 // so we don't need to have DefaultExecutionFlow
123 // implementing StructureAware
124 DefaultExecutionFlow flow = (DefaultExecutionFlow) executable;
125 flow.setPath(path + '/' + flow.getName());
126 }
127 }
128 }
129 }
130
131 public void setBeanName(String name) {
132 this.name = name;
133 }
134
135 public void setExecutables(List<Runnable> executables) {
136 this.executables = executables;
137 }
138
139 public void setParameters(Map<String, Object> attributes) {
140 this.parameters = attributes;
141 }
142
143 public String getName() {
144 return name;
145 }
146
147 public ExecutionSpec getExecutionSpec() {
148 return executionSpec;
149 }
150
151 public Object getParameter(String parameterName) {
152 // Verify that there is a spec attribute
153 ExecutionSpecAttribute specAttr = null;
154 if (executionSpec.getAttributes().containsKey(parameterName)) {
155 specAttr = executionSpec.getAttributes().get(parameterName);
156 } else {
157 throw new SlcException("Key " + parameterName
158 + " is not defined in the specifications of " + toString());
159 }
160
161 if (parameters.containsKey(parameterName)) {
162 Object paramValue = parameters.get(parameterName);
163 return paramValue;
164 } else {
165 if (specAttr.getValue() != null) {
166 return specAttr.getValue();
167 }
168 }
169 throw new SlcException("Key " + parameterName
170 + " is not set as parameter in " + toString());
171 }
172
173 public Boolean isSetAsParameter(String key) {
174 return parameters.containsKey(key)
175 || (executionSpec.getAttributes().containsKey(key) && executionSpec
176 .getAttributes().get(key).getValue() != null);
177 }
178
179 @Override
180 public String toString() {
181 return new StringBuffer("Execution flow ").append(name).toString();
182 }
183
184 @Override
185 public boolean equals(Object obj) {
186 return ((ExecutionFlow) obj).getName().equals(name);
187 }
188
189 @Override
190 public int hashCode() {
191 return name.hashCode();
192 }
193
194 public String getPath() {
195 return path;
196 }
197
198 public void setPath(String path) {
199 this.path = path;
200 }
201
202 public void setRegistry(StructureRegistry<TreeSPath> registry) {
203 this.registry = registry;
204 }
205
206 public Boolean getFailOnError() {
207 return failOnError;
208 }
209
210 public void setFailOnError(Boolean failOnError) {
211 this.failOnError = failOnError;
212 }
213
214 }