1 package org
.argeo
.slc
.core
.execution
;
3 import java
.util
.ArrayList
;
4 import java
.util
.HashMap
;
8 import org
.argeo
.slc
.SlcException
;
9 import org
.argeo
.slc
.core
.structure
.tree
.TreeSPath
;
10 import org
.argeo
.slc
.core
.structure
.tree
.TreeSRegistry
;
11 import org
.argeo
.slc
.execution
.ExecutionFlow
;
12 import org
.argeo
.slc
.execution
.ExecutionSpec
;
13 import org
.argeo
.slc
.execution
.ExecutionSpecAttribute
;
14 import org
.argeo
.slc
.structure
.StructureAware
;
15 import org
.argeo
.slc
.structure
.StructureRegistry
;
16 import org
.springframework
.beans
.factory
.BeanNameAware
;
17 import org
.springframework
.beans
.factory
.InitializingBean
;
18 import org
.springframework
.context
.ResourceLoaderAware
;
19 import org
.springframework
.core
.io
.Resource
;
20 import org
.springframework
.core
.io
.ResourceLoader
;
21 import org
.springframework
.validation
.MapBindingResult
;
23 public class DefaultExecutionFlow
implements ExecutionFlow
, InitializingBean
,
24 BeanNameAware
, StructureAware
<TreeSPath
>, ResourceLoaderAware
{
26 private final ExecutionSpec executionSpec
;
27 private String name
= null;
28 private Map
<String
, Object
> parameters
= new HashMap
<String
, Object
>();
29 private List
<Runnable
> executables
= new ArrayList
<Runnable
>();
32 private StructureRegistry
<TreeSPath
> registry
= new TreeSRegistry();
34 private ResourceLoader resourceLoader
= null;
36 public DefaultExecutionFlow() {
37 this.executionSpec
= new DefaultExecutionSpec();
40 public DefaultExecutionFlow(ExecutionSpec executionSpec
) {
41 this.executionSpec
= executionSpec
;
44 public DefaultExecutionFlow(ExecutionSpec executionSpec
,
45 Map
<String
, Object
> parameters
) {
46 // be sure to have an execution spec
47 this.executionSpec
= (executionSpec
== null) ?
new DefaultExecutionSpec()
50 // only parameters contained in the executionSpec can be set
51 for (String parameter
: parameters
.keySet()) {
52 if (!executionSpec
.getAttributes().containsKey(parameter
)) {
53 throw new SlcException("Parameter " + parameter
54 + " is not defined in the ExecutionSpec");
59 this.parameters
.putAll(parameters
);
61 // check that all the required parameters are defined
62 MapBindingResult errors
= new MapBindingResult(parameters
, "execution#"
64 for (String key
: executionSpec
.getAttributes().keySet()) {
65 ExecutionSpecAttribute attr
= executionSpec
.getAttributes()
68 if (attr
.getIsParameter() && !isSetAsParameter(key
)) {
69 errors
.rejectValue(key
, "Parameter not set");
73 if (attr
.getIsFrozen() && !isSetAsParameter(key
)) {
74 errors
.rejectValue(key
, "Frozen but not set as parameter");
78 if (attr
.getIsHidden() && !isSetAsParameter(key
)) {
79 errors
.rejectValue(key
, "Hidden but not set as parameter");
84 if (errors
.hasErrors())
85 throw new SlcException("Could not prepare execution flow: "
91 for (Runnable executable
: executables
) {
96 public void afterPropertiesSet() throws Exception
{
98 for (Runnable 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
<Runnable
> executables
) {
112 this.executables
= executables
;
115 public void setParameters(Map
<String
, Object
> attributes
) {
116 this.parameters
= attributes
;
119 public String
getName() {
123 public ExecutionSpec
getExecutionSpec() {
124 return executionSpec
;
127 public Object
getParameter(String parameterName
) {
128 // Verify that there is a spec attribute
129 ExecutionSpecAttribute specAttr
= null;
130 if (executionSpec
.getAttributes().containsKey(parameterName
)) {
131 specAttr
= executionSpec
.getAttributes().get(parameterName
);
133 throw new SlcException("Key " + parameterName
134 + " is not defined in the specifications of " + toString());
137 if (parameters
.containsKey(parameterName
)) {
138 Object paramValue
= parameters
.get(parameterName
);
139 if (specAttr
instanceof ResourceSpecAttribute
) {
140 // deal with resources
141 Resource resource
= resourceLoader
.getResource(paramValue
143 return ((ResourceSpecAttribute
) specAttr
)
144 .convertResource(resource
);
149 if (specAttr
.getValue() != null) {
150 return specAttr
.getValue();
153 throw new SlcException("Key " + parameterName
154 + " is not set as parameter in " + toString());
157 public Boolean
isSetAsParameter(String key
) {
158 return parameters
.containsKey(key
)
159 || (executionSpec
.getAttributes().containsKey(key
) && executionSpec
160 .getAttributes().get(key
).getValue() != null);
163 public String
toString() {
164 return new StringBuffer("Flow ").append(name
).toString();
167 public boolean equals(Object obj
) {
168 return ((ExecutionFlow
) obj
).getName().equals(name
);
171 public String
getPath() {
175 public void setPath(String path
) {
179 public void setRegistry(StructureRegistry
<TreeSPath
> registry
) {
180 this.registry
= registry
;
183 public void notifyCurrentPath(StructureRegistry
<TreeSPath
> registry
,
185 if (this.path
== null) {
186 this.path
= path
.toString();
190 public void setResourceLoader(ResourceLoader resourceLoader
) {
191 this.resourceLoader
= resourceLoader
;