]> 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
Introduce revision build numbers
[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.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;
22
23 public class DefaultExecutionFlow implements ExecutionFlow, InitializingBean,
24 BeanNameAware, StructureAware<TreeSPath>, ResourceLoaderAware {
25
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>();
30
31 private String path;
32 private StructureRegistry<TreeSPath> registry = new TreeSRegistry();
33
34 private ResourceLoader resourceLoader = null;
35
36 public DefaultExecutionFlow() {
37 this.executionSpec = new DefaultExecutionSpec();
38 }
39
40 public DefaultExecutionFlow(ExecutionSpec executionSpec) {
41 this.executionSpec = executionSpec;
42 }
43
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()
48 : executionSpec;
49
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");
55 }
56 }
57
58 // set the parameters
59 this.parameters.putAll(parameters);
60
61 // check that all the required parameters are defined
62 MapBindingResult errors = new MapBindingResult(parameters, "execution#"
63 + getName());
64 for (String key : executionSpec.getAttributes().keySet()) {
65 ExecutionSpecAttribute attr = executionSpec.getAttributes()
66 .get(key);
67
68 if (attr.getIsParameter() && !isSetAsParameter(key)) {
69 errors.rejectValue(key, "Parameter not set");
70 break;
71 }
72
73 if (attr.getIsFrozen() && !isSetAsParameter(key)) {
74 errors.rejectValue(key, "Frozen but not set as parameter");
75 break;
76 }
77
78 if (attr.getIsHidden() && !isSetAsParameter(key)) {
79 errors.rejectValue(key, "Hidden but not set as parameter");
80 break;
81 }
82 }
83
84 if (errors.hasErrors())
85 throw new SlcException("Could not prepare execution flow: "
86 + errors.toString());
87
88 }
89
90 public void run() {
91 for (Runnable executable : executables) {
92 executable.run();
93 }
94 }
95
96 public void afterPropertiesSet() throws Exception {
97 if (path != null) {
98 for (Runnable executable : executables) {
99 if (executable instanceof StructureAware) {
100 ((StructureAware<TreeSPath>) executable).notifyCurrentPath(
101 registry, new TreeSPath(path));
102 }
103 }
104 }
105 }
106
107 public void setBeanName(String name) {
108 this.name = name;
109 }
110
111 public void setExecutables(List<Runnable> executables) {
112 this.executables = executables;
113 }
114
115 public void setParameters(Map<String, Object> attributes) {
116 this.parameters = attributes;
117 }
118
119 public String getName() {
120 return name;
121 }
122
123 public ExecutionSpec getExecutionSpec() {
124 return executionSpec;
125 }
126
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);
132 } else {
133 throw new SlcException("Key " + parameterName
134 + " is not defined in the specifications of " + toString());
135 }
136
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
142 .toString());
143 return ((ResourceSpecAttribute) specAttr)
144 .convertResource(resource);
145 } else {
146 return paramValue;
147 }
148 } else {
149 if (specAttr.getValue() != null) {
150 return specAttr.getValue();
151 }
152 }
153 throw new SlcException("Key " + parameterName
154 + " is not set as parameter in " + toString());
155 }
156
157 public Boolean isSetAsParameter(String key) {
158 return parameters.containsKey(key)
159 || (executionSpec.getAttributes().containsKey(key) && executionSpec
160 .getAttributes().get(key).getValue() != null);
161 }
162
163 public String toString() {
164 return new StringBuffer("Flow ").append(name).toString();
165 }
166
167 public boolean equals(Object obj) {
168 return ((ExecutionFlow) obj).getName().equals(name);
169 }
170
171 public String getPath() {
172 return path;
173 }
174
175 public void setPath(String path) {
176 this.path = path;
177 }
178
179 public void setRegistry(StructureRegistry<TreeSPath> registry) {
180 this.registry = registry;
181 }
182
183 public void notifyCurrentPath(StructureRegistry<TreeSPath> registry,
184 TreeSPath path) {
185 if (this.path == null) {
186 this.path = path.toString();
187 }
188 }
189
190 public void setResourceLoader(ResourceLoader resourceLoader) {
191 this.resourceLoader = resourceLoader;
192 }
193
194 }