]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/DefaultExecutionFlow.java
Remove usage of flow path in ExecutionFlowGenerator
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / execution / DefaultExecutionFlow.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.core.execution;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.argeo.slc.SlcException;
26 import org.argeo.slc.execution.ExecutionFlow;
27 import org.argeo.slc.execution.ExecutionSpec;
28 import org.argeo.slc.execution.ExecutionSpecAttribute;
29 import org.springframework.beans.factory.BeanNameAware;
30 import org.springframework.validation.MapBindingResult;
31
32 /** Default implementation of an execution flow. */
33 public class DefaultExecutionFlow implements ExecutionFlow, BeanNameAware {
34 private final static Log log = LogFactory
35 .getLog(DefaultExecutionFlow.class);
36
37 private final ExecutionSpec executionSpec;
38 private String name = null;
39 private Map<String, Object> parameters = new HashMap<String, Object>();
40 private List<Runnable> executables = new ArrayList<Runnable>();
41
42 private Boolean failOnError = true;
43
44 public DefaultExecutionFlow() {
45 this.executionSpec = new DefaultExecutionSpec();
46 }
47
48 public DefaultExecutionFlow(ExecutionSpec executionSpec) {
49 this.executionSpec = executionSpec;
50 }
51
52 public DefaultExecutionFlow(ExecutionSpec executionSpec,
53 Map<String, Object> parameters) {
54 // be sure to have an execution spec
55 this.executionSpec = (executionSpec == null) ? new DefaultExecutionSpec()
56 : executionSpec;
57
58 // only parameters contained in the executionSpec can be set
59 for (String parameter : parameters.keySet()) {
60 if (!executionSpec.getAttributes().containsKey(parameter)) {
61 throw new SlcException("Parameter " + parameter
62 + " is not defined in the ExecutionSpec");
63 }
64 }
65
66 // set the parameters
67 this.parameters.putAll(parameters);
68
69 // check that all the required parameters are defined
70 MapBindingResult errors = new MapBindingResult(parameters, "execution#"
71 + getName());
72 for (String key : executionSpec.getAttributes().keySet()) {
73 ExecutionSpecAttribute attr = executionSpec.getAttributes()
74 .get(key);
75
76 if (attr.getIsImmutable() && !isSetAsParameter(key)) {
77 errors.rejectValue(key, "Immutable but not set");
78 break;
79 }
80
81 if (attr.getIsConstant() && !isSetAsParameter(key)) {
82 errors.rejectValue(key, "Constant but not set as parameter");
83 break;
84 }
85
86 if (attr.getIsHidden() && !isSetAsParameter(key)) {
87 errors.rejectValue(key, "Hidden but not set as parameter");
88 break;
89 }
90 }
91
92 if (errors.hasErrors())
93 throw new SlcException("Could not prepare execution flow: "
94 + errors.toString());
95
96 }
97
98 public void run() {
99 try {
100 for (Runnable executable : executables) {
101 if (Thread.interrupted()) {
102 log.error("Flow '" + getName() + "' killed before '"
103 + executable + "'");
104 Thread.currentThread().interrupt();
105 return;
106 // throw new ThreadDeath();
107 }
108 this.doExecuteRunnable(executable);
109 }
110 } catch (RuntimeException e) {
111 if (Thread.interrupted()) {
112 log.error("Flow '" + getName()
113 + "' killed while receiving an unrelated exception", e);
114 Thread.currentThread().interrupt();
115 return;
116 // throw new ThreadDeath();
117 }
118 if (failOnError)
119 throw e;
120 else {
121 log.error("Execution flow failed,"
122 + " but process did not fail"
123 + " because failOnError property"
124 + " is set to false: " + e);
125 if (log.isTraceEnabled())
126 e.printStackTrace();
127 }
128 }
129 }
130
131 public void doExecuteRunnable(Runnable runnable) {
132 runnable.run();
133 }
134
135 public void setBeanName(String name) {
136 this.name = name;
137 }
138
139 public void setExecutables(List<Runnable> executables) {
140 this.executables = executables;
141 }
142
143 public void setParameters(Map<String, Object> attributes) {
144 this.parameters = attributes;
145 }
146
147 public String getName() {
148 return name;
149 }
150
151 public ExecutionSpec getExecutionSpec() {
152 return executionSpec;
153 }
154
155 public Object getParameter(String parameterName) {
156 // Verify that there is a spec attribute
157 ExecutionSpecAttribute specAttr = null;
158 if (executionSpec.getAttributes().containsKey(parameterName)) {
159 specAttr = executionSpec.getAttributes().get(parameterName);
160 } else {
161 throw new SlcException("Key " + parameterName
162 + " is not defined in the specifications of " + toString());
163 }
164
165 if (parameters.containsKey(parameterName)) {
166 Object paramValue = parameters.get(parameterName);
167 return paramValue;
168 } else {
169 if (specAttr.getValue() != null) {
170 return specAttr.getValue();
171 }
172 }
173 throw new SlcException("Key " + parameterName
174 + " is not set as parameter in " + toString());
175 }
176
177 public Boolean isSetAsParameter(String key) {
178 return parameters.containsKey(key)
179 || (executionSpec.getAttributes().containsKey(key) && executionSpec
180 .getAttributes().get(key).getValue() != null);
181 }
182
183 @Override
184 public String toString() {
185 return new StringBuffer("Execution flow ").append(name).toString();
186 }
187
188 @Override
189 public boolean equals(Object obj) {
190 return ((ExecutionFlow) obj).getName().equals(name);
191 }
192
193 @Override
194 public int hashCode() {
195 return name.hashCode();
196 }
197
198 /** @deprecated does nothing */
199 @Deprecated
200 public void setPath(String path) {
201 }
202
203 public Boolean getFailOnError() {
204 return failOnError;
205 }
206
207 public void setFailOnError(Boolean failOnError) {
208 this.failOnError = failOnError;
209 }
210
211 }