]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/ProcessThread.java
Revert explicit security context propagation
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / execution / ProcessThread.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
17 package org.argeo.slc.core.execution;
18
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Set;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.argeo.slc.SlcException;
28 import org.argeo.slc.execution.ExecutionModulesManager;
29 import org.argeo.slc.execution.ExecutionProcess;
30 import org.argeo.slc.execution.ExecutionStep;
31 import org.argeo.slc.process.RealizedFlow;
32 import org.argeo.slc.process.SlcExecution;
33
34 /** Thread of the SLC Process, starting the sub executions. */
35 @SuppressWarnings("deprecation")
36 public class ProcessThread extends Thread {
37 private final static Log log = LogFactory.getLog(ProcessThread.class);
38
39 private final ExecutionModulesManager executionModulesManager;
40 private final ExecutionProcess process;
41 private final ProcessThreadGroup processThreadGroup;
42
43 private Set<ExecutionThread> executionThreads = Collections
44 .synchronizedSet(new HashSet<ExecutionThread>());
45
46 private Boolean hadAnError = false;
47 private Boolean killed = false;
48
49 public ProcessThread(ThreadGroup processesThreadGroup,
50 ExecutionModulesManager executionModulesManager,
51 ExecutionProcess process) {
52 super(processesThreadGroup, "SLC Process #" + process.getUuid());
53 this.executionModulesManager = executionModulesManager;
54 this.process = process;
55 processThreadGroup = new ProcessThreadGroup(executionModulesManager,
56 this);
57 }
58
59 public final void run() {
60 // authenticate thread
61 // Authentication authentication = getProcessThreadGroup()
62 // .getAuthentication();
63 // if (authentication == null)
64 // throw new SlcException("Can only execute authenticated threads");
65 // SecurityContextHolder.getContext().setAuthentication(authentication);
66
67 // log.info("\n##\n## SLC Process #" + process.getUuid() +
68 // " STARTED by "
69 // + authentication.getName() + "\n##\n");
70 log.info("\n##\n## SLC Process #" + process.getUuid()
71 + " STARTED\n##\n");
72
73 // Start logging
74 new LoggingThread().start();
75
76 String oldStatus = process.getStatus();
77 process.setStatus(ExecutionProcess.RUNNING);
78 executionModulesManager.dispatchUpdateStatus(process, oldStatus,
79 ExecutionProcess.RUNNING);
80
81 try {
82 process();
83 } catch (InterruptedException e) {
84 die();
85 return;
86 }
87
88 // waits for all execution threads to complete (in case they were
89 // started asynchronously)
90 for (ExecutionThread executionThread : executionThreads) {
91 if (executionThread.isAlive()) {
92 try {
93 executionThread.join();
94 } catch (InterruptedException e) {
95 die();
96 return;
97 }
98 }
99 }
100
101 computeFinalStatus();
102 }
103
104 /** Make sure this is called BEFORE all the threads are interrupted. */
105 private void computeFinalStatus() {
106 String oldStatus = process.getStatus();
107 // TODO: error management at flow level?
108 if (killed)
109 process.setStatus(ExecutionProcess.KILLED);
110 else if (hadAnError)
111 process.setStatus(ExecutionProcess.ERROR);
112 else
113 process.setStatus(ExecutionProcess.COMPLETED);
114 executionModulesManager.dispatchUpdateStatus(process, oldStatus,
115 process.getStatus());
116 log.info("\n## SLC Process #" + process.getUuid() + " "
117 + process.getStatus() + "\n");
118 }
119
120 /** Called when being killed */
121 private synchronized void die() {
122 killed = true;
123 computeFinalStatus();
124 for (ExecutionThread executionThread : executionThreads) {
125 try {
126 executionThread.interrupt();
127 } catch (Exception e) {
128 log.error("Cannot interrupt " + executionThread);
129 }
130 }
131 processThreadGroup.interrupt();
132 }
133
134 /**
135 * Implementation specific execution. To be overridden in order to deal with
136 * custom process types. Default expects an {@link SlcExecution}.
137 */
138 protected void process() throws InterruptedException {
139 if (!(process instanceof SlcExecution))
140 throw new SlcException("Unsupported process type "
141 + process.getClass());
142 SlcExecution slcExecution = (SlcExecution) process;
143 List<RealizedFlow> flowsToProcess = new ArrayList<RealizedFlow>();
144 flowsToProcess.addAll(slcExecution.getRealizedFlows());
145
146 while (flowsToProcess.size() > 0) {
147 RealizedFlow realizedFlow = flowsToProcess.remove(0);
148 execute(realizedFlow, true);
149 }
150 }
151
152 /** @return the (distinct) thread used for this execution */
153 protected final void execute(RealizedFlow realizedFlow, Boolean synchronous)
154 throws InterruptedException {
155 if (killed)
156 return;
157
158 ExecutionThread thread = new ExecutionThread(this, realizedFlow);
159 executionThreads.add(thread);
160 thread.start();
161
162 if (synchronous)
163 thread.join();
164
165 return;
166 }
167
168 public void notifyError() {
169 hadAnError = true;
170 }
171
172 public synchronized void flowCompleted() {
173 // notifyAll();
174 }
175
176 public ExecutionProcess getProcess() {
177 return process;
178 }
179
180 public ProcessThreadGroup getProcessThreadGroup() {
181 return processThreadGroup;
182 }
183
184 public ExecutionModulesManager getExecutionModulesManager() {
185 return executionModulesManager;
186 }
187
188 private class LoggingThread extends Thread {
189
190 public LoggingThread() {
191 super("SLC Process Logger #" + process.getUuid());
192 }
193
194 public void run() {
195 boolean run = true;
196 while (run) {
197 List<ExecutionStep> newSteps = new ArrayList<ExecutionStep>();
198 processThreadGroup.getSteps().drainTo(newSteps);
199 if (newSteps.size() > 0) {
200 // System.out.println(steps.size() + " steps");
201 process.addSteps(newSteps);
202 }
203
204 try {
205 Thread.sleep(1000);
206 } catch (InterruptedException e) {
207 break;
208 }
209
210 if (!ProcessThread.this.isAlive()
211 && processThreadGroup.getSteps().size() == 0)
212 run = false;
213 }
214 }
215
216 }
217 }