1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.argeo.slc.core.execution.generator;
17
18 import java.util.ArrayList;
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.argeo.slc.SlcException;
26 import org.argeo.slc.core.execution.DefaultExecutionSpec;
27 import org.argeo.slc.execution.ExecutionContext;
28 import org.argeo.slc.execution.ExecutionFlow;
29 import org.argeo.slc.execution.ExecutionSpec;
30 import org.springframework.context.ApplicationContext;
31 import org.springframework.context.ApplicationContextAware;
32
33
34
35
36
37
38
39 public class RunnableCallFlow implements ExecutionFlow, ApplicationContextAware {
40
41 private final static Log log = LogFactory.getLog(RunnableCallFlow.class);
42
43
44
45
46
47 public final static String VAR_CALL_INDEX = "slcVar.runnableCallFlow.callIndex";
48
49
50
51
52 private String name;
53
54
55
56
57 private String path;
58
59
60
61
62
63 private Boolean failOnError = true;
64
65
66
67
68
69 private List<RunnableCall> runnableCalls;
70
71
72
73
74
75 private Map<String, Object> sharedContextValuesMap;
76
77
78
79
80 private ExecutionSpec executionSpec = new DefaultExecutionSpec();
81
82
83
84
85 private ExecutionContext executionContext;
86
87
88
89
90
91
92 private ApplicationContext applicationContext;
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 private void run(Runnable runnable, Map<String, Object> executionVariables,
110 Map<String, Object> contextValues, int callIndex) {
111
112 for (Map.Entry<String, Object> entry : executionVariables.entrySet()) {
113 executionContext.setVariable(entry.getKey(), entry.getValue());
114 }
115
116
117 executionContext.setVariable(VAR_CALL_INDEX, callIndex);
118
119
120 if (sharedContextValuesMap != null) {
121 sharedContextValuesMap.clear();
122 sharedContextValuesMap.putAll(contextValues);
123 }
124
125
126 doExecuteRunnable(runnable);
127 }
128
129 public void doExecuteRunnable(Runnable runnable) {
130 runnable.run();
131 }
132
133
134
135
136
137
138
139 public void run() {
140 if (applicationContext == null) {
141 throw new SlcException("No ApplicationContext defined");
142 }
143
144 try {
145 for (int callIndex = 0; callIndex < runnableCalls.size(); ++callIndex) {
146 RunnableCall runnableCall = runnableCalls.get(callIndex);
147 Object bean = applicationContext.getBean(
148 runnableCall.getBeanName(), Runnable.class);
149 if (log.isDebugEnabled())
150 log.debug("Running flow '" + runnableCall.getBeanName()
151 + "'");
152 run((Runnable) bean, runnableCall.getExecutionVariables(),
153 runnableCall.getContextValues(), callIndex);
154 }
155 } catch (RuntimeException e) {
156 if (failOnError)
157 throw e;
158 else {
159 log.error("Execution flow failed,"
160 + " but process did not fail"
161 + " because failOnError property"
162 + " is set to false: " + e);
163 if (log.isTraceEnabled())
164 e.printStackTrace();
165 }
166 }
167 }
168
169 public Iterator<Runnable> runnables() {
170 List<Runnable> runnables = new ArrayList<Runnable>();
171 for (int callIndex = 0; callIndex < runnableCalls.size(); ++callIndex) {
172 RunnableCall runnableCall = runnableCalls.get(callIndex);
173 Object bean = applicationContext.getBean(
174 runnableCall.getBeanName(), Runnable.class);
175 runnables.add((Runnable) bean);
176 }
177 return runnables.iterator();
178 }
179
180 public Runnable getRunnable() {
181 if (runnableCalls.size() == 1)
182 return runnables().next();
183 else
184 throw new SlcException("There are " + runnableCalls.size()
185 + " runnables in flow " + getName());
186 }
187
188 @Override
189 public String toString() {
190 return new StringBuffer("RunnableCallFlow ").append(name).toString();
191 }
192
193 public ExecutionSpec getExecutionSpec() {
194 return executionSpec;
195 }
196
197 public String getName() {
198 return name;
199 }
200
201 public Object getParameter(String key) {
202 throw new SlcException("RunnableCallFlow have no parameters");
203 }
204
205 public String getPath() {
206 return path;
207 }
208
209 public Boolean isSetAsParameter(String key) {
210
211
212 return false;
213 }
214
215 public void setName(String name) {
216 this.name = name;
217 }
218
219 public void setPath(String path) {
220 this.path = path;
221 }
222
223 public void setExecutionContext(ExecutionContext executionContext) {
224 this.executionContext = executionContext;
225 }
226
227 public void setRunnableCalls(List<RunnableCall> runnableCalls) {
228 this.runnableCalls = runnableCalls;
229 }
230
231 public void setApplicationContext(ApplicationContext applicationContext) {
232 this.applicationContext = applicationContext;
233 }
234
235 public void setSharedContextValuesMap(Map<String, Object> contextValues) {
236 this.sharedContextValuesMap = contextValues;
237 }
238
239 public void setFailOnError(Boolean failOnError) {
240 this.failOnError = failOnError;
241 }
242
243 }