2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.core
.execution
;
18 import java
.util
.ArrayList
;
19 import java
.util
.HashMap
;
20 import java
.util
.Iterator
;
21 import java
.util
.List
;
24 import org
.apache
.commons
.logging
.Log
;
25 import org
.apache
.commons
.logging
.LogFactory
;
26 import org
.argeo
.slc
.SlcException
;
27 import org
.argeo
.slc
.execution
.ExecutionFlow
;
28 import org
.argeo
.slc
.execution
.ExecutionSpec
;
29 import org
.argeo
.slc
.execution
.ExecutionSpecAttribute
;
30 import org
.springframework
.beans
.factory
.BeanNameAware
;
31 import org
.springframework
.beans
.factory
.InitializingBean
;
32 import org
.springframework
.validation
.MapBindingResult
;
34 /** Default implementation of an execution flow. */
35 public class DefaultExecutionFlow
implements ExecutionFlow
, InitializingBean
,
37 private final static Log log
= LogFactory
38 .getLog(DefaultExecutionFlow
.class);
40 private final ExecutionSpec executionSpec
;
41 private String name
= null;
42 private Map
<String
, Object
> parameters
= new HashMap
<String
, Object
>();
43 private List
<Runnable
> executables
= new ArrayList
<Runnable
>();
47 private Boolean failOnError
= true;
49 public DefaultExecutionFlow() {
50 this.executionSpec
= new DefaultExecutionSpec();
53 public DefaultExecutionFlow(ExecutionSpec executionSpec
) {
54 this.executionSpec
= executionSpec
;
57 public DefaultExecutionFlow(ExecutionSpec executionSpec
,
58 Map
<String
, Object
> parameters
) {
59 // be sure to have an execution spec
60 this.executionSpec
= (executionSpec
== null) ?
new DefaultExecutionSpec()
63 // only parameters contained in the executionSpec can be set
64 for (String parameter
: parameters
.keySet()) {
65 if (!executionSpec
.getAttributes().containsKey(parameter
)) {
66 throw new SlcException("Parameter " + parameter
67 + " is not defined in the ExecutionSpec");
72 this.parameters
.putAll(parameters
);
74 // check that all the required parameters are defined
75 MapBindingResult errors
= new MapBindingResult(parameters
, "execution#"
77 for (String key
: executionSpec
.getAttributes().keySet()) {
78 ExecutionSpecAttribute attr
= executionSpec
.getAttributes()
81 if (attr
.getIsImmutable() && !isSetAsParameter(key
)) {
82 errors
.rejectValue(key
, "Immutable but not set");
86 if (attr
.getIsConstant() && !isSetAsParameter(key
)) {
87 errors
.rejectValue(key
, "Constant but not set as parameter");
91 if (attr
.getIsHidden() && !isSetAsParameter(key
)) {
92 errors
.rejectValue(key
, "Hidden but not set as parameter");
97 if (errors
.hasErrors())
98 throw new SlcException("Could not prepare execution flow: "
105 for (Runnable executable
: executables
) {
106 if (Thread
.interrupted()) {
107 log
.error("Flow '" + getName() + "' killed before '"
109 Thread
.currentThread().interrupt();
111 // throw new ThreadDeath();
113 this.doExecuteRunnable(executable
);
115 } catch (RuntimeException e
) {
116 if (Thread
.interrupted()) {
117 log
.error("Flow '" + getName()
118 + "' killed while receiving an unrelated exception", e
);
119 Thread
.currentThread().interrupt();
121 // throw new ThreadDeath();
126 log
.error("Execution flow failed,"
127 + " but process did not fail"
128 + " because failOnError property"
129 + " is set to false: " + e
);
130 if (log
.isTraceEnabled())
137 * List sub-runnables that would be executed if run() method would be
140 public Iterator
<Runnable
> runnables() {
141 return executables
.iterator();
145 * If there is one and only one runnable wrapped return it, throw an
146 * exeception otherwise.
148 public Runnable
getRunnable() {
149 if (executables
.size() == 1)
150 return executables
.get(0);
152 throw new SlcException("There are " + executables
.size()
153 + " runnables in flow " + getName());
156 public void doExecuteRunnable(Runnable runnable
) {
160 public void afterPropertiesSet() throws Exception
{
162 if (name
.charAt(0) == '/') {
163 path
= name
.substring(0, name
.lastIndexOf('/'));
168 for (Runnable executable
: executables
) {
169 if (executable
instanceof DefaultExecutionFlow
) {
170 // so we don't need to have DefaultExecutionFlow
171 // implementing StructureAware
172 // FIXME: probably has side effects
173 DefaultExecutionFlow flow
= (DefaultExecutionFlow
) executable
;
174 String newPath
= path
+ '/' + flow
.getName();
175 flow
.setPath(newPath
);
176 log
.warn(newPath
+ " was forcibly set on " + flow
);
182 public void setBeanName(String name
) {
186 public void setExecutables(List
<Runnable
> executables
) {
187 this.executables
= executables
;
190 public void setParameters(Map
<String
, Object
> attributes
) {
191 this.parameters
= attributes
;
194 public String
getName() {
198 public ExecutionSpec
getExecutionSpec() {
199 return executionSpec
;
202 public Object
getParameter(String parameterName
) {
203 // Verify that there is a spec attribute
204 ExecutionSpecAttribute specAttr
= null;
205 if (executionSpec
.getAttributes().containsKey(parameterName
)) {
206 specAttr
= executionSpec
.getAttributes().get(parameterName
);
208 throw new SlcException("Key " + parameterName
209 + " is not defined in the specifications of " + toString());
212 if (parameters
.containsKey(parameterName
)) {
213 Object paramValue
= parameters
.get(parameterName
);
216 if (specAttr
.getValue() != null) {
217 return specAttr
.getValue();
220 throw new SlcException("Key " + parameterName
221 + " is not set as parameter in " + toString());
224 public Boolean
isSetAsParameter(String key
) {
225 return parameters
.containsKey(key
)
226 || (executionSpec
.getAttributes().containsKey(key
) && executionSpec
227 .getAttributes().get(key
).getValue() != null);
231 public String
toString() {
232 return new StringBuffer("Execution flow ").append(name
).toString();
236 public boolean equals(Object obj
) {
237 return ((ExecutionFlow
) obj
).getName().equals(name
);
241 public int hashCode() {
242 return name
.hashCode();
245 public String
getPath() {
249 public void setPath(String path
) {
253 public Boolean
getFailOnError() {
257 public void setFailOnError(Boolean failOnError
) {
258 this.failOnError
= failOnError
;