2 * Copyright (C) 2007-2012 Mathieu Baudier
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.core
.execution
;
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
;
24 /** Thread of a single execution */
25 public class ExecutionThread
extends Thread
{
26 public final static String SYSPROP_EXECUTION_AUTO_UPGRADE
= "slc.execution.autoupgrade";
28 private final static Log log
= LogFactory
.getLog(ExecutionThread
.class);
30 private final RealizedFlow realizedFlow
;
31 private final ProcessThread processThread
;
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
;
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);
49 if (getContextClassLoader() != null) {
50 if (log
.isTraceEnabled())
51 log
.trace("Context class loader set to "
52 + getContextClassLoader());
55 // Retrieve execution flow descriptor
56 ExecutionFlowDescriptor executionFlowDescriptor
= realizedFlow
58 String flowName
= executionFlowDescriptor
.getName();
60 dispatchAddStep(new ExecutionStep(realizedFlow
.getModuleName(),
61 ExecutionStep
.PHASE_START
, "Flow " + flowName
));
64 String autoUpgrade
= System
65 .getProperty(SYSPROP_EXECUTION_AUTO_UPGRADE
);
66 if (autoUpgrade
!= null && autoUpgrade
.equals("true"))
67 processThread
.getExecutionModulesManager().upgrade(
68 realizedFlow
.getModuleNameVersion());
71 processThread
.getExecutionModulesManager().execute(realizedFlow
);
73 } catch (Exception e
) {
74 // TODO: re-throw exception ?
75 String msg
= "Execution of flow " + flowName
+ " failed.";
77 dispatchAddStep(new ExecutionStep(realizedFlow
.getModuleName(),
78 ExecutionStep
.ERROR
, msg
+ " " + e
.getMessage()));
79 processThread
.notifyError();
81 processThread
.flowCompleted();
82 dispatchAddStep(new ExecutionStep(realizedFlow
.getModuleName(),
83 ExecutionStep
.PHASE_END
, "Flow " + flowName
));
87 private void dispatchAddStep(ExecutionStep step
) {
88 processThread
.getProcessThreadGroup().dispatchAddStep(step
);
91 protected ProcessThreadGroup
getProcessThreadGroup() {
92 return processThread
.getProcessThreadGroup();
95 public RealizedFlow
getRealizedFlow() {