]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/ProcessThread.java
Start working on serialized JMS
[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.Iterator;
21 import java.util.List;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.argeo.slc.process.RealizedFlow;
26 import org.argeo.slc.process.SlcExecution;
27 import org.argeo.slc.process.SlcExecutionNotifier;
28
29 /** Thread of the SLC Process, starting the sub executions. */
30 public class ProcessThread extends Thread {
31 private final static Log log = LogFactory.getLog(ProcessThread.class);
32
33 private final AbstractExecutionModulesManager executionModulesManager;
34 private final SlcExecution slcProcess;
35 private final ProcessThreadGroup processThreadGroup;
36 private final List<RealizedFlow> flowsToProcess = new ArrayList<RealizedFlow>();
37
38 private Boolean hadAnError = false;
39
40 public ProcessThread(
41 AbstractExecutionModulesManager executionModulesManager,
42 SlcExecution slcExecution) {
43 super(executionModulesManager.getProcessesThreadGroup(),
44 "SLC Process #" + slcExecution.getUuid());
45 this.executionModulesManager = executionModulesManager;
46 this.slcProcess = slcExecution;
47 processThreadGroup = new ProcessThreadGroup(this);
48 }
49
50 public void run() {
51 log.info("\n##\n## Process SLC Execution " + slcProcess + "\n##\n");
52
53 slcProcess.setStatus(SlcExecution.STATUS_RUNNING);
54 dispatchUpdateStatus(slcProcess, SlcExecution.STATUS_SCHEDULED,
55 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 synchronized (this) {
65 try {
66 wait();
67 } catch (InterruptedException e) {
68 // silent
69 }
70 }
71 }
72
73 if (hadAnError)
74 slcProcess.setStatus(SlcExecution.STATUS_ERROR);
75 else
76 slcProcess.setStatus(SlcExecution.STATUS_FINISHED);
77 dispatchUpdateStatus(slcProcess, SlcExecution.STATUS_RUNNING,
78 slcProcess.getStatus());
79 }
80
81 protected void dispatchUpdateStatus(SlcExecution slcExecution,
82 String oldStatus, String newStatus) {
83 for (Iterator<SlcExecutionNotifier> it = executionModulesManager
84 .getSlcExecutionNotifiers().iterator(); it.hasNext();) {
85 it.next().updateStatus(slcExecution, oldStatus, newStatus);
86 }
87 }
88
89 public void notifyError() {
90 hadAnError = true;
91 }
92
93 public synchronized void flowCompleted() {
94 notifyAll();
95 }
96
97 public SlcExecution getSlcProcess() {
98 return slcProcess;
99 }
100
101 public ProcessThreadGroup getProcessThreadGroup() {
102 return processThreadGroup;
103 }
104
105 public AbstractExecutionModulesManager getExecutionModulesManager() {
106 return executionModulesManager;
107 }
108 }