]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/AbstractExecutionModulesManager.java
b3d92ae69aad4d9bf6ae605e56569ed512d79197
[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.Collections;
21 import java.util.HashMap;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.argeo.slc.execution.ExecutionContext;
29 import org.argeo.slc.execution.ExecutionFlow;
30 import org.argeo.slc.execution.ExecutionFlowDescriptorConverter;
31 import org.argeo.slc.execution.ExecutionModulesManager;
32 import org.argeo.slc.execution.ExecutionProcess;
33 import org.argeo.slc.execution.ExecutionProcessNotifier;
34 import org.argeo.slc.execution.ExecutionStep;
35 import org.argeo.slc.process.RealizedFlow;
36 import org.argeo.slc.process.SlcExecutionNotifier;
37
38 /** Provides the base feature of an execution module manager. */
39 @SuppressWarnings("deprecation")
40 public abstract class AbstractExecutionModulesManager implements
41 ExecutionModulesManager {
42 private final static Log log = LogFactory
43 .getLog(AbstractExecutionModulesManager.class);
44
45 private List<SlcExecutionNotifier> slcExecutionNotifiers = new ArrayList<SlcExecutionNotifier>();
46
47 private List<FilteredNotifier> filteredNotifiers = Collections
48 .synchronizedList(new ArrayList<FilteredNotifier>());
49
50 protected abstract ExecutionFlow findExecutionFlow(String moduleName,
51 String moduleVersion, String flowName);
52
53 protected abstract ExecutionContext findExecutionContext(String moduleName,
54 String moduleVersion);
55
56 protected abstract ExecutionFlowDescriptorConverter getExecutionFlowDescriptorConverter(
57 String moduleName, String moduleVersion);
58
59 public void execute(RealizedFlow realizedFlow) {
60 if (log.isTraceEnabled())
61 log.trace("Executing " + realizedFlow);
62
63 String moduleName = realizedFlow.getModuleName();
64 String moduleVersion = realizedFlow.getModuleVersion();
65
66 Map<? extends String, ? extends Object> variablesToAdd = getExecutionFlowDescriptorConverter(
67 moduleName, moduleVersion).convertValues(
68 realizedFlow.getFlowDescriptor());
69 ExecutionContext executionContext = findExecutionContext(moduleName,
70 moduleVersion);
71 for (String key : variablesToAdd.keySet())
72 executionContext.setVariable(key, variablesToAdd.get(key));
73
74 ExecutionFlow flow = findExecutionFlow(moduleName, moduleVersion,
75 realizedFlow.getFlowDescriptor().getName());
76
77 //
78 // Actually runs the flow, IN THIS THREAD
79 //
80 flow.run();
81 //
82 //
83 //
84 }
85
86 public void dispatchUpdateStatus(ExecutionProcess process,
87 String oldStatus, String newStatus) {
88 // generic notifiers (notified of everything)
89 for (Iterator<SlcExecutionNotifier> it = getSlcExecutionNotifiers()
90 .iterator(); it.hasNext();) {
91 it.next().updateStatus(process, oldStatus, newStatus);
92 }
93
94 // filtered notifiers
95 for (Iterator<FilteredNotifier> it = filteredNotifiers.iterator(); it
96 .hasNext();) {
97 FilteredNotifier filteredNotifier = it.next();
98 if (filteredNotifier.receiveFrom(process))
99 filteredNotifier.getNotifier().updateStatus(process, oldStatus,
100 newStatus);
101 }
102
103 }
104
105 public void dispatchAddSteps(ExecutionProcess process,
106 List<ExecutionStep> steps) {
107 for (Iterator<SlcExecutionNotifier> it = getSlcExecutionNotifiers()
108 .iterator(); it.hasNext();) {
109 it.next().addSteps(process, steps);
110 }
111
112 for (Iterator<FilteredNotifier> it = filteredNotifiers.iterator(); it
113 .hasNext();) {
114 FilteredNotifier filteredNotifier = it.next();
115 if (filteredNotifier.receiveFrom(process))
116 filteredNotifier.getNotifier().addSteps(process, steps);
117 }
118 }
119
120 public void registerProcessNotifier(ExecutionProcessNotifier notifier,
121 Map<String, String> properties) {
122 filteredNotifiers.add(new FilteredNotifier(notifier, properties));
123 }
124
125 public void unregisterProcessNotifier(ExecutionProcessNotifier notifier,
126 Map<String, String> properties) {
127 filteredNotifiers.remove(notifier);
128 }
129
130 public void setSlcExecutionNotifiers(
131 List<SlcExecutionNotifier> slcExecutionNotifiers) {
132 this.slcExecutionNotifiers = slcExecutionNotifiers;
133 }
134
135 private List<SlcExecutionNotifier> getSlcExecutionNotifiers() {
136 return slcExecutionNotifiers;
137 }
138
139 protected class FilteredNotifier {
140 private final ExecutionProcessNotifier notifier;
141 private final String processId;
142
143 public FilteredNotifier(ExecutionProcessNotifier notifier,
144 Map<String, String> properties) {
145 super();
146 this.notifier = notifier;
147 if (properties == null)
148 properties = new HashMap<String, String>();
149 if (properties.containsKey(SLC_PROCESS_ID))
150 processId = properties.get(SLC_PROCESS_ID);
151 else
152 processId = null;
153 }
154
155 /**
156 * Whether event from this process should be received by this listener.
157 */
158 public Boolean receiveFrom(ExecutionProcess process) {
159 if (processId != null)
160 if (process.getUuid().equals(processId))
161 return true;
162 else
163 return false;
164 return true;
165 }
166
167 @Override
168 public int hashCode() {
169 return notifier.hashCode();
170 }
171
172 @Override
173 public boolean equals(Object obj) {
174 if (obj instanceof FilteredNotifier) {
175 FilteredNotifier fn = (FilteredNotifier) obj;
176 return notifier.equals(fn.notifier);
177 } else if (obj instanceof ExecutionProcessNotifier) {
178 ExecutionProcessNotifier epn = (ExecutionProcessNotifier) obj;
179 return notifier.equals(epn);
180 } else
181 return false;
182 }
183
184 public ExecutionProcessNotifier getNotifier() {
185 return notifier;
186 }
187
188 }
189 }