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
.context
.ResourceLoaderAware
;
21 import org
.springframework
.core
.io
.Resource
;
22 import org
.springframework
.core
.io
.ResourceLoader
;
23 import org
.springframework
.validation
.MapBindingResult
;
25 public class DefaultExecutionFlow
implements ExecutionFlow
, InitializingBean
,
26 BeanNameAware
, StructureAware
<TreeSPath
>, ResourceLoaderAware
{
28 private final static Log log
= LogFactory
29 .getLog(DefaultExecutionFlow
.class);
31 private final ExecutionSpec executionSpec
;
32 private String name
= null;
33 private Map
<String
, Object
> parameters
= new HashMap
<String
, Object
>();
34 private List
<Runnable
> executables
= new ArrayList
<Runnable
>();
37 private StructureRegistry
<TreeSPath
> registry
= new TreeSRegistry();
39 private ResourceLoader resourceLoader
= null;
41 public DefaultExecutionFlow() {
42 this.executionSpec
= new DefaultExecutionSpec();
45 public DefaultExecutionFlow(ExecutionSpec executionSpec
) {
46 this.executionSpec
= executionSpec
;
49 public DefaultExecutionFlow(ExecutionSpec executionSpec
,
50 Map
<String
, Object
> parameters
) {
51 // be sure to have an execution spec
52 this.executionSpec
= (executionSpec
== null) ?
new DefaultExecutionSpec()
55 // only parameters contained in the executionSpec can be set
56 for (String parameter
: parameters
.keySet()) {
57 if (!executionSpec
.getAttributes().containsKey(parameter
)) {
58 throw new SlcException("Parameter " + parameter
59 + " is not defined in the ExecutionSpec");
64 this.parameters
.putAll(parameters
);
66 // check that all the required parameters are defined
67 MapBindingResult errors
= new MapBindingResult(parameters
, "execution#"
69 for (String key
: executionSpec
.getAttributes().keySet()) {
70 ExecutionSpecAttribute attr
= executionSpec
.getAttributes()
73 if (attr
.getIsParameter() && !isSetAsParameter(key
)) {
74 errors
.rejectValue(key
, "Parameter not set");
78 if (attr
.getIsFrozen() && !isSetAsParameter(key
)) {
79 errors
.rejectValue(key
, "Frozen but not set as parameter");
83 if (attr
.getIsHidden() && !isSetAsParameter(key
)) {
84 errors
.rejectValue(key
, "Hidden but not set as parameter");
89 if (errors
.hasErrors())
90 throw new SlcException("Could not prepare execution flow: "
96 for (Runnable executable
: executables
) {
101 public void afterPropertiesSet() throws Exception
{
103 for (Runnable executable
: executables
) {
104 if (executable
instanceof StructureAware
) {
105 ((StructureAware
<TreeSPath
>) executable
).notifyCurrentPath(
106 registry
, new TreeSPath(path
));
112 public void setBeanName(String name
) {
116 public void setExecutables(List
<Runnable
> executables
) {
117 this.executables
= executables
;
120 public void setParameters(Map
<String
, Object
> attributes
) {
121 this.parameters
= attributes
;
124 public String
getName() {
128 public ExecutionSpec
getExecutionSpec() {
129 return executionSpec
;
132 public Object
getParameter(String parameterName
) {
133 // Verify that there is a spec attribute
134 ExecutionSpecAttribute specAttr
= null;
135 if (executionSpec
.getAttributes().containsKey(parameterName
)) {
136 specAttr
= executionSpec
.getAttributes().get(parameterName
);
138 throw new SlcException("Key " + parameterName
139 + " is not defined in the specifications of " + toString());
142 if (parameters
.containsKey(parameterName
)) {
143 Object paramValue
= parameters
.get(parameterName
);
144 if (specAttr
instanceof ResourceSpecAttribute
) {
145 // deal with resources
146 Resource resource
= resourceLoader
.getResource(paramValue
148 return ((ResourceSpecAttribute
) specAttr
)
149 .convertResource(resource
);
154 if (specAttr
.getValue() != null) {
155 return specAttr
.getValue();
158 throw new SlcException("Key " + parameterName
159 + " is not set as parameter in " + toString());
162 public Boolean
isSetAsParameter(String key
) {
163 return parameters
.containsKey(key
)
164 || (executionSpec
.getAttributes().containsKey(key
) && executionSpec
165 .getAttributes().get(key
).getValue() != null);
168 public String
toString() {
169 return new StringBuffer("Flow ").append(name
).toString();
172 public boolean equals(Object obj
) {
173 return ((ExecutionFlow
) obj
).getName().equals(name
);
176 public String
getPath() {
180 public void setPath(String path
) {
184 public void setRegistry(StructureRegistry
<TreeSPath
> registry
) {
185 this.registry
= registry
;
188 public void notifyCurrentPath(StructureRegistry
<TreeSPath
> registry
,
190 if (this.path
== null) {
191 this.path
= path
.toString();
195 public void setResourceLoader(ResourceLoader resourceLoader
) {
196 this.resourceLoader
= resourceLoader
;