2 * Copyright (C) 2007-2012 Argeo GmbH
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.
16 package org
.argeo
.slc
.core
.execution
;
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
;
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
.execution
.RealizedFlow
;
35 import org
.argeo
.slc
.process
.SlcExecutionNotifier
;
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);
44 private List
<SlcExecutionNotifier
> slcExecutionNotifiers
= new ArrayList
<SlcExecutionNotifier
>();
46 private List
<FilteredNotifier
> filteredNotifiers
= Collections
47 .synchronizedList(new ArrayList
<FilteredNotifier
>());
49 protected abstract ExecutionFlow
findExecutionFlow(String moduleName
,
50 String moduleVersion
, String flowName
);
52 protected abstract ExecutionContext
findExecutionContext(String moduleName
,
53 String moduleVersion
);
55 protected abstract ExecutionFlowDescriptorConverter
getExecutionFlowDescriptorConverter(
56 String moduleName
, String moduleVersion
);
58 public void execute(RealizedFlow realizedFlow
) {
59 if (log
.isTraceEnabled())
60 log
.trace("Executing " + realizedFlow
);
62 String moduleName
= realizedFlow
.getModuleName();
63 String moduleVersion
= realizedFlow
.getModuleVersion();
65 Map
<?
extends String
, ?
extends Object
> variablesToAdd
= getExecutionFlowDescriptorConverter(
66 moduleName
, moduleVersion
).convertValues(
67 realizedFlow
.getFlowDescriptor());
68 ExecutionContext executionContext
= findExecutionContext(moduleName
,
70 for (String key
: variablesToAdd
.keySet())
71 executionContext
.setVariable(key
, variablesToAdd
.get(key
));
73 ExecutionFlow flow
= findExecutionFlow(moduleName
, moduleVersion
,
74 realizedFlow
.getFlowDescriptor().getName());
77 // Actually runs the flow, IN THIS THREAD
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
);
94 for (Iterator
<FilteredNotifier
> it
= filteredNotifiers
.iterator(); it
96 FilteredNotifier filteredNotifier
= it
.next();
97 if (filteredNotifier
.receiveFrom(process
))
98 filteredNotifier
.getNotifier().updateStatus(process
, oldStatus
,
104 public void dispatchAddSteps(ExecutionProcess process
,
105 List
<ExecutionStep
> steps
) {
106 process
.addSteps(steps
);
108 for (Iterator
<SlcExecutionNotifier
> it
= getSlcExecutionNotifiers()
109 .iterator(); it
.hasNext();) {
110 it
.next().addSteps(process
, steps
);
113 for (Iterator
<FilteredNotifier
> it
= filteredNotifiers
.iterator(); it
115 FilteredNotifier filteredNotifier
= it
.next();
116 if (filteredNotifier
.receiveFrom(process
))
117 filteredNotifier
.getNotifier().addSteps(process
, steps
);
121 public void registerProcessNotifier(ExecutionProcessNotifier notifier
,
122 Map
<String
, String
> properties
) {
123 filteredNotifiers
.add(new FilteredNotifier(notifier
, properties
));
126 public void unregisterProcessNotifier(ExecutionProcessNotifier notifier
,
127 Map
<String
, String
> properties
) {
128 filteredNotifiers
.remove(notifier
);
131 public void setSlcExecutionNotifiers(
132 List
<SlcExecutionNotifier
> slcExecutionNotifiers
) {
133 this.slcExecutionNotifiers
= slcExecutionNotifiers
;
136 private List
<SlcExecutionNotifier
> getSlcExecutionNotifiers() {
137 return slcExecutionNotifiers
;
140 protected class FilteredNotifier
{
141 private final ExecutionProcessNotifier notifier
;
142 private final String processId
;
144 public FilteredNotifier(ExecutionProcessNotifier notifier
,
145 Map
<String
, String
> properties
) {
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
);
157 * Whether event from this process should be received by this listener.
159 public Boolean
receiveFrom(ExecutionProcess process
) {
160 if (processId
!= null)
161 if (process
.getUuid().equals(processId
))
169 public int hashCode() {
170 return notifier
.hashCode();
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
);
185 public ExecutionProcessNotifier
getNotifier() {