dsto.dfc.util
Class WeakEventListener

java.lang.Object
  extended by java.lang.ref.Reference<T>
      extended by java.lang.ref.WeakReference
          extended by dsto.dfc.util.WeakEventListener
All Implemented Interfaces:
InvocationHandler

public class WeakEventListener
extends WeakReference
implements InvocationHandler

A 'weak' event listener proxy that does not preclude garbage collection of the listener. Similar in concept to a weak reference, this class acts as a proxy to a contained listener. The contained listener is held with a weak reference, allowing it to be garbage collected if the listener registration is the only thing pointing to it.

This can be especially useful when transient classes (such as commands) need to listen to global or static classes.

Example - add a property change listener to a bean:


 WeakEventListener.createListener
  (PropertyChangeListener.class, bean, listener);

 

Version:
$Revision$
Author:
Matthew
See Also:
Beans.addListener(java.lang.Class, java.lang.Object, java.lang.Object)

Field Summary
protected static dsto.dfc.util.WeakEventListener.CleanupThread cleanupThread
          Cleans up GC's WeakEventListener's.
protected  Class listenerInterface
          The event listener interface (eg PropertyChangeListener).
protected  Object proxy
           
protected  Object source
          The thing being listened to.
 
Constructor Summary
WeakEventListener(Class listenerInterface, Object source, Object listener)
          Create a proxy weak event listener.
 
Method Summary
static WeakEventListener createListener(Class listenerInterface, Object source, Object listener)
          Shortcut to automatically create a WeakEventListener instance and register its proxy with the event source.
protected  Object createProxy()
           
 void dispose()
          Manually dispose the obvject and remove the proxy listener.
protected static dsto.dfc.util.WeakEventListener.CleanupThread getCleanupThread()
          Get or create the cleanup thread instance.
 Object getProxy()
          The actual proxy created to receive calls to the listener interface.
 Object invoke(Object proxyInstance, Method method, Object[] args)
           
 
Methods inherited from class java.lang.ref.Reference
clear, enqueue, get, isEnqueued
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

cleanupThread

protected static dsto.dfc.util.WeakEventListener.CleanupThread cleanupThread
Cleans up GC's WeakEventListener's.


source

protected Object source
The thing being listened to.


listenerInterface

protected Class listenerInterface
The event listener interface (eg PropertyChangeListener).


proxy

protected Object proxy
Constructor Detail

WeakEventListener

public WeakEventListener(Class listenerInterface,
                         Object source,
                         Object listener)
Create a proxy weak event listener. Use getProxy() to retrieve the actual proxy instance of listenerInterface. Most clients will use createListener(java.lang.Class, java.lang.Object, java.lang.Object) instead to automatically create a WeakEventListener instance and register its proxy with the event source.

Parameters:
listenerInterface - The event listener interface (eg PropertyChangeListener).
source - The event source (ie the thing being listened to).
listener - The actual listener instance (must implement the listener interface).
Method Detail

createProxy

protected Object createProxy()

dispose

public void dispose()
Manually dispose the obvject and remove the proxy listener.


createListener

public static WeakEventListener createListener(Class listenerInterface,
                                               Object source,
                                               Object listener)
Shortcut to automatically create a WeakEventListener instance and register its proxy with the event source.

Parameters:
listenerInterface - The event listener interface (eg PropertyChangeListener).
source - The event source (ie the thing being listened to).
listener - The actual listener instance (must implement the listener interface).
Returns:
The weak event listener, or null if source does not support the event type.

getProxy

public Object getProxy()
The actual proxy created to receive calls to the listener interface.


getCleanupThread

protected static dsto.dfc.util.WeakEventListener.CleanupThread getCleanupThread()
Get or create the cleanup thread instance.


invoke

public Object invoke(Object proxyInstance,
                     Method method,
                     Object[] args)
              throws Throwable
Specified by:
invoke in interface InvocationHandler
Throws:
Throwable


Copyright © 2008 Commonwealth of Australia