]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/ExecutionThread.java
Document and improve execution model
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / execution / ExecutionThread.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.argeo.slc.core.execution;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.argeo.slc.execution.ExecutionFlowDescriptor;
22 import org.argeo.slc.process.RealizedFlow;
23 import org.argeo.slc.process.SlcExecutionStep;
24
25 /** Thread of a single execution */
26 public class ExecutionThread extends Thread {
27 public final static String SYSPROP_EXECUTION_AUTO_UPGRADE = "slc.execution.autoupgrade";
28
29 private final static Log log = LogFactory.getLog(ExecutionThread.class);
30
31 private final RealizedFlow realizedFlow;
32 private final ProcessThread processThread;
33
34 public ExecutionThread(ProcessThread processThread,
35 RealizedFlow realizedFlow) {
36 super(processThread.getProcessThreadGroup(), "Flow "
37 + realizedFlow.getFlowDescriptor().getName());
38 this.realizedFlow = realizedFlow;
39 this.processThread = processThread;
40 }
41
42 public void run() {
43 if (getContextClassLoader() != null) {
44 if (log.isTraceEnabled())
45 log.debug("Context class loader set to "
46 + getContextClassLoader());
47 }
48
49 // Retrieve execution flow descriptor
50 ExecutionFlowDescriptor executionFlowDescriptor = realizedFlow
51 .getFlowDescriptor();
52 String flowName = executionFlowDescriptor.getName();
53
54 dispatchAddStep(new SlcExecutionStep(SlcExecutionStep.PHASE_START,
55 "Flow " + flowName));
56
57 try {
58 String autoUpgrade = System
59 .getProperty(SYSPROP_EXECUTION_AUTO_UPGRADE);
60 if (autoUpgrade != null && autoUpgrade.equals("true"))
61 processThread.getExecutionModulesManager().upgrade(
62 realizedFlow.getModuleNameVersion());
63
64 // START FLOW
65 processThread.getExecutionModulesManager().execute(realizedFlow);
66 // END FLOW
67 } catch (Exception e) {
68 // TODO: re-throw exception ?
69 String msg = "Execution of flow " + flowName + " failed.";
70 log.error(msg, e);
71 dispatchAddStep(new SlcExecutionStep(SlcExecutionStep.ERROR, msg
72 + " " + e.getMessage()));
73 processThread.notifyError();
74 } finally {
75 processThread.flowCompleted();
76 dispatchAddStep(new SlcExecutionStep(SlcExecutionStep.PHASE_END,
77 "Flow " + flowName));
78 }
79 }
80
81 private void dispatchAddStep(SlcExecutionStep step) {
82 processThread.getProcessThreadGroup().dispatchAddStep(step);
83 }
84
85 }