]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/AbstractExecutionModulesManager.java
Document and improve execution model
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / execution / AbstractExecutionModulesManager.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 import java.util.Map;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.argeo.slc.execution.ExecutionContext;
27 import org.argeo.slc.execution.ExecutionFlow;
28 import org.argeo.slc.execution.ExecutionFlowDescriptorConverter;
29 import org.argeo.slc.execution.ExecutionModulesManager;
30 import org.argeo.slc.execution.ExecutionProcess;
31 import org.argeo.slc.process.RealizedFlow;
32 import org.argeo.slc.process.SlcExecutionNotifier;
33 import org.argeo.slc.process.SlcExecutionStep;
34
35 /** Provides the base feature of an execution module manager. */
36 @SuppressWarnings("deprecation")
37 public abstract class AbstractExecutionModulesManager implements
38 ExecutionModulesManager {
39 private final static Log log = LogFactory
40 .getLog(AbstractExecutionModulesManager.class);
41
42 private List<SlcExecutionNotifier> slcExecutionNotifiers = new ArrayList<SlcExecutionNotifier>();
43
44 private ThreadGroup processesThreadGroup = new ThreadGroup("Processes");
45
46 protected abstract ExecutionFlow findExecutionFlow(String moduleName,
47 String moduleVersion, String flowName);
48
49 protected abstract ExecutionContext findExecutionContext(String moduleName,
50 String moduleVersion);
51
52 protected abstract ExecutionFlowDescriptorConverter getExecutionFlowDescriptorConverter(
53 String moduleName, String moduleVersion);
54
55 public void execute(RealizedFlow realizedFlow) {
56 if (log.isTraceEnabled())
57 log.trace("Executing " + realizedFlow);
58
59 String moduleName = realizedFlow.getModuleName();
60 String moduleVersion = realizedFlow.getModuleVersion();
61
62 Map<? extends String, ? extends Object> variablesToAdd = getExecutionFlowDescriptorConverter(
63 moduleName, moduleVersion).convertValues(
64 realizedFlow.getFlowDescriptor());
65 ExecutionContext executionContext = findExecutionContext(moduleName,
66 moduleVersion);
67 for (String key : variablesToAdd.keySet())
68 executionContext.setVariable(key, variablesToAdd.get(key));
69
70 ExecutionFlow flow = findExecutionFlow(moduleName, moduleVersion,
71 realizedFlow.getFlowDescriptor().getName());
72
73 //
74 // Actually runs the flow, IN THIS THREAD
75 //
76 flow.run();
77 //
78 //
79 //
80 }
81
82 public void dispatchUpdateStatus(ExecutionProcess slcExecution,
83 String oldStatus, String newStatus) {
84 for (Iterator<SlcExecutionNotifier> it = getSlcExecutionNotifiers()
85 .iterator(); it.hasNext();) {
86 it.next().updateStatus(slcExecution, oldStatus, newStatus);
87 }
88 }
89
90 public void dispatchAddStep(ExecutionProcess slcExecution,
91 SlcExecutionStep step) {
92 List<SlcExecutionStep> steps = new ArrayList<SlcExecutionStep>();
93 steps.add(step);
94 for (Iterator<SlcExecutionNotifier> it = getSlcExecutionNotifiers()
95 .iterator(); it.hasNext();) {
96 it.next().addSteps(slcExecution, steps);
97 }
98 }
99
100 public void setSlcExecutionNotifiers(
101 List<SlcExecutionNotifier> slcExecutionNotifiers) {
102 this.slcExecutionNotifiers = slcExecutionNotifiers;
103 }
104
105 public List<SlcExecutionNotifier> getSlcExecutionNotifiers() {
106 return slcExecutionNotifiers;
107 }
108
109 public ThreadGroup getProcessesThreadGroup() {
110 return processesThreadGroup;
111 }
112
113 }