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