]> 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 logging
[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.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 private ThreadGroup processesThreadGroup = new ThreadGroup("SLC Processes");
50
51 protected abstract ExecutionFlow findExecutionFlow(String moduleName,
52 String moduleVersion, String flowName);
53
54 protected abstract ExecutionContext findExecutionContext(String moduleName,
55 String moduleVersion);
56
57 protected abstract ExecutionFlowDescriptorConverter getExecutionFlowDescriptorConverter(
58 String moduleName, String moduleVersion);
59
60 public void execute(RealizedFlow realizedFlow) {
61 if (log.isTraceEnabled())
62 log.trace("Executing " + realizedFlow);
63
64 String moduleName = realizedFlow.getModuleName();
65 String moduleVersion = realizedFlow.getModuleVersion();
66
67 Map<? extends String, ? extends Object> variablesToAdd = getExecutionFlowDescriptorConverter(
68 moduleName, moduleVersion).convertValues(
69 realizedFlow.getFlowDescriptor());
70 ExecutionContext executionContext = findExecutionContext(moduleName,
71 moduleVersion);
72 for (String key : variablesToAdd.keySet())
73 executionContext.setVariable(key, variablesToAdd.get(key));
74
75 ExecutionFlow flow = findExecutionFlow(moduleName, moduleVersion,
76 realizedFlow.getFlowDescriptor().getName());
77
78 //
79 // Actually runs the flow, IN THIS THREAD
80 //
81 flow.run();
82 //
83 //
84 //
85 }
86
87 public void dispatchUpdateStatus(ExecutionProcess process,
88 String oldStatus, String newStatus) {
89 // generic notifiers (notified of everything)
90 for (Iterator<SlcExecutionNotifier> it = getSlcExecutionNotifiers()
91 .iterator(); it.hasNext();) {
92 it.next().updateStatus(process, oldStatus, newStatus);
93 }
94
95 // filtered notifiers
96 for (Iterator<FilteredNotifier> it = filteredNotifiers.iterator(); it
97 .hasNext();) {
98 FilteredNotifier filteredNotifier = it.next();
99 if (filteredNotifier.receiveFrom(process))
100 filteredNotifier.getNotifier().updateStatus(process, oldStatus,
101 newStatus);
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 setSlcExecutionNotifiers(
126 List<SlcExecutionNotifier> slcExecutionNotifiers) {
127 this.slcExecutionNotifiers = slcExecutionNotifiers;
128 }
129
130 private List<SlcExecutionNotifier> getSlcExecutionNotifiers() {
131 return slcExecutionNotifiers;
132 }
133
134 public ThreadGroup getProcessesThreadGroup() {
135 return processesThreadGroup;
136 }
137
138 protected class FilteredNotifier {
139 private final ExecutionProcessNotifier notifier;
140 private final String processId;
141
142 public FilteredNotifier(ExecutionProcessNotifier notifier,
143 Map<String, String> properties) {
144 super();
145 this.notifier = notifier;
146 if (properties.containsKey(SLC_PROCESS_ID))
147 processId = properties.get(SLC_PROCESS_ID);
148 else
149 processId = null;
150 }
151
152 /**
153 * Whether event from this process should be received by this listener.
154 */
155 public Boolean receiveFrom(ExecutionProcess process) {
156 if (processId != null)
157 if (process.getUuid().equals(processId))
158 return true;
159 else
160 return false;
161 return true;
162 }
163
164 @Override
165 public int hashCode() {
166 return notifier.hashCode();
167 }
168
169 @Override
170 public boolean equals(Object obj) {
171 return notifier.equals(obj);
172 }
173
174 public ExecutionProcessNotifier getNotifier() {
175 return notifier;
176 }
177
178 }
179 }