1 /**
2 * $Id: BaseFlowExecutorImpl.java 49 2006-10-26 06:10:31Z maldito_orco $
3 * $Revision: 49 $
4 * $Date: 2006-10-26 03:10:31 -0300 (Thu, 26 Oct 2006) $
5 *
6 * =========================================================================
7 *
8 * Copyright 2005 Tubo
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22 package org.tubo.resource.flow;
23
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 import org.tubo.resource.ResourceManager;
29 import org.tubo.resource.consumer.ConsumerListener;
30 import org.tubo.exception.TuboException;
31 import org.tubo.configuration.def.FlowDef;
32
33 import java.util.Map;
34
35 /**
36 * <p></p>
37 * <p>
38 * Created: Oct 23, 2006, 12:21:15 AM <br>
39 * Last Modification Date: $Date: 2006-10-26 03:10:31 -0300 (Thu, 26 Oct 2006) $
40 * </p>
41 *
42 * @author maldito_orco (maldito_orco@users.sourceforge.net)
43 * @version $Revision: 49 $
44 */
45 public class BaseFlowExecutorImpl implements FlowExecutor, ConsumerListener {
46 public static final String RCS_ID = "$Id: BaseFlowExecutorImpl.java 49 2006-10-26 06:10:31Z maldito_orco $";
47 private static Log log = LogFactory.getLog(BaseFlowExecutorImpl.class);
48
49 private ResourceManager resourceManager = null;
50 private Map consumerFlowMapping = null;
51
52 public ResourceManager getResourceManager() { return resourceManager; }
53 public void setResourceManager(ResourceManager resourceManager) throws TuboException {
54 this.resourceManager = resourceManager;
55 }
56
57 public Map getConsumerFlowMapping() { return consumerFlowMapping; }
58 public void setConsumerFlowMapping(Map consumerFlowMapping) {
59 this.consumerFlowMapping = consumerFlowMapping;
60 }
61
62 /**
63 * TODO: Exception?? NullPointer??
64 * @param consumerId
65 * @return
66 */
67 public String mapConsumerIdToFlow(String consumerId) {
68 String flowId = (String)consumerFlowMapping.get(consumerId);
69 return flowId;
70 }
71
72 /**
73 * Execute the flow associated with this consumer
74 *
75 * @param flowContext
76 * @throws TuboException
77 */
78 public void execute(FlowContext flowContext) throws TuboException {
79
80
81 String consumerId = flowContext.getOriginator().getId();
82
83
84 String flowId = mapConsumerIdToFlow(consumerId);
85
86
87 FlowDef flowDef = resourceManager.getConfiguration().getFlowDef(flowId);
88
89
90
91 FlowProcessor flowProcessor = new BaseFlowProcessorImpl();
92 flowProcessor.setResourceManager(getResourceManager());
93 flowProcessor.execute(flowDef, flowContext);
94
95 }
96
97
98 /**
99 * Consumer listener event handler
100 *
101 * @param flowContext
102 */
103 public void onOriginFlowEvent(FlowContext flowContext) {
104 if (log.isTraceEnabled()) log.trace(".onOriginFlowEvent() - Executing FlowEvent action="+flowContext.getOriginEvent().getAction());
105
106
107
108 try {
109 execute(flowContext);
110 } catch (TuboException e) {
111 log.error("Error executing flow", e);
112 }
113 if (log.isTraceEnabled()) log.trace(".onOriginFlowEvent() - Done");
114 }
115 }