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