View Javadoc

1   /**
2    * $Id: BaseConsumerImpl.java 81 2006-12-15 21:44:02Z maldito_orco $
3    * $Revision: 81 $
4    * $Date: 2006-12-15 18:44:02 -0300 (Fri, 15 Dec 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.consumer;
23  
24  //log
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  //item
28  import org.tubo.item.Item;
29  import org.tubo.resource.baseimpl.BaseResourceImpl;
30  import org.tubo.resource.flow.FlowContext;
31  import org.tubo.event.Event;
32  
33  /**
34   * Created: Jun 21, 2005 6:59:48 AM
35   * Last Modification Date: $Date: 2006-12-15 18:44:02 -0300 (Fri, 15 Dec 2006) $
36   *
37   * @author jvlio (jvlio@users.sourceforge.net)
38   * @version $Revision: 81 $
39   */
40  public class BaseConsumerImpl extends BaseResourceImpl implements Consumer {
41      public static final String RCS_ID = "$Id: BaseConsumerImpl.java 81 2006-12-15 21:44:02Z maldito_orco $";
42  
43      private static Log log = LogFactory.getLog(BaseConsumerImpl.class);
44  
45      private ConsumerListener listener = null;
46  
47      /**
48       * Default empty constructor
49       */
50      public BaseConsumerImpl() {}
51  
52  
53      private ConsumerListener getListener() { return listener; }
54      public void setListener(ConsumerListener listener) {
55          this.listener = listener;
56      }
57  
58  
59      /**
60       * This method is a template method with the life cycle of the consumption process.
61       *
62       * @param eventAction
63       * @param consumedRawData
64       * @return FlowContext used on this life cycle
65       */
66      protected FlowContext doConsumerLifeCycle(String eventAction, Object consumedRawData) {
67          //
68          // create new Event
69          Event event = createEvent();
70          //
71          // set default event stuff
72          event.setAction(eventAction);
73          event.setWhen(System.currentTimeMillis());
74          //
75          // setup event (hook)
76          initializeEvent(event);
77          //
78          // create new item
79          Item item = createItem();
80          //
81          // set raw data consumed
82          item.setConsumedRawData(consumedRawData);
83          //
84          // setup item (hook)
85          initializeItem(item);
86          //
87          // dispatch event
88          FlowContext flowContext = dispatchOriginFlowEvent(event,item);
89          //
90          // return
91          return flowContext;
92      }
93  
94  
95      /**
96       * Create a new Item instance using ResourceManager
97       *
98       * @return a Item instance
99       */
100     protected Event createEvent() {
101         Event event = getResourceManager().getNewEvent();
102         return event;
103     }
104 
105     /**
106      * LifeCycle Hook used by subclasses to do a custom Event setup
107      *
108      * @param event Event to be configured
109      */
110     protected void initializeEvent(Event event) {}
111 
112     /**
113      * Create a new Item instance using ResourceManager
114      * @return a Item instance
115      */
116     protected Item createItem() {
117         Item item = getResourceManager().getNewItem();
118         return item;
119     }
120 
121     /**
122      * LifeCycle Hook used by subclasses to make a custom Event setup
123      *
124      * @param item Item to be configured
125      */
126     protected void initializeItem(Item item) {}
127                                      
128 
129     protected FlowContext dispatchOriginFlowEvent(Event event,Item item) {
130         //
131         // create FlowContext
132         FlowContext flowContext = createFlowContext();
133         //
134         // set default FlowContext stuff
135         flowContext.setOriginEvent(event);
136         flowContext.setItem(item);
137         flowContext.setOriginator(this);
138         //
139         // actions pre call listener (hook)
140         preOriginFlowEvent(flowContext);
141         //
142         // call listener and delegate process
143         getListener().onOriginFlowEvent(flowContext);
144         //
145         // actions post call listener (hook)
146         postOriginFlowEvent(flowContext);
147         //
148         // return flowContext
149         return flowContext;
150     }
151 
152     /**
153      * Create a new FlowContext instance using ResourceManager
154      * @return a Item instance
155      */
156     protected FlowContext createFlowContext() {
157         FlowContext flowContext = getResourceManager().getNewFlowContext();
158         return flowContext;
159     }
160 
161     /**
162      * LifeCycle Hook used by subclasses to execute pre event actions
163      * @param flowContext FlowContext to be used on this event
164      */
165     protected void preOriginFlowEvent(FlowContext flowContext) {}
166 
167     /**
168      * LifeCycle Hook used by subclasses to execute post event actions
169      * @param flowContext FlowContext used on this event
170      */
171     protected void postOriginFlowEvent(FlowContext flowContext) {}
172 
173 }