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