2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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.
17 package org
.argeo
.slc
.core
.execution
;
19 import java
.util
.ArrayList
;
20 import java
.util
.Collections
;
21 import java
.util
.HashSet
;
22 import java
.util
.List
;
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
;
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);
39 private final ExecutionModulesManager executionModulesManager
;
40 private final ExecutionProcess process
;
41 private final ProcessThreadGroup processThreadGroup
;
43 private Set
<ExecutionThread
> executionThreads
= Collections
44 .synchronizedSet(new HashSet
<ExecutionThread
>());
46 private Boolean hadAnError
= false;
47 private Boolean killed
= false;
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
,
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);
67 // log.info("\n##\n## SLC Process #" + process.getUuid() +
69 // + authentication.getName() + "\n##\n");
70 log
.info("\n##\n## SLC Process #" + process
.getUuid()
74 new LoggingThread().start();
76 String oldStatus
= process
.getStatus();
77 process
.setStatus(ExecutionProcess
.RUNNING
);
78 executionModulesManager
.dispatchUpdateStatus(process
, oldStatus
,
79 ExecutionProcess
.RUNNING
);
83 } catch (InterruptedException e
) {
88 // waits for all execution threads to complete (in case they were
89 // started asynchronously)
90 for (ExecutionThread executionThread
: executionThreads
) {
91 if (executionThread
.isAlive()) {
93 executionThread
.join();
94 } catch (InterruptedException e
) {
101 computeFinalStatus();
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?
109 process
.setStatus(ExecutionProcess
.KILLED
);
111 process
.setStatus(ExecutionProcess
.ERROR
);
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");
120 /** Called when being killed */
121 private synchronized void die() {
123 computeFinalStatus();
124 for (ExecutionThread executionThread
: executionThreads
) {
126 executionThread
.interrupt();
127 } catch (Exception e
) {
128 log
.error("Cannot interrupt " + executionThread
);
131 processThreadGroup
.interrupt();
135 * Implementation specific execution. To be overridden in order to deal with
136 * custom process types. Default expects an {@link SlcExecution}.
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());
146 while (flowsToProcess
.size() > 0) {
147 RealizedFlow realizedFlow
= flowsToProcess
.remove(0);
148 execute(realizedFlow
, true);
152 /** @return the (distinct) thread used for this execution */
153 protected final void execute(RealizedFlow realizedFlow
, Boolean synchronous
)
154 throws InterruptedException
{
158 ExecutionThread thread
= new ExecutionThread(this, realizedFlow
);
159 executionThreads
.add(thread
);
168 public void notifyError() {
172 public synchronized void flowCompleted() {
176 public ExecutionProcess
getProcess() {
180 public ProcessThreadGroup
getProcessThreadGroup() {
181 return processThreadGroup
;
184 public ExecutionModulesManager
getExecutionModulesManager() {
185 return executionModulesManager
;
188 private class LoggingThread
extends Thread
{
190 public LoggingThread() {
191 super("SLC Process Logger #" + process
.getUuid());
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
);
206 } catch (InterruptedException e
) {
210 if (!ProcessThread
.this.isAlive()
211 && processThreadGroup
.getSteps().size() == 0)