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
.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
.springframework
.beans
.factory
.BeanNameAware
;
20 import org
.springframework
.beans
.factory
.InitializingBean
;
21 import org
.springframework
.context
.ResourceLoaderAware
;
22 import org
.springframework
.core
.io
.Resource
;
23 import org
.springframework
.core
.io
.ResourceLoader
;
24 import org
.springframework
.validation
.MapBindingResult
;
26 public class DefaultExecutionFlow
implements ExecutionFlow
, InitializingBean
,
27 BeanNameAware
, StructureAware
<TreeSPath
>, ResourceLoaderAware
{
29 private final static Log log
= LogFactory
30 .getLog(DefaultExecutionFlow
.class);
32 private final ExecutionSpec executionSpec
;
33 private String name
= null;
34 private Map
<String
, Object
> parameters
= new HashMap
<String
, Object
>();
35 private List
<Executable
> executables
= new ArrayList
<Executable
>();
38 private StructureRegistry
<TreeSPath
> registry
= new TreeSRegistry();
40 private ResourceLoader resourceLoader
= null;
42 public DefaultExecutionFlow() {
43 this.executionSpec
= new DefaultExecutionSpec();
46 public DefaultExecutionFlow(ExecutionSpec executionSpec
) {
47 this.executionSpec
= executionSpec
;
50 public DefaultExecutionFlow(ExecutionSpec executionSpec
,
51 Map
<String
, Object
> parameters
) {
52 // be sure to have an execution spec
53 this.executionSpec
= (executionSpec
== null) ?
new DefaultExecutionSpec()
56 // only parameters contained in the executionSpec can be set
57 for (String parameter
: parameters
.keySet()) {
58 if (!executionSpec
.getAttributes().containsKey(parameter
)) {
59 throw new SlcException("Parameter " + parameter
60 + " is not defined in the ExecutionSpec");
65 this.parameters
.putAll(parameters
);
67 // check that all the required parameters are defined
68 MapBindingResult errors
= new MapBindingResult(parameters
, "execution#"
70 for (String key
: executionSpec
.getAttributes().keySet()) {
71 ExecutionSpecAttribute attr
= executionSpec
.getAttributes()
74 if (attr
.getIsParameter() && !isSetAsParameter(key
)) {
75 errors
.rejectValue(key
, "Parameter not set");
79 if (attr
.getIsFrozen() && !isSetAsParameter(key
)) {
80 errors
.rejectValue(key
, "Frozen but not set as parameter");
84 if (attr
.getIsHidden() && !isSetAsParameter(key
)) {
85 errors
.rejectValue(key
, "Hidden but not set as parameter");
90 if (errors
.hasErrors())
91 throw new SlcException("Could not prepare execution flow: "
96 public void execute() {
97 for (Executable executable
: executables
) {
102 public void afterPropertiesSet() throws Exception
{
104 for (Executable executable
: executables
) {
105 if (executable
instanceof StructureAware
) {
106 ((StructureAware
<TreeSPath
>) executable
).notifyCurrentPath(
107 registry
, new TreeSPath(path
));
113 public void setBeanName(String name
) {
117 public void setExecutables(List
<Executable
> executables
) {
118 this.executables
= executables
;
121 public void setParameters(Map
<String
, Object
> attributes
) {
122 this.parameters
= attributes
;
125 public String
getName() {
129 public ExecutionSpec
getExecutionSpec() {
130 return executionSpec
;
133 public Object
getParameter(String parameterName
) {
134 // Verify that there is a spec attribute
135 ExecutionSpecAttribute specAttr
= null;
136 if (executionSpec
.getAttributes().containsKey(parameterName
)) {
137 specAttr
= executionSpec
.getAttributes().get(parameterName
);
139 throw new SlcException("Key " + parameterName
140 + " is not defined in the specifications of " + toString());
143 if (parameters
.containsKey(parameterName
)) {
144 Object paramValue
= parameters
.get(parameterName
);
145 if (specAttr
instanceof ResourceSpecAttribute
) {
146 // deal with resources
147 Resource resource
= resourceLoader
.getResource(paramValue
.toString());
148 return ((ResourceSpecAttribute
) specAttr
).convertResource(resource
);
153 if (specAttr
.getValue() != null) {
154 return specAttr
.getValue();
157 throw new SlcException("Key " + parameterName
158 + " is not set as parameter in " + toString());
161 public Boolean
isSetAsParameter(String key
) {
162 return parameters
.containsKey(key
)
163 || (executionSpec
.getAttributes().containsKey(key
) && executionSpec
164 .getAttributes().get(key
).getValue() != null);
167 public String
toString() {
168 return new StringBuffer("Flow ").append(name
).toString();
171 public boolean equals(Object obj
) {
172 return ((ExecutionFlow
) obj
).getName().equals(name
);
175 public String
getPath() {
179 public void setPath(String path
) {
183 public void setRegistry(StructureRegistry
<TreeSPath
> registry
) {
184 this.registry
= registry
;
187 public void notifyCurrentPath(StructureRegistry
<TreeSPath
> registry
,
189 if (this.path
== null) {
190 this.path
= path
.toString();
194 public void setResourceLoader(ResourceLoader resourceLoader
) {
195 this.resourceLoader
= resourceLoader
;