1 package org
.argeo
.slc
.core
.execution
;
3 import java
.util
.ArrayList
;
4 import java
.util
.HashMap
;
9 import org
.apache
.commons
.lang
.math
.RandomUtils
;
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
.Executable
;
14 import org
.argeo
.slc
.execution
.ExecutionFlow
;
15 import org
.argeo
.slc
.execution
.ExecutionSpec
;
16 import org
.argeo
.slc
.execution
.ExecutionSpecAttribute
;
17 import org
.argeo
.slc
.structure
.StructureAware
;
18 import org
.argeo
.slc
.structure
.StructureRegistry
;
19 import org
.argeo
.slc
.test
.ExecutableTestRun
;
20 import org
.springframework
.beans
.factory
.BeanNameAware
;
21 import org
.springframework
.beans
.factory
.InitializingBean
;
22 import org
.springframework
.validation
.MapBindingResult
;
24 public class DefaultExecutionFlow
implements ExecutionFlow
, InitializingBean
,
25 BeanNameAware
, StructureAware
<TreeSPath
> {
26 private ExecutionSpec executionSpec
= new DefaultExecutionSpec();
27 private String name
= null;
28 private Map
<String
, Object
> parameters
= new HashMap
<String
, Object
>();
29 private List
<Executable
> executables
= new ArrayList
<Executable
>();
32 private StructureRegistry
<TreeSPath
> registry
= new TreeSRegistry();
34 public DefaultExecutionFlow() {
37 public DefaultExecutionFlow(ExecutionSpec executionSpec
) {
38 this.executionSpec
= executionSpec
;
41 public DefaultExecutionFlow(ExecutionSpec executionSpec
, Map
<String
, Object
> parameters
) {
42 this.parameters
.putAll(parameters
);
43 this.executionSpec
= executionSpec
;
46 public void execute() {
47 for (Executable executable
: executables
) {
52 public void afterPropertiesSet() throws Exception
{
53 // Validate execution specs
54 if (executionSpec
== null)
57 MapBindingResult errors
= new MapBindingResult(parameters
, "execution#"
59 for (String key
: executionSpec
.getAttributes().keySet()) {
60 ExecutionSpecAttribute attr
= executionSpec
.getAttributes()
63 if (attr
.getIsParameter() && !isSetAsParameter(key
)) {
64 errors
.rejectValue(key
, "Parameter not set");
68 if (attr
.getIsFrozen() && !isSetAsParameter(key
)) {
69 errors
.rejectValue(key
, "Frozen but not set as parameter");
73 if (attr
.getIsHidden() && !isSetAsParameter(key
)) {
74 errors
.rejectValue(key
, "Hidden but not set as parameter");
79 * if (!parameters.containsKey(key)) { Object defaultValue =
80 * attr.getValue(); if (defaultValue == null)
81 * errors.rejectValue(key, "Not set and no default value"); else
82 * parameters.put(key, defaultValue); } else {// contains key Object
83 * obj = parameters.get(key); if (attr instanceof RefSpecAttribute)
84 * { RefSpecAttribute rsa = (RefSpecAttribute) attr; // TODO: make
85 * sure this will not cause pb with OSGi Class targetClass =
86 * rsa.getTargetClass(); if
87 * (!targetClass.isAssignableFrom(obj.getClass())) {
88 * errors.reject(key + " not compatible with target class " +
93 if (errors
.hasErrors())
94 throw new SlcException("Could not prepare execution flow: "
97 // if (path == null) {
98 // path = "/" + executionSpec.getName() + "/" + name;
102 for (Executable executable
: executables
) {
103 if (executable
instanceof StructureAware
) {
104 ((StructureAware
<TreeSPath
>) executable
).notifyCurrentPath(
105 registry
, new TreeSPath(path
));
111 public void setBeanName(String name
) {
115 public void setExecutables(List
<Executable
> executables
) {
116 this.executables
= executables
;
119 // public void setExecutionSpec(ExecutionSpec executionSpec) {
120 // this.executionSpec = executionSpec;
123 public void setParameters(Map
<String
, Object
> attributes
) {
124 this.parameters
= attributes
;
127 public String
getName() {
131 public ExecutionSpec
getExecutionSpec() {
132 return executionSpec
;
135 public Object
getParameter(String name
) {
136 if (parameters
.containsKey(name
)) {
137 return parameters
.get(name
);
139 if (executionSpec
.getAttributes().containsKey(name
)) {
140 ExecutionSpecAttribute esa
= executionSpec
.getAttributes().get(
142 if (esa
.getValue() != null)
143 return esa
.getValue();
145 throw new SlcException("Key " + name
146 + " is not defined in the specifications of "
150 throw new SlcException("Key " + name
+ " is not set as parameter in "
154 public Boolean
isSetAsParameter(String key
) {
155 return parameters
.containsKey(key
)
156 || (executionSpec
.getAttributes().containsKey(key
) && executionSpec
157 .getAttributes().get(key
).getValue() != null);
160 public String
toString() {
161 return new StringBuffer("Flow ").append(name
).toString();
164 public boolean equals(Object obj
) {
165 return ((ExecutionFlow
) obj
).getName().equals(name
);
168 public String
getPath() {
172 public void setPath(String path
) {
176 public void setRegistry(StructureRegistry
<TreeSPath
> registry
) {
177 this.registry
= registry
;
180 public void notifyCurrentPath(StructureRegistry
<TreeSPath
> registry
,
182 if (this.path
== null) {
183 this.path
= path
.toString();