2 * Copyright (C) 2007-2012 Mathieu Baudier
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
.List
;
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
.beans
.factory
.InitializingBean
;
31 import org
.springframework
.validation
.MapBindingResult
;
33 /** Default implementation of an execution flow. */
34 public class DefaultExecutionFlow
implements ExecutionFlow
, InitializingBean
,
36 private final static Log log
= LogFactory
37 .getLog(DefaultExecutionFlow
.class);
39 private final ExecutionSpec executionSpec
;
40 private String name
= null;
41 private Map
<String
, Object
> parameters
= new HashMap
<String
, Object
>();
42 private List
<Runnable
> executables
= new ArrayList
<Runnable
>();
46 private Boolean failOnError
= true;
48 public DefaultExecutionFlow() {
49 this.executionSpec
= new DefaultExecutionSpec();
52 public DefaultExecutionFlow(ExecutionSpec executionSpec
) {
53 this.executionSpec
= executionSpec
;
56 public DefaultExecutionFlow(ExecutionSpec executionSpec
,
57 Map
<String
, Object
> parameters
) {
58 // be sure to have an execution spec
59 this.executionSpec
= (executionSpec
== null) ?
new DefaultExecutionSpec()
62 // only parameters contained in the executionSpec can be set
63 for (String parameter
: parameters
.keySet()) {
64 if (!executionSpec
.getAttributes().containsKey(parameter
)) {
65 throw new SlcException("Parameter " + parameter
66 + " is not defined in the ExecutionSpec");
71 this.parameters
.putAll(parameters
);
73 // check that all the required parameters are defined
74 MapBindingResult errors
= new MapBindingResult(parameters
, "execution#"
76 for (String key
: executionSpec
.getAttributes().keySet()) {
77 ExecutionSpecAttribute attr
= executionSpec
.getAttributes()
80 if (attr
.getIsImmutable() && !isSetAsParameter(key
)) {
81 errors
.rejectValue(key
, "Immutable but not set");
85 if (attr
.getIsConstant() && !isSetAsParameter(key
)) {
86 errors
.rejectValue(key
, "Constant but not set as parameter");
90 if (attr
.getIsHidden() && !isSetAsParameter(key
)) {
91 errors
.rejectValue(key
, "Hidden but not set as parameter");
96 if (errors
.hasErrors())
97 throw new SlcException("Could not prepare execution flow: "
104 for (Runnable executable
: executables
) {
105 if (Thread
.interrupted()) {
106 log
.error("Flow '" + getName() + "' killed before '"
108 Thread
.currentThread().interrupt();
110 // throw new ThreadDeath();
112 this.doExecuteRunnable(executable
);
114 } catch (RuntimeException e
) {
115 if (Thread
.interrupted()) {
116 log
.error("Flow '" + getName()
117 + "' killed while receiving an unrelated exception", e
);
118 Thread
.currentThread().interrupt();
120 // throw new ThreadDeath();
125 log
.error("Execution flow failed,"
126 + " but process did not fail"
127 + " because failOnError property"
128 + " is set to false: " + e
);
129 if (log
.isTraceEnabled())
135 public void doExecuteRunnable(Runnable runnable
) {
139 public void afterPropertiesSet() throws Exception
{
141 if (name
.charAt(0) == '/') {
142 path
= name
.substring(0, name
.lastIndexOf('/'));
147 for (Runnable executable
: executables
) {
148 if (executable
instanceof DefaultExecutionFlow
) {
149 // so we don't need to have DefaultExecutionFlow
150 // implementing StructureAware
151 // FIXME: probably has side effects
152 DefaultExecutionFlow flow
= (DefaultExecutionFlow
) executable
;
153 String newPath
= path
+ '/' + flow
.getName();
154 flow
.setPath(newPath
);
155 log
.warn(newPath
+ " was forcibly set on " + flow
);
161 public void setBeanName(String name
) {
165 public void setExecutables(List
<Runnable
> executables
) {
166 this.executables
= executables
;
169 public void setParameters(Map
<String
, Object
> attributes
) {
170 this.parameters
= attributes
;
173 public String
getName() {
177 public ExecutionSpec
getExecutionSpec() {
178 return executionSpec
;
181 public Object
getParameter(String parameterName
) {
182 // Verify that there is a spec attribute
183 ExecutionSpecAttribute specAttr
= null;
184 if (executionSpec
.getAttributes().containsKey(parameterName
)) {
185 specAttr
= executionSpec
.getAttributes().get(parameterName
);
187 throw new SlcException("Key " + parameterName
188 + " is not defined in the specifications of " + toString());
191 if (parameters
.containsKey(parameterName
)) {
192 Object paramValue
= parameters
.get(parameterName
);
195 if (specAttr
.getValue() != null) {
196 return specAttr
.getValue();
199 throw new SlcException("Key " + parameterName
200 + " is not set as parameter in " + toString());
203 public Boolean
isSetAsParameter(String key
) {
204 return parameters
.containsKey(key
)
205 || (executionSpec
.getAttributes().containsKey(key
) && executionSpec
206 .getAttributes().get(key
).getValue() != null);
210 public String
toString() {
211 return new StringBuffer("Execution flow ").append(name
).toString();
215 public boolean equals(Object obj
) {
216 return ((ExecutionFlow
) obj
).getName().equals(name
);
220 public int hashCode() {
221 return name
.hashCode();
224 public String
getPath() {
228 public void setPath(String path
) {
232 public Boolean
getFailOnError() {
236 public void setFailOnError(Boolean failOnError
) {
237 this.failOnError
= failOnError
;