net.larsan.urd.util
Class EventInvoker

java.lang.Object
  |
  +--net.larsan.urd.util.EventInvoker
All Implemented Interfaces:
java.lang.reflect.InvocationHandler

public class EventInvoker
extends java.lang.Object
implements java.lang.reflect.InvocationHandler

An event invocation handler is a generic broadcatser for events to use with a java.lang.reflect.Proxy. An invocation handler does not check any validity on listener registration but will report errors using an error handler during runtime.

This class should be used as a proxy for the listeners. The using code can get hold of the event invoker through the proxys getInvocationHandler method like so:

     Proxy prox = (Proxy)Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { listenerClass }, new EventInvoker()); 
     EventInvoker inv = (EventInvoker)prx.getInvocationHandler(prox);
 

This class will try to route incoming events to all it's listeners, should this behaviour need changing a subclass can override the empty acceptBroadcast method to filter wich listeners to target.

This class is synchronized so that listener add/remove does not interfere with ongoing broadcasting.

Version:
Alpha-0.1 / 2002-10-06 21:37
Author:
Lars J. Nilsson

Constructor Summary
EventInvoker()
          Create invocation handler.
EventInvoker(int size)
          Create invocation handler with a initial size.
 
Method Summary
 void addListener(java.lang.Object listener)
          Add a listener to this event broadcaster.
 void clear()
          Clear this handler of all listeners.
 java.lang.Object invoke(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args)
          Handle fired event, this broadcasts the event to all listeners.
 void removeListener(java.lang.Object listener)
          Remove a listerer from this broadcaster.
 void setErrorHandler(ErrorHandler handler)
          Set the error handler.
 int size()
          Get the current number of listeners registered with this invoker
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

EventInvoker

public EventInvoker(int size)
Create invocation handler with a initial size. The handler uses a list for storage of the listeners. The size argument must not be less than zero.

Parameters:
size - Initial size of listener collection

EventInvoker

public EventInvoker()
Create invocation handler. The handler uses a list for storage of the listeners and will use a default size of 10.

Method Detail

addListener

public void addListener(java.lang.Object listener)
Add a listener to this event broadcaster. The invoker will check for dupklicate listeners by calls to equals method.

Parameters:
listener - listener object to add

removeListener

public void removeListener(java.lang.Object listener)
Remove a listerer from this broadcaster. This removes an object where the equals mthoed returns true used with the parameter.

Parameters:
listener - listener to remove

size

public int size()
Get the current number of listeners registered with this invoker

Returns:
The number of registered listeners

clear

public void clear()
Clear this handler of all listeners.


setErrorHandler

public void setErrorHandler(ErrorHandler handler)
Set the error handler.

Parameters:
handler - Error handler for this broadcaster

invoke

public java.lang.Object invoke(java.lang.Object proxy,
                               java.lang.reflect.Method method,
                               java.lang.Object[] args)
Handle fired event, this broadcasts the event to all listeners. Eventual errors will be reported to the error handler if there is one.

Specified by:
invoke in interface java.lang.reflect.InvocationHandler
Parameters:
proxy - Proxy which the event invocation originated on
method - Event method that was called
args - Event method parameter array
Returns:
Null