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