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