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
.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
.process
.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 private ThreadGroup processesThreadGroup
= new ThreadGroup("SLC Processes");
51 protected abstract ExecutionFlow
findExecutionFlow(String moduleName
,
52 String moduleVersion
, String flowName
);
54 protected abstract ExecutionContext
findExecutionContext(String moduleName
,
55 String moduleVersion
);
57 protected abstract ExecutionFlowDescriptorConverter
getExecutionFlowDescriptorConverter(
58 String moduleName
, String moduleVersion
);
60 public void execute(RealizedFlow realizedFlow
) {
61 if (log
.isTraceEnabled())
62 log
.trace("Executing " + realizedFlow
);
64 String moduleName
= realizedFlow
.getModuleName();
65 String moduleVersion
= realizedFlow
.getModuleVersion();
67 Map
<?
extends String
, ?
extends Object
> variablesToAdd
= getExecutionFlowDescriptorConverter(
68 moduleName
, moduleVersion
).convertValues(
69 realizedFlow
.getFlowDescriptor());
70 ExecutionContext executionContext
= findExecutionContext(moduleName
,
72 for (String key
: variablesToAdd
.keySet())
73 executionContext
.setVariable(key
, variablesToAdd
.get(key
));
75 ExecutionFlow flow
= findExecutionFlow(moduleName
, moduleVersion
,
76 realizedFlow
.getFlowDescriptor().getName());
79 // Actually runs the flow, IN THIS THREAD
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
);
96 for (Iterator
<FilteredNotifier
> it
= filteredNotifiers
.iterator(); it
98 FilteredNotifier filteredNotifier
= it
.next();
99 if (filteredNotifier
.receiveFrom(process
))
100 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 setSlcExecutionNotifiers(
126 List
<SlcExecutionNotifier
> slcExecutionNotifiers
) {
127 this.slcExecutionNotifiers
= slcExecutionNotifiers
;
130 private List
<SlcExecutionNotifier
> getSlcExecutionNotifiers() {
131 return slcExecutionNotifiers
;
134 public ThreadGroup
getProcessesThreadGroup() {
135 return processesThreadGroup
;
138 protected class FilteredNotifier
{
139 private final ExecutionProcessNotifier notifier
;
140 private final String processId
;
142 public FilteredNotifier(ExecutionProcessNotifier notifier
,
143 Map
<String
, String
> properties
) {
145 this.notifier
= notifier
;
146 if (properties
.containsKey(SLC_PROCESS_ID
))
147 processId
= properties
.get(SLC_PROCESS_ID
);
153 * Whether event from this process should be received by this listener.
155 public Boolean
receiveFrom(ExecutionProcess process
) {
156 if (processId
!= null)
157 if (process
.getUuid().equals(processId
))
165 public int hashCode() {
166 return notifier
.hashCode();
170 public boolean equals(Object obj
) {
171 return notifier
.equals(obj
);
174 public ExecutionProcessNotifier
getNotifier() {