]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/ExecutionThread.java
Improve log
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / execution / ExecutionThread.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
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 package org.argeo.slc.core.execution;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.argeo.slc.execution.ExecutionFlowDescriptor;
21 import org.argeo.slc.execution.ExecutionStep;
22 import org.argeo.slc.process.RealizedFlow;
23
24 /** Thread of a single execution */
25 public class ExecutionThread extends Thread {
26 public final static String SYSPROP_EXECUTION_AUTO_UPGRADE = "slc.execution.autoupgrade";
27
28 private final static Log log = LogFactory.getLog(ExecutionThread.class);
29
30 private final RealizedFlow realizedFlow;
31 private final ProcessThread processThread;
32
33 public ExecutionThread(ProcessThread processThread,
34 RealizedFlow realizedFlow) {
35 super(processThread.getProcessThreadGroup(), "Flow "
36 + realizedFlow.getFlowDescriptor().getName());
37 this.realizedFlow = realizedFlow;
38 this.processThread = processThread;
39 }
40
41 public void run() {
42 // authenticate thread
43 // Authentication authentication = getProcessThreadGroup()
44 // .getAuthentication();
45 // if (authentication == null)
46 // throw new SlcException("Can only execute authenticated threads");
47 // SecurityContextHolder.getContext().setAuthentication(authentication);
48
49 if (getContextClassLoader() != null) {
50 if (log.isTraceEnabled())
51 log.trace("Context class loader set to "
52 + getContextClassLoader());
53 }
54
55 // Retrieve execution flow descriptor
56 ExecutionFlowDescriptor executionFlowDescriptor = realizedFlow
57 .getFlowDescriptor();
58 String flowName = executionFlowDescriptor.getName();
59
60 dispatchAddStep(new ExecutionStep(realizedFlow.getModuleName(),
61 ExecutionStep.PHASE_START, "Flow " + flowName));
62
63 try {
64 String autoUpgrade = System
65 .getProperty(SYSPROP_EXECUTION_AUTO_UPGRADE);
66 if (autoUpgrade != null && autoUpgrade.equals("true"))
67 processThread.getExecutionModulesManager().upgrade(
68 realizedFlow.getModuleNameVersion());
69
70 // START FLOW
71 processThread.getExecutionModulesManager().execute(realizedFlow);
72 // END FLOW
73 } catch (Exception e) {
74 // TODO: re-throw exception ?
75 String msg = "Execution of flow " + flowName + " failed.";
76 log.error(msg, e);
77 dispatchAddStep(new ExecutionStep(realizedFlow.getModuleName(),
78 ExecutionStep.ERROR, msg + " " + e.getMessage()));
79 processThread.notifyError();
80 } finally {
81 processThread.flowCompleted();
82 dispatchAddStep(new ExecutionStep(realizedFlow.getModuleName(),
83 ExecutionStep.PHASE_END, "Flow " + flowName));
84 }
85 }
86
87 private void dispatchAddStep(ExecutionStep step) {
88 processThread.getProcessThreadGroup().dispatchAddStep(step);
89 }
90
91 protected ProcessThreadGroup getProcessThreadGroup() {
92 return processThread.getProcessThreadGroup();
93 }
94
95 public RealizedFlow getRealizedFlow() {
96 return realizedFlow;
97 }
98
99 }