]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.core/src/org/argeo/slc/core/execution/ExecutionThread.java
Disable trace logging
[gpl/argeo-slc.git] / org.argeo.slc.core / src / org / argeo / slc / core / execution / ExecutionThread.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
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 java.util.ArrayList;
19 import java.util.List;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.argeo.slc.SlcException;
24 import org.argeo.slc.execution.ExecutionFlowDescriptor;
25 import org.argeo.slc.execution.ExecutionModulesManager;
26 import org.argeo.slc.execution.ExecutionStep;
27 import org.argeo.slc.execution.RealizedFlow;
28 import org.springframework.security.core.Authentication;
29 import org.springframework.security.core.context.SecurityContextHolder;
30
31 /** Thread of a single execution */
32 public class ExecutionThread extends Thread {
33 public final static String SYSPROP_EXECUTION_AUTO_UPGRADE = "slc.execution.autoupgrade";
34 private final static Log log = LogFactory.getLog(ExecutionThread.class);
35
36 private ExecutionModulesManager executionModulesManager;
37 private final RealizedFlow realizedFlow;
38
39 private List<Runnable> destructionCallbacks = new ArrayList<Runnable>();
40
41 public ExecutionThread(ProcessThreadGroup processThreadGroup,
42 ExecutionModulesManager executionModulesManager,
43 RealizedFlow realizedFlow) {
44 super(processThreadGroup, "Flow "
45 + realizedFlow.getFlowDescriptor().getName());
46 this.realizedFlow = realizedFlow;
47 this.executionModulesManager = executionModulesManager;
48 }
49
50 public void run() {
51 // authenticate thread
52 Authentication authentication = getProcessThreadGroup()
53 .getAuthentication();
54 if (authentication == null)
55 throw new SlcException("Can only execute authenticated threads");
56 SecurityContextHolder.getContext().setAuthentication(authentication);
57
58 // Retrieve execution flow descriptor
59 ExecutionFlowDescriptor executionFlowDescriptor = realizedFlow
60 .getFlowDescriptor();
61 String flowName = executionFlowDescriptor.getName();
62
63 getProcessThreadGroup().dispatchAddStep(
64 new ExecutionStep(realizedFlow.getModuleName(),
65 ExecutionStep.PHASE_START, "Flow " + flowName));
66
67 try {
68 String autoUpgrade = System
69 .getProperty(SYSPROP_EXECUTION_AUTO_UPGRADE);
70 if (autoUpgrade != null && autoUpgrade.equals("true"))
71 executionModulesManager.upgrade(realizedFlow
72 .getModuleNameVersion());
73 executionModulesManager.start(realizedFlow.getModuleNameVersion());
74 //
75 // START FLOW
76 //
77 executionModulesManager.execute(realizedFlow);
78 // END FLOW
79 } catch (FlowConfigurationException e) {
80 String msg = "Configuration problem with flow " + flowName + ":\n"
81 + e.getMessage();
82 log.error(msg);
83 getProcessThreadGroup().dispatchAddStep(
84 new ExecutionStep(realizedFlow.getModuleName(),
85 ExecutionStep.ERROR, msg + " " + e.getMessage()));
86 } catch (Exception e) {
87 // TODO: re-throw exception ?
88 String msg = "Execution of flow " + flowName + " failed.";
89 log.error(msg, e);
90 getProcessThreadGroup().dispatchAddStep(
91 new ExecutionStep(realizedFlow.getModuleName(),
92 ExecutionStep.ERROR, msg + " " + e.getMessage()));
93 } finally {
94 getProcessThreadGroup().dispatchAddStep(
95 new ExecutionStep(realizedFlow.getModuleName(),
96 ExecutionStep.PHASE_END, "Flow " + flowName));
97 processDestructionCallbacks();
98 }
99 }
100
101 private synchronized void processDestructionCallbacks() {
102 for (int i = destructionCallbacks.size() - 1; i >= 0; i--) {
103 try {
104 destructionCallbacks.get(i).run();
105 } catch (Exception e) {
106 log.warn("Could not process destruction callback " + i
107 + " in thread " + getName(), e);
108 }
109 }
110 }
111
112 /**
113 * Gather object destruction callback to be called in reverse order at the
114 * end of the thread
115 */
116 synchronized void registerDestructionCallback(String name, Runnable callback) {
117 destructionCallbacks.add(callback);
118 }
119
120 protected ProcessThreadGroup getProcessThreadGroup() {
121 return (ProcessThreadGroup) getThreadGroup();
122 }
123 }