1 package org
.argeo
.slc
.core
.execution
;
3 import java
.util
.ArrayList
;
4 import java
.util
.HashMap
;
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
;
22 public class DefaultExecutionFlow
implements ExecutionFlow
, InitializingBean
,
24 private final static Log log
= LogFactory
25 .getLog(DefaultExecutionFlow
.class);
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
>();
33 private StructureRegistry
<TreeSPath
> registry
= new TreeSRegistry();
35 private Boolean failOnError
= true;
37 public DefaultExecutionFlow() {
38 this.executionSpec
= new DefaultExecutionSpec();
41 public DefaultExecutionFlow(ExecutionSpec executionSpec
) {
42 this.executionSpec
= executionSpec
;
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()
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");
60 this.parameters
.putAll(parameters
);
62 // check that all the required parameters are defined
63 MapBindingResult errors
= new MapBindingResult(parameters
, "execution#"
65 for (String key
: executionSpec
.getAttributes().keySet()) {
66 ExecutionSpecAttribute attr
= executionSpec
.getAttributes()
69 if (attr
.getIsParameter() && !isSetAsParameter(key
)) {
70 errors
.rejectValue(key
, "Parameter not set");
74 if (attr
.getIsFrozen() && !isSetAsParameter(key
)) {
75 errors
.rejectValue(key
, "Frozen but not set as parameter");
79 if (attr
.getIsHidden() && !isSetAsParameter(key
)) {
80 errors
.rejectValue(key
, "Hidden but not set as parameter");
85 if (errors
.hasErrors())
86 throw new SlcException("Could not prepare execution flow: "
93 for (Runnable executable
: executables
) {
96 } catch (RuntimeException e
) {
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())
110 @SuppressWarnings(value
= { "unchecked" })
111 public void afterPropertiesSet() throws Exception
{
113 for (Runnable executable
: executables
) {
114 if (executable
instanceof StructureAware
) {
115 ((StructureAware
<TreeSPath
>) executable
).notifyCurrentPath(
116 registry
, new TreeSPath(path
));
117 } else if (executable
instanceof DefaultExecutionFlow
) {
118 // so we don't need to have DefaultExecutionFlow
119 // implementing StructureAware
120 DefaultExecutionFlow flow
= (DefaultExecutionFlow
) executable
;
121 flow
.setPath(path
+ '/' + flow
.getName());
127 public void setBeanName(String name
) {
131 public void setExecutables(List
<Runnable
> executables
) {
132 this.executables
= executables
;
135 public void setParameters(Map
<String
, Object
> attributes
) {
136 this.parameters
= attributes
;
139 public String
getName() {
143 public ExecutionSpec
getExecutionSpec() {
144 return executionSpec
;
147 public Object
getParameter(String parameterName
) {
148 // Verify that there is a spec attribute
149 ExecutionSpecAttribute specAttr
= null;
150 if (executionSpec
.getAttributes().containsKey(parameterName
)) {
151 specAttr
= executionSpec
.getAttributes().get(parameterName
);
153 throw new SlcException("Key " + parameterName
154 + " is not defined in the specifications of " + toString());
157 if (parameters
.containsKey(parameterName
)) {
158 Object paramValue
= parameters
.get(parameterName
);
161 if (specAttr
.getValue() != null) {
162 return specAttr
.getValue();
165 throw new SlcException("Key " + parameterName
166 + " is not set as parameter in " + toString());
169 public Boolean
isSetAsParameter(String key
) {
170 return parameters
.containsKey(key
)
171 || (executionSpec
.getAttributes().containsKey(key
) && executionSpec
172 .getAttributes().get(key
).getValue() != null);
176 public String
toString() {
177 return new StringBuffer("Execution flow ").append(name
).toString();
181 public boolean equals(Object obj
) {
182 return ((ExecutionFlow
) obj
).getName().equals(name
);
186 public int hashCode() {
187 return name
.hashCode();
190 public String
getPath() {
194 public void setPath(String path
) {
198 public void setRegistry(StructureRegistry
<TreeSPath
> registry
) {
199 this.registry
= registry
;
202 public Boolean
getFailOnError() {
206 public void setFailOnError(Boolean failOnError
) {
207 this.failOnError
= failOnError
;