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() {
38 public DefaultExecutionFlow(Map
<String
, Object
> parameters
) {
39 this.parameters
.putAll(parameters
);
42 public void execute() {
43 for (Executable executable
: executables
) {
48 public void afterPropertiesSet() throws Exception
{
49 // Validate execution specs
50 if (executionSpec
== null)
53 MapBindingResult errors
= new MapBindingResult(parameters
, "execution#"
55 for (String key
: executionSpec
.getAttributes().keySet()) {
56 ExecutionSpecAttribute attr
= executionSpec
.getAttributes()
59 if (attr
.getIsParameter() && !isSetAsParameter(key
)) {
60 errors
.rejectValue(key
, "Parameter not set");
64 if (attr
.getIsFrozen() && !isSetAsParameter(key
)) {
65 errors
.rejectValue(key
, "Frozen but not set as parameter");
69 if (attr
.getIsHidden() && !isSetAsParameter(key
)) {
70 errors
.rejectValue(key
, "Hidden but not set as parameter");
75 * if (!parameters.containsKey(key)) { Object defaultValue =
76 * attr.getValue(); if (defaultValue == null)
77 * errors.rejectValue(key, "Not set and no default value"); else
78 * parameters.put(key, defaultValue); } else {// contains key Object
79 * obj = parameters.get(key); if (attr instanceof RefSpecAttribute)
80 * { RefSpecAttribute rsa = (RefSpecAttribute) attr; // TODO: make
81 * sure this will not cause pb with OSGi Class targetClass =
82 * rsa.getTargetClass(); if
83 * (!targetClass.isAssignableFrom(obj.getClass())) {
84 * errors.reject(key + " not compatible with target class " +
89 if (errors
.hasErrors())
90 throw new SlcException("Could not prepare execution flow: "
93 // if (path == null) {
94 // path = "/" + executionSpec.getName() + "/" + name;
98 for (Executable executable
: executables
) {
99 if (executable
instanceof StructureAware
) {
100 ((StructureAware
<TreeSPath
>) executable
).notifyCurrentPath(
101 registry
, new TreeSPath(path
));
107 public void setBeanName(String name
) {
111 public void setExecutables(List
<Executable
> executables
) {
112 this.executables
= executables
;
115 public void setExecutionSpec(ExecutionSpec executionSpec
) {
116 this.executionSpec
= executionSpec
;
119 public void setParameters(Map
<String
, Object
> attributes
) {
120 this.parameters
= attributes
;
123 public String
getName() {
127 public ExecutionSpec
getExecutionSpec() {
128 return executionSpec
;
131 public Object
getParameter(String name
) {
132 if (parameters
.containsKey(name
)) {
133 return parameters
.get(name
);
135 if (executionSpec
.getAttributes().containsKey(name
)) {
136 ExecutionSpecAttribute esa
= executionSpec
.getAttributes().get(
138 if (esa
.getValue() != null)
139 return esa
.getValue();
141 throw new SlcException("Key " + name
142 + " is not defined in the specifications of "
146 throw new SlcException("Key " + name
+ " is not set as parameter in "
150 public Boolean
isSetAsParameter(String key
) {
151 return parameters
.containsKey(key
)
152 || (executionSpec
.getAttributes().containsKey(key
) && executionSpec
153 .getAttributes().get(key
).getValue() != null);
156 public String
toString() {
157 return new StringBuffer("Flow ").append(name
).toString();
160 public boolean equals(Object obj
) {
161 return ((ExecutionFlow
) obj
).getName().equals(name
);
164 public String
getPath() {
168 public void setPath(String path
) {
172 public void setRegistry(StructureRegistry
<TreeSPath
> registry
) {
173 this.registry
= registry
;
176 public void notifyCurrentPath(StructureRegistry
<TreeSPath
> registry
,
178 if (this.path
== null) {
179 this.path
= path
.toString();