]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/ProcessThread.java
Flows automatically registered in JCR
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / execution / ProcessThread.java
index 1987f259e9ef8c84d997f6ada6a0a0f0283cc638..5af3262d57cb4923a76ce2e6a738154876ff12aa 100644 (file)
@@ -1,42 +1,58 @@
+/*
+ * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *         http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 package org.argeo.slc.core.execution;
 
 import java.util.ArrayList;
-import java.util.Iterator;
 import java.util.List;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.argeo.slc.execution.ExecutionModulesManager;
 import org.argeo.slc.process.RealizedFlow;
 import org.argeo.slc.process.SlcExecution;
-import org.argeo.slc.process.SlcExecutionNotifier;
 
 /** Thread of the SLC Process, starting the sub executions. */
 public class ProcessThread extends Thread {
        private final static Log log = LogFactory.getLog(ProcessThread.class);
 
-       private final AbstractExecutionModulesManager executionModulesManager;
+       private final ExecutionModulesManager executionModulesManager;
        private final SlcExecution slcProcess;
        private final ProcessThreadGroup processThreadGroup;
        private final List<RealizedFlow> flowsToProcess = new ArrayList<RealizedFlow>();
 
        private Boolean hadAnError = false;
 
-       public ProcessThread(
-                       AbstractExecutionModulesManager executionModulesManager,
+       public ProcessThread(ExecutionModulesManager executionModulesManager,
                        SlcExecution slcExecution) {
                super(executionModulesManager.getProcessesThreadGroup(),
                                "SLC Process #" + slcExecution.getUuid());
                this.executionModulesManager = executionModulesManager;
                this.slcProcess = slcExecution;
-               processThreadGroup = new ProcessThreadGroup(this);
+               processThreadGroup = new ProcessThreadGroup(executionModulesManager,
+                               this);
        }
 
        public void run() {
-               log.info("\n##\n## Process SLC Execution " + slcProcess + "\n##\n");
+               log.info("\n##\n## SLC Process #" + slcProcess.getUuid()
+                               + " STARTED\n##\n");
 
                slcProcess.setStatus(SlcExecution.STATUS_RUNNING);
-               dispatchUpdateStatus(slcProcess, SlcExecution.STATUS_SCHEDULED,
-                               SlcExecution.STATUS_RUNNING);
+               executionModulesManager.dispatchUpdateStatus(slcProcess,
+                               SlcExecution.STATUS_SCHEDULED, SlcExecution.STATUS_RUNNING);
 
                flowsToProcess.addAll(slcProcess.getRealizedFlows());
 
@@ -45,29 +61,30 @@ public class ProcessThread extends Thread {
                        ExecutionThread thread = new ExecutionThread(this, flow);
                        thread.start();
 
-                       synchronized (this) {
-                               try {
-                                       wait();
-                               } catch (InterruptedException e) {
-                                       // silent
-                               }
+                       try {
+                               thread.join();
+                       } catch (InterruptedException e) {
+                               log.error("Flow " + flow + " was interrupted", e);
                        }
+
+                       // synchronized (this) {
+                       // try {
+                       // wait();
+                       // } catch (InterruptedException e) {
+                       // // silent
+                       // }
+                       // }
                }
 
+               // TODO: error management at flow level?
                if (hadAnError)
                        slcProcess.setStatus(SlcExecution.STATUS_ERROR);
                else
                        slcProcess.setStatus(SlcExecution.STATUS_FINISHED);
-               dispatchUpdateStatus(slcProcess, SlcExecution.STATUS_RUNNING,
-                               slcProcess.getStatus());
-       }
+               executionModulesManager.dispatchUpdateStatus(slcProcess,
+                               SlcExecution.STATUS_RUNNING, slcProcess.getStatus());
 
-       protected void dispatchUpdateStatus(SlcExecution slcExecution,
-                       String oldStatus, String newStatus) {
-               for (Iterator<SlcExecutionNotifier> it = executionModulesManager
-                               .getSlcExecutionNotifiers().iterator(); it.hasNext();) {
-                       it.next().updateStatus(slcExecution, oldStatus, newStatus);
-               }
+               log.info("\n## SLC Process #" + slcProcess.getUuid() + " COMPLETED\n");
        }
 
        public void notifyError() {
@@ -75,7 +92,7 @@ public class ProcessThread extends Thread {
        }
 
        public synchronized void flowCompleted() {
-               notifyAll();
+               // notifyAll();
        }
 
        public SlcExecution getSlcProcess() {
@@ -86,7 +103,7 @@ public class ProcessThread extends Thread {
                return processThreadGroup;
        }
 
-       public AbstractExecutionModulesManager getExecutionModulesManager() {
+       public ExecutionModulesManager getExecutionModulesManager() {
                return executionModulesManager;
        }
 }