2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.slc
.core
.execution
;
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
;
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
;
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);
45 private List
<SlcExecutionNotifier
> slcExecutionNotifiers
= new ArrayList
<SlcExecutionNotifier
>();
47 private List
<FilteredNotifier
> filteredNotifiers
= Collections
48 .synchronizedList(new ArrayList
<FilteredNotifier
>());
50 protected abstract ExecutionFlow
findExecutionFlow(String moduleName
,
51 String moduleVersion
, String flowName
);
53 protected abstract ExecutionContext
findExecutionContext(String moduleName
,
54 String moduleVersion
);
56 protected abstract ExecutionFlowDescriptorConverter
getExecutionFlowDescriptorConverter(
57 String moduleName
, String moduleVersion
);
59 public void execute(RealizedFlow realizedFlow
) {
60 if (log
.isTraceEnabled())
61 log
.trace("Executing " + realizedFlow
);
63 String moduleName
= realizedFlow
.getModuleName();
64 String moduleVersion
= realizedFlow
.getModuleVersion();
66 Map
<?
extends String
, ?
extends Object
> variablesToAdd
= getExecutionFlowDescriptorConverter(
67 moduleName
, moduleVersion
).convertValues(
68 realizedFlow
.getFlowDescriptor());
69 ExecutionContext executionContext
= findExecutionContext(moduleName
,
71 for (String key
: variablesToAdd
.keySet())
72 executionContext
.setVariable(key
, variablesToAdd
.get(key
));
74 ExecutionFlow flow
= findExecutionFlow(moduleName
, moduleVersion
,
75 realizedFlow
.getFlowDescriptor().getName());
78 // Actually runs the flow, IN THIS THREAD
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
);
95 for (Iterator
<FilteredNotifier
> it
= filteredNotifiers
.iterator(); it
97 FilteredNotifier filteredNotifier
= it
.next();
98 if (filteredNotifier
.receiveFrom(process
))
99 filteredNotifier
.getNotifier().updateStatus(process
, oldStatus
,
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
);
112 for (Iterator
<FilteredNotifier
> it
= filteredNotifiers
.iterator(); it
114 FilteredNotifier filteredNotifier
= it
.next();
115 if (filteredNotifier
.receiveFrom(process
))
116 filteredNotifier
.getNotifier().addSteps(process
, steps
);
120 public void registerProcessNotifier(ExecutionProcessNotifier notifier
,
121 Map
<String
, String
> properties
) {
122 filteredNotifiers
.add(new FilteredNotifier(notifier
, properties
));
125 public void unregisterProcessNotifier(ExecutionProcessNotifier notifier
,
126 Map
<String
, String
> properties
) {
127 filteredNotifiers
.remove(notifier
);
130 public void setSlcExecutionNotifiers(
131 List
<SlcExecutionNotifier
> slcExecutionNotifiers
) {
132 this.slcExecutionNotifiers
= slcExecutionNotifiers
;
135 private List
<SlcExecutionNotifier
> getSlcExecutionNotifiers() {
136 return slcExecutionNotifiers
;
139 protected class FilteredNotifier
{
140 private final ExecutionProcessNotifier notifier
;
141 private final String processId
;
143 public FilteredNotifier(ExecutionProcessNotifier notifier
,
144 Map
<String
, String
> properties
) {
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
);
156 * Whether event from this process should be received by this listener.
158 public Boolean
receiveFrom(ExecutionProcess process
) {
159 if (processId
!= null)
160 if (process
.getUuid().equals(processId
))
168 public int hashCode() {
169 return notifier
.hashCode();
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
);
184 public ExecutionProcessNotifier
getNotifier() {