View Javadoc

1   /**
2    * $Id: BaseFlowProcessorImpl.java 53 2006-11-08 06:50:20Z maldito_orco $
3    * $Revision: 53 $
4    * $Date: 2006-11-08 03:50:20 -0300 (Wed, 08 Nov 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  //log
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  //tubo
28  import org.tubo.resource.flow.FlowProcessor;
29  import org.tubo.resource.flow.FlowContext;
30  import org.tubo.resource.component.ComponentManager;
31  import org.tubo.resource.component.Component;
32  import org.tubo.resource.ResourceManager;
33  import org.tubo.configuration.def.FlowDef;
34  import org.tubo.configuration.def.FlowNodeDef;
35  import org.tubo.configuration.def.FlowNodeList;
36  import org.tubo.configuration.def.ComponentRefFlowNodeDef;
37  import org.tubo.configuration.def.FlowRefFlowNodeDef;
38  import org.tubo.configuration.def.SwitchFlowNodeDef;
39  import org.tubo.exception.TuboException;
40  import org.tubo.item.Item;
41  //util
42  import java.util.Iterator;
43  import java.util.Properties;
44  import java.util.Map;
45  import java.lang.reflect.Method;
46  import java.lang.reflect.InvocationTargetException;
47  
48  /**
49   * <p>
50   * </p>
51   *
52   * <p>
53   * Created: Oct 20, 2006, 2:45:35 PM<br>
54   * Last Modification Date: $Date: 2006-11-08 03:50:20 -0300 (Wed, 08 Nov 2006) $
55   * </p>
56   *
57   * @author maldito_orco (maldito_orco@users.sourceforge.net)
58   * @version $Revision: 53 $
59   */
60  public class BaseFlowProcessorImpl implements FlowProcessor {
61      public static final String RCS_ID = "$Id: BaseFlowProcessorImpl.java 53 2006-11-08 06:50:20Z maldito_orco $";
62      private static Log log = LogFactory.getLog(BaseFlowProcessorImpl.class);
63  
64      private ResourceManager resourceManager = null;
65  
66      public ResourceManager getResourceManager() { return resourceManager; }
67      public void setResourceManager(ResourceManager resourceManager) throws TuboException {
68          this.resourceManager = resourceManager;
69      }
70  
71      /**
72       * Execute a Flow Definition
73       *
74       * @param flowDef
75       * @param context
76       */
77      public void execute(FlowDef flowDef, FlowContext context) {
78          if (log.isTraceEnabled()) log.trace(".execute(FlowDef,FlowContext) - Executing Flow ID="+flowDef.getId());
79          //
80          // get flow node list
81          FlowNodeList flowNodeList = flowDef.getFlowNodeList();
82          //
83          // execute list
84          execute(flowNodeList,context);
85          //
86          // execute node list
87          if (log.isTraceEnabled()) log.trace(".execute(FlowDef,FlowContext) - Done");
88      }
89  
90      /**
91       * Execute a List of FlowNodeDef
92       * @param flowNodeList
93       * @param context
94       */
95      protected void execute(FlowNodeList flowNodeList, FlowContext context) {
96          if (log.isTraceEnabled()) log.trace(".execute(FlowNodeList,FlowContext) -  Enter");
97          //
98          // iterate all nodes
99          for (Iterator it=flowNodeList.getList().iterator(); it.hasNext(); ) {
100             //
101             // cast to flow node def
102             FlowNodeDef flowNodeDef = (FlowNodeDef)it.next();
103             //
104             // execute flow node def
105             execute(flowNodeDef, context);
106         }
107         if (log.isTraceEnabled()) log.trace(".execute(FlowNodeList,FlowContext)  - Done");
108     }
109 
110     /**
111      * Execute a Node
112      * @param flowNodeDef
113      * @param context
114      */
115     protected void execute(FlowNodeDef flowNodeDef, FlowContext context) {
116         //
117         // check if is a node referencing to a component
118         if (flowNodeDef instanceof ComponentRefFlowNodeDef) {
119             //
120             // cast to ComponentRefFlowNodeDef
121             ComponentRefFlowNodeDef componentRef = (ComponentRefFlowNodeDef)flowNodeDef;
122             //
123             // execute component
124             execute(componentRef,context);
125         }
126         //
127         // check if is a node referencing a flow
128         else if (flowNodeDef instanceof FlowRefFlowNodeDef) {
129             //
130             // cast to FlowRefFlowNodeDef
131             FlowRefFlowNodeDef flowRef = (FlowRefFlowNodeDef)flowNodeDef;
132             //
133             // execute flow
134             execute(flowRef,context);
135         }
136         //
137         // check if is a switch node
138         else if (flowNodeDef instanceof SwitchFlowNodeDef) {
139             //
140             // cast to SwitchFlowNodeDef
141             SwitchFlowNodeDef switchDef = (SwitchFlowNodeDef)flowNodeDef;
142             //
143             // execute switch
144             execute(switchDef,context);
145         }
146     }
147 
148     /**
149      * Execute a Component Node
150      * @param componentRef
151      * @param context
152      */
153     protected void execute(ComponentRefFlowNodeDef componentRef, FlowContext context) {
154         if (log.isTraceEnabled()) log.trace(".execute(ComponentRefFlowNodeDef,FlowContext)  - Executing Component ID="+componentRef.getRefId());
155         //
156         // get component id (which this node reference)
157         String componentId = componentRef.getRefId();
158         //
159         // get (via resource manager) the correct manager
160         ComponentManager componentManager = resourceManager.getComponentManager(componentId);
161         //
162         // set flow node properties
163         Properties flowNodeProps = componentRef.getProperties();
164         Map itemProps = context.getItem().getProperties();
165         itemProps.putAll(flowNodeProps);
166 
167         //
168         // FIXME: ONLY TEST, FIX THAT!!!!!!!!!!!!!!!!!!
169         //        we need exception management  :D
170         Component component = null;
171         String methodName = null;
172         try {
173 //
174             // get component (via manager)
175             component = componentManager.getComponent();
176             //
177             // check event listen
178             String action = context.getOriginEvent().getAction();
179             if(componentRef.isListenEvent(action)) {
180                 //
181                 // get method to execute
182                 methodName = componentRef.getListenEventDef(action).getExecute();
183                 //
184                 // call method
185                 Method method = component.getClass().getMethod(methodName, new Class[] {FlowContext.class});
186                 method.invoke(component,new Object[] {context});
187             } else {
188                 //
189                 // execute default method
190                 component.execute(context);
191             }
192         } catch (NoSuchMethodException e) {
193             Object args[] = {
194                     component.getClass().getName(),
195                     methodName,
196                     componentId
197             };
198             throw resourceManager.getExceptionManager().getException(1500,args,e);
199         } catch (IllegalAccessException e) {
200             Object args[] = {
201                     component.getClass().getName(),
202                     methodName,
203                     componentId
204             };
205             throw resourceManager.getExceptionManager().getException(1501,args,e);
206         } catch (IllegalArgumentException e) {
207             Object args[] = {
208                     component.getClass().getName(),
209                     methodName,
210                     componentId
211             };
212             throw resourceManager.getExceptionManager().getException(1502,args,e);
213         } catch (InvocationTargetException e) {
214             Object args[] = {
215                     component.getClass().getName(),
216                     methodName,
217                     componentId
218             };
219             throw resourceManager.getExceptionManager().getException(1503,args,e);
220         } finally {
221             //
222             // restore component
223             if (component!=null)
224                 componentManager.restore(component);
225             //
226             // delete flow node properties
227             for(Iterator it=flowNodeProps.keySet().iterator(); it.hasNext(); ) {
228                 String prop = (String)it.next();
229                 itemProps.remove(prop);
230             }
231         }
232     }
233 
234     /**
235      * Execute a Flow Node
236      * @param flowRef
237      * @param context
238      */
239     protected void execute(FlowRefFlowNodeDef flowRef, FlowContext context) {
240         //
241         // get flow id (which this node reference)
242         String flowId = flowRef.getRefId();
243         //
244         // get flow def
245         FlowDef flowDef = resourceManager.getConfiguration().getFlowDef(flowId);
246         //
247         // execute flow def (recursive call!)
248         execute(flowDef,context);
249     }
250 
251     /**
252      * Execute Switch
253      * @param flowRef
254      * @param context
255      */
256     protected void execute(SwitchFlowNodeDef flowRef, FlowContext context) {
257         log.error(".execute(SwitchFlowNodeDef,FlowContext) - ERROR, feature not implemented. Switch Nodes not implemented yet.");
258     }
259 
260 }
261