]> git.argeo.org Git - gpl/argeo-slc.git/blob - ExecutionThread.java
c81e8b1a0d64eecfa2b13bd5a2a3349d1024ed2a
[gpl/argeo-slc.git] / ExecutionThread.java
1 package org.argeo.slc.core.execution;
2
3 import java.util.ArrayList;
4 import java.util.Iterator;
5 import java.util.List;
6
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9 import org.argeo.slc.execution.ExecutionFlowDescriptor;
10 import org.argeo.slc.process.RealizedFlow;
11 import org.argeo.slc.process.SlcExecutionNotifier;
12 import org.argeo.slc.process.SlcExecutionStep;
13
14 /** Thread of a single execution */
15 public class ExecutionThread extends Thread {
16 public final static String SYSPROP_EXECUTION_AUTO_UPGRADE = "slc.execution.autoupgrade";
17
18 private final static Log log = LogFactory.getLog(ExecutionThread.class);
19
20 private final RealizedFlow realizedFlow;
21 private final ProcessThread processThread;
22
23 public ExecutionThread(ProcessThread processThread,
24 RealizedFlow realizedFlow) {
25 super(processThread.getProcessThreadGroup(), "Flow "
26 + realizedFlow.getFlowDescriptor().getName());
27 this.realizedFlow = realizedFlow;
28 this.processThread = processThread;
29 }
30
31 public void run() {
32 if (getContextClassLoader() != null) {
33 if (log.isTraceEnabled())
34 log.debug("Context class loader set to "
35 + getContextClassLoader());
36 }
37
38 // Retrieve execution flow descriptor
39 ExecutionFlowDescriptor executionFlowDescriptor = realizedFlow
40 .getFlowDescriptor();
41 String flowName = executionFlowDescriptor.getName();
42
43 dispatchAddStep(new SlcExecutionStep(SlcExecutionStep.TYPE_PHASE_START,
44 "Flow " + flowName));
45
46 try {
47 String autoUpgrade = System
48 .getProperty(SYSPROP_EXECUTION_AUTO_UPGRADE);
49 if (autoUpgrade != null && autoUpgrade.equals("true"))
50 processThread.getExecutionModulesManager().upgrade(
51 realizedFlow.getModuleNameVersion());
52 processThread.getExecutionModulesManager().execute(realizedFlow);
53 } catch (Exception e) {
54 // TODO: re-throw exception ?
55 String msg = "Execution of flow " + flowName + " failed.";
56 log.error(msg, e);
57 dispatchAddStep(new SlcExecutionStep(msg + " " + e.getMessage()));
58 processThread.notifyError();
59 } finally {
60 processThread.flowCompleted();
61 dispatchAddStep(new SlcExecutionStep(
62 SlcExecutionStep.TYPE_PHASE_END, "Flow " + flowName));
63 }
64 }
65
66 public void dispatchAddStep(SlcExecutionStep step) {
67 processThread.getSlcProcess().getSteps().add(step);
68 List<SlcExecutionStep> steps = new ArrayList<SlcExecutionStep>();
69 steps.add(step);
70 for (Iterator<SlcExecutionNotifier> it = processThread
71 .getExecutionModulesManager().getSlcExecutionNotifiers()
72 .iterator(); it.hasNext();) {
73 it.next().addSteps(processThread.getSlcProcess(), steps);
74 }
75 }
76
77 }