]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/ProcessThread.java
Introduce JCR agent. Client agent not needed anymore.
[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.List;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.argeo.slc.execution.ExecutionModulesManager;
25 import org.argeo.slc.process.RealizedFlow;
26 import org.argeo.slc.process.SlcExecution;
27
28 /** Thread of the SLC Process, starting the sub executions. */
29 public class ProcessThread extends Thread {
30 private final static Log log = LogFactory.getLog(ProcessThread.class);
31
32 private final ExecutionModulesManager executionModulesManager;
33 private final SlcExecution slcProcess;
34 private final ProcessThreadGroup processThreadGroup;
35 private final List<RealizedFlow> flowsToProcess = new ArrayList<RealizedFlow>();
36
37 private Boolean hadAnError = false;
38
39 public ProcessThread(ExecutionModulesManager executionModulesManager,
40 SlcExecution slcExecution) {
41 super(executionModulesManager.getProcessesThreadGroup(),
42 "SLC Process #" + slcExecution.getUuid());
43 this.executionModulesManager = executionModulesManager;
44 this.slcProcess = slcExecution;
45 processThreadGroup = new ProcessThreadGroup(executionModulesManager,
46 this);
47 }
48
49 public void run() {
50 log.info("\n##\n## SLC Process #" + slcProcess.getUuid()
51 + " STARTED\n##\n");
52
53 slcProcess.setStatus(SlcExecution.STATUS_RUNNING);
54 executionModulesManager.dispatchUpdateStatus(slcProcess,
55 SlcExecution.STATUS_SCHEDULED, SlcExecution.STATUS_RUNNING);
56
57 flowsToProcess.addAll(slcProcess.getRealizedFlows());
58
59 while (flowsToProcess.size() > 0) {
60 RealizedFlow flow = flowsToProcess.remove(0);
61 ExecutionThread thread = new ExecutionThread(this, flow);
62 thread.start();
63
64 try {
65 thread.join();
66 } catch (InterruptedException e) {
67 log.error("Flow " + flow + " was interrupted", e);
68 }
69
70 // synchronized (this) {
71 // try {
72 // wait();
73 // } catch (InterruptedException e) {
74 // // silent
75 // }
76 // }
77 }
78
79 // TODO: error management at flow level?
80 if (hadAnError)
81 slcProcess.setStatus(SlcExecution.STATUS_ERROR);
82 else
83 slcProcess.setStatus(SlcExecution.STATUS_FINISHED);
84 executionModulesManager.dispatchUpdateStatus(slcProcess,
85 SlcExecution.STATUS_RUNNING, slcProcess.getStatus());
86
87 log.info("\n## SLC Process #" + slcProcess.getUuid() + " COMPLETED\n");
88 }
89
90 public void notifyError() {
91 hadAnError = true;
92 }
93
94 public synchronized void flowCompleted() {
95 // notifyAll();
96 }
97
98 public SlcExecution getSlcProcess() {
99 return slcProcess;
100 }
101
102 public ProcessThreadGroup getProcessThreadGroup() {
103 return processThreadGroup;
104 }
105
106 public ExecutionModulesManager getExecutionModulesManager() {
107 return executionModulesManager;
108 }
109 }