de.fhg.igd.event
Class EventReflectorImpl

java.lang.Object
  extended by de.fhg.igd.event.EventReflectorImpl
All Implemented Interfaces:
EventReflector

public class EventReflectorImpl
extends Object
implements EventReflector

Reflects event notifications to registered listeners. The listeners are referenced by means of soft references. Soft references are eligible for garbage collection unless a strong reference points to the listener. Depending on the policy for this class, listeners are also wrapped into either a AsyncListener or a TrustedListener. Both classes spawn a thread that handles distribution of event notifications to the wrapped listener. Therefor, a strong reference to the wrapper as well as to the wrapped listener remains throughout the lifetime of the wrapper thread.

Wrapper threads are started at the time of registering a listener, and they are started in the default thread group (the caller's thread group in general).

Hence, agents that register listeners may safely assume that listeners they register persist during the agent's lifetime. Wrapper threads can be terminated by interrupting them. In that case, the wrapper is removed and garbage collected as soon as there are no more references to it.

Version:
"$Id: EventReflectorImpl.java 1913 2007-08-08 02:41:53Z jpeters $"
Author:
Volker Roth

Field Summary
private  List targets_
          The private list that is used to keep references to the listeners.
 
Constructor Summary
EventReflectorImpl()
          Creates an instance.
 
Method Summary
 TrustedListener addListener(Listener listener)
          Adds a listener.
 void notifyListeners(Object trigger)
          Notifies all registered listeners which are alive of the given trigger event.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

targets_

private List targets_
The private list that is used to keep references to the listeners.

Constructor Detail

EventReflectorImpl

public EventReflectorImpl()
Creates an instance.

Method Detail

addListener

public TrustedListener addListener(Listener listener)
Adds a listener. The listener will receive all events sent to this reflector. The listener is wrapped into a TrustedListener before it is registered. The TrustedListener thread is started immediately as a daemon thread, and the current thread yields in order to give the VM a chance to set up the new thread. References to TrustedListeners are kept wrapped in WeakReferences. As long as the thread runs, the listeners are safe from garbage collection even if there are no external references to the listeners. This ensures that listeners are cleared eventually when agents terminate.

TrustedListeners can be terminated by interrupting them, or by sending a null notification.

Specified by:
addListener in interface EventReflector
Parameters:
listener - The listener to be registered.
Returns:
The TrustedListener that wraps around the given listener.
Throws:
NullPointerException - if listener is null.

notifyListeners

public void notifyListeners(Object trigger)
Notifies all registered listeners which are alive of the given trigger event. This method also removes listeners which are not alive anymore.

Specified by:
notifyListeners in interface EventReflector
Parameters:
trigger - The object that is distributed to the registered listeners.
Throws:
NullPointerException - if the trigger is null.


Copyright © Fraunhofer Gesellschaft. All Rights Reserved.