org.enhydra.barracuda.core.event
Class DefaultEventDispatcher

java.lang.Object
  extended byorg.enhydra.barracuda.core.event.DefaultEventDispatcher
All Implemented Interfaces:
EventDispatcher
Direct Known Subclasses:
PageEventDispatcher

public class DefaultEventDispatcher
extends Object
implements EventDispatcher

This class is responsible for dispatching a queue of events to interested listeners. If you ever want to create a custom dispatching policy, this is the class to extend.

This particular implementation takes a repeating two phased dispatch approach in order to provide a Model 2 flow control mechanism. In order to pull this off, the incoming DispatchQueue must be an instance of DefaultDispatchQueue. If it is not, an EventException will be thrown.

Basically, we dispatch all the non-Response events in the queue first. Listeners which handle these events loosely correspond to controllers. Then, we dispatch all the Response events. Listeners that handle these loosely correspond to views. If a response is required and there is no response event, we look to the queue state to locate a DEFAULT_RESPONSE_EVENT, which defaults to HttpResponseEvent. To implement a single phase dispatch, just define the queue as not requiring a response and avoid adding Response events to it.

Note that when we are dispatching, if we encounter an event that implements Polymorphic, we create and dispatch parent events first. If we enouncter an event that implements Exceptional and that event is NOT handled, then we add it's parent event to the queue.

Note that the getEventChain method could probably be better optimized...


Field Summary
static String DEFAULT_RESPONSE_EVENT
           
protected static org.apache.log4j.Logger logger
           
static int MAX_DISPATCH_QUEUE_DEPTH
           
static int MAX_POLY_CHAIN_DEPTH
           
 
Constructor Summary
DefaultEventDispatcher()
           
 
Method Summary
protected  void dispatch(int dispatchPhase, EventBroker eb, EventContext context, DefaultDispatchQueue eventQueue)
          Dispatch a queue of events
 void dispatchEvent(EventBroker eb, EventContext context)
          Dispatch a queue of events.
protected  List findListeners(BaseEvent event, EventBroker eb)
          Find the listeners for an event
protected  List getEventChain(BaseEvent event)
          Get the event chain for a particular event.
protected  void notifyListeners(BaseEvent event, List list, EventContext context)
          Actually dispatch the specific event to the list of listener factories
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_RESPONSE_EVENT

public static final String DEFAULT_RESPONSE_EVENT
See Also:
Constant Field Values

MAX_POLY_CHAIN_DEPTH

public static int MAX_POLY_CHAIN_DEPTH

MAX_DISPATCH_QUEUE_DEPTH

public static int MAX_DISPATCH_QUEUE_DEPTH

logger

protected static final org.apache.log4j.Logger logger
Constructor Detail

DefaultEventDispatcher

public DefaultEventDispatcher()
Method Detail

dispatchEvent

public void dispatchEvent(EventBroker eb,
                          EventContext context)
                   throws EventException

Dispatch a queue of events. The incoming queue must be an instance of DefaultDispatchQueue or an EventException will be thrown. This means that if you're going to provide a custom event broker, you may need to provide a custom dispatcher as well.

Note: this is really a repeating 2 Phase approach -- we will continue to do dispatch until there are no remaining unhandled events in the queue. This allows response event handlers to add non-response events back into the queue (ie. for logging purposes, etc) and we are guaranteed that they will get dispatched (barring an exception getting thrown)

Specified by:
dispatchEvent in interface EventDispatcher
Parameters:
eb - the event broker to be used to match events to listeners
context - the event context for this whole dispatch cycle
Throws:
EventException

dispatch

protected void dispatch(int dispatchPhase,
                        EventBroker eb,
                        EventContext context,
                        DefaultDispatchQueue eventQueue)
                 throws EventException
Dispatch a queue of events

Parameters:
dispatchPhase - an internal flag to determine whether we're in REQ or RESP phase
eb - the event broker to be used to match events to listeners
context - the event context for this whole dispatch cycle
eventQueue - the default dispatch queue
Throws:
EventException

getEventChain

protected List getEventChain(BaseEvent event)
                      throws EventException
Get the event chain for a particular event. If this event implements Polymorphic, this will be a list of events starting from the highest object in the chain that implements polymorphic all the way down to the actual event we pass in. If it doesn't implement polymorphic, it'll just be the incoming event... Note: this method can probably be optimized to only instantiate when there are actually listeners...

Parameters:
event - the base event for which we're trying to find an event chain
Returns:
list of events in the chain
Throws:
EventException

findListeners

protected List findListeners(BaseEvent event,
                             EventBroker eb)
Find the listeners for an event

Parameters:
event - the base event for which we're trying to find listeners
eb - the event broker to use when looking up listeners

notifyListeners

protected void notifyListeners(BaseEvent event,
                               List list,
                               EventContext context)
                        throws EventException
Actually dispatch the specific event to the list of listener factories

Parameters:
event - the event to be dispatched
list - the list of listeners to be notified for this particular event
context - the event context containing event, queue, and, sometimes, http information
Throws:
EventException


Copyright © 2003 BarracudaMVC.org All Rights Reserved.