]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/DefaultExecutionFlow.java
Rename OverrideContextAware task
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / core / execution / DefaultExecutionFlow.java
1 package org.argeo.slc.core.execution;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7
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;
25
26 public class DefaultExecutionFlow implements ExecutionFlow, InitializingBean,
27 BeanNameAware, StructureAware<TreeSPath>, ResourceLoaderAware {
28
29 private final static Log log = LogFactory
30 .getLog(DefaultExecutionFlow.class);
31
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>();
36
37 private String path;
38 private StructureRegistry<TreeSPath> registry = new TreeSRegistry();
39
40 private ResourceLoader resourceLoader = null;
41
42 public DefaultExecutionFlow() {
43 this.executionSpec = new DefaultExecutionSpec();
44 }
45
46 public DefaultExecutionFlow(ExecutionSpec executionSpec) {
47 this.executionSpec = executionSpec;
48 }
49
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()
54 : executionSpec;
55
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");
61 }
62 }
63
64 // set the parameters
65 this.parameters.putAll(parameters);
66
67 // check that all the required parameters are defined
68 MapBindingResult errors = new MapBindingResult(parameters, "execution#"
69 + getName());
70 for (String key : executionSpec.getAttributes().keySet()) {
71 ExecutionSpecAttribute attr = executionSpec.getAttributes()
72 .get(key);
73
74 if (attr.getIsParameter() && !isSetAsParameter(key)) {
75 errors.rejectValue(key, "Parameter not set");
76 break;
77 }
78
79 if (attr.getIsFrozen() && !isSetAsParameter(key)) {
80 errors.rejectValue(key, "Frozen but not set as parameter");
81 break;
82 }
83
84 if (attr.getIsHidden() && !isSetAsParameter(key)) {
85 errors.rejectValue(key, "Hidden but not set as parameter");
86 break;
87 }
88 }
89
90 if (errors.hasErrors())
91 throw new SlcException("Could not prepare execution flow: "
92 + errors.toString());
93
94 }
95
96 public void execute() {
97 for (Executable executable : executables) {
98 executable.execute();
99 }
100 }
101
102 public void afterPropertiesSet() throws Exception {
103 if (path != null) {
104 for (Executable executable : executables) {
105 if (executable instanceof StructureAware) {
106 ((StructureAware<TreeSPath>) executable).notifyCurrentPath(
107 registry, new TreeSPath(path));
108 }
109 }
110 }
111 }
112
113 public void setBeanName(String name) {
114 this.name = name;
115 }
116
117 public void setExecutables(List<Executable> executables) {
118 this.executables = executables;
119 }
120
121 public void setParameters(Map<String, Object> attributes) {
122 this.parameters = attributes;
123 }
124
125 public String getName() {
126 return name;
127 }
128
129 public ExecutionSpec getExecutionSpec() {
130 return executionSpec;
131 }
132
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);
138 } else {
139 throw new SlcException("Key " + parameterName
140 + " is not defined in the specifications of " + toString());
141 }
142
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);
149 } else {
150 return paramValue;
151 }
152 } else {
153 if (specAttr.getValue() != null) {
154 return specAttr.getValue();
155 }
156 }
157 throw new SlcException("Key " + parameterName
158 + " is not set as parameter in " + toString());
159 }
160
161 public Boolean isSetAsParameter(String key) {
162 return parameters.containsKey(key)
163 || (executionSpec.getAttributes().containsKey(key) && executionSpec
164 .getAttributes().get(key).getValue() != null);
165 }
166
167 public String toString() {
168 return new StringBuffer("Flow ").append(name).toString();
169 }
170
171 public boolean equals(Object obj) {
172 return ((ExecutionFlow) obj).getName().equals(name);
173 }
174
175 public String getPath() {
176 return path;
177 }
178
179 public void setPath(String path) {
180 this.path = path;
181 }
182
183 public void setRegistry(StructureRegistry<TreeSPath> registry) {
184 this.registry = registry;
185 }
186
187 public void notifyCurrentPath(StructureRegistry<TreeSPath> registry,
188 TreeSPath path) {
189 if (this.path == null) {
190 this.path = path.toString();
191 }
192 }
193
194 public void setResourceLoader(ResourceLoader resourceLoader) {
195 this.resourceLoader = resourceLoader;
196 }
197
198 }