]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/AbstractExecutionModulesManager.java
Improve diff issue
[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 process.addSteps(steps);
108
109 for (Iterator<SlcExecutionNotifier> it = getSlcExecutionNotifiers()
110 .iterator(); it.hasNext();) {
111 it.next().addSteps(process, steps);
112 }
113
114 for (Iterator<FilteredNotifier> it = filteredNotifiers.iterator(); it
115 .hasNext();) {
116 FilteredNotifier filteredNotifier = it.next();
117 if (filteredNotifier.receiveFrom(process))
118 filteredNotifier.getNotifier().addSteps(process, steps);
119 }
120 }
121
122 public void registerProcessNotifier(ExecutionProcessNotifier notifier,
123 Map<String, String> properties) {
124 filteredNotifiers.add(new FilteredNotifier(notifier, properties));
125 }
126
127 public void unregisterProcessNotifier(ExecutionProcessNotifier notifier,
128 Map<String, String> properties) {
129 filteredNotifiers.remove(notifier);
130 }
131
132 public void setSlcExecutionNotifiers(
133 List<SlcExecutionNotifier> slcExecutionNotifiers) {
134 this.slcExecutionNotifiers = slcExecutionNotifiers;
135 }
136
137 private List<SlcExecutionNotifier> getSlcExecutionNotifiers() {
138 return slcExecutionNotifiers;
139 }
140
141 protected class FilteredNotifier {
142 private final ExecutionProcessNotifier notifier;
143 private final String processId;
144
145 public FilteredNotifier(ExecutionProcessNotifier notifier,
146 Map<String, String> properties) {
147 super();
148 this.notifier = notifier;
149 if (properties == null)
150 properties = new HashMap<String, String>();
151 if (properties.containsKey(SLC_PROCESS_ID))
152 processId = properties.get(SLC_PROCESS_ID);
153 else
154 processId = null;
155 }
156
157 /**
158 * Whether event from this process should be received by this listener.
159 */
160 public Boolean receiveFrom(ExecutionProcess process) {
161 if (processId != null)
162 if (process.getUuid().equals(processId))
163 return true;
164 else
165 return false;
166 return true;
167 }
168
169 @Override
170 public int hashCode() {
171 return notifier.hashCode();
172 }
173
174 @Override
175 public boolean equals(Object obj) {
176 if (obj instanceof FilteredNotifier) {
177 FilteredNotifier fn = (FilteredNotifier) obj;
178 return notifier.equals(fn.notifier);
179 } else if (obj instanceof ExecutionProcessNotifier) {
180 ExecutionProcessNotifier epn = (ExecutionProcessNotifier) obj;
181 return notifier.equals(epn);
182 } else
183 return false;
184 }
185
186 public ExecutionProcessNotifier getNotifier() {
187 return notifier;
188 }
189
190 }
191 }