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
.SlcException
;
21 import org
.argeo
.slc
.execution
.ExecutionFlowDescriptor
;
22 import org
.argeo
.slc
.execution
.ExecutionStep
;
23 import org
.argeo
.slc
.execution
.RealizedFlow
;
24 import org
.springframework
.security
.Authentication
;
25 import org
.springframework
.security
.context
.SecurityContextHolder
;
27 /** Thread of a single execution */
28 public class ExecutionThread
extends Thread
{
29 public final static String SYSPROP_EXECUTION_AUTO_UPGRADE
= "slc.execution.autoupgrade";
31 private final static Log log
= LogFactory
.getLog(ExecutionThread
.class);
33 private final RealizedFlow realizedFlow
;
34 private final ProcessThread processThread
;
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
;
45 // authenticate thread
46 Authentication authentication
= getProcessThreadGroup()
48 if (authentication
== null)
49 throw new SlcException("Can only execute authenticated threads");
50 SecurityContextHolder
.getContext().setAuthentication(authentication
);
52 if (getContextClassLoader() != null) {
53 if (log
.isTraceEnabled())
54 log
.trace("Context class loader set to "
55 + getContextClassLoader());
58 // Retrieve execution flow descriptor
59 ExecutionFlowDescriptor executionFlowDescriptor
= realizedFlow
61 String flowName
= executionFlowDescriptor
.getName();
63 dispatchAddStep(new ExecutionStep(realizedFlow
.getModuleName(),
64 ExecutionStep
.PHASE_START
, "Flow " + flowName
));
67 String autoUpgrade
= System
68 .getProperty(SYSPROP_EXECUTION_AUTO_UPGRADE
);
69 if (autoUpgrade
!= null && autoUpgrade
.equals("true"))
70 processThread
.getExecutionModulesManager().upgrade(
71 realizedFlow
.getModuleNameVersion());
74 processThread
.getExecutionModulesManager().execute(realizedFlow
);
76 } catch (Exception e
) {
77 // TODO: re-throw exception ?
78 String msg
= "Execution of flow " + flowName
+ " failed.";
80 dispatchAddStep(new ExecutionStep(realizedFlow
.getModuleName(),
81 ExecutionStep
.ERROR
, msg
+ " " + e
.getMessage()));
82 processThread
.notifyError();
84 processThread
.flowCompleted();
85 dispatchAddStep(new ExecutionStep(realizedFlow
.getModuleName(),
86 ExecutionStep
.PHASE_END
, "Flow " + flowName
));
90 private void dispatchAddStep(ExecutionStep step
) {
91 processThread
.getProcessThreadGroup().dispatchAddStep(step
);
94 protected ProcessThreadGroup
getProcessThreadGroup() {
95 return processThread
.getProcessThreadGroup();
98 public RealizedFlow
getRealizedFlow() {