org.logicalcobwebs.proxool.util
Class AbstractListenerContainer

java.lang.Object
  extended byorg.logicalcobwebs.proxool.util.AbstractListenerContainer
All Implemented Interfaces:
ListenerContainerIF
Direct Known Subclasses:
CompositeConfigurationListener, CompositeConnectionListener, CompositeProxoolListener, CompositeStateListener, CompositeStatisticsListener, CompositeTestListener

public abstract class AbstractListenerContainer
extends java.lang.Object
implements ListenerContainerIF

Implementation of ListenerContainerIF that uses a reads/write lock to handle concurrency in a safe and fast way.

The registered listeners are offered to subclasses through the protected getListenerIterator() method. This method aquires a read lock before it returns the iterator, but it is the subclass responsibility to release this lock by calling the releaseReadLock() method. Failing to do this will prevent any more listeneres to be added or removed. Your code sould look like this:


     Iterator listenerIterator = null;
     try {
         listenerIterator = getListenerIterator();
         if (listenerIterator != null) {
            // ... Iterate through the listeners and notify them
         }
     } catch (InterruptedException e) {
         LOG.error("Tried to aquire read lock for " + MyClass.class.getName()
             + " iterator but was interrupted.");
     } finally {
         releaseReadLock();
     }
 

Since:
Proxool 0.7
Version:
$Revision: 1.7 $, $Date: 2003/03/11 00:12:11 $
Author:
Christian Nedregaard (christian_nedregaard@email.com), $Author: billhorsman $ (current maintainer)

Field Summary
private  java.util.List listeners
           
(package private) static Log LOG
           
private  WriterPreferenceReadWriteLock readWriteLock
           
 
Constructor Summary
AbstractListenerContainer()
           
 
Method Summary
 void addListener(java.lang.Object listener)
          Add a listener to this container.
protected  java.util.Iterator getListenerIterator()
          Get an iterator containing the listeners in this container.
 boolean isEmpty()
          Get wether this container is empty or not.
protected  void releaseReadLock()
          Release the read lock aquired by the getListenerIterator() method.
 boolean removeListener(java.lang.Object listener)
          Remove a listener from this container.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOG

static final Log LOG

listeners

private java.util.List listeners

readWriteLock

private WriterPreferenceReadWriteLock readWriteLock
Constructor Detail

AbstractListenerContainer

public AbstractListenerContainer()
Method Detail

addListener

public void addListener(java.lang.Object listener)
Description copied from interface: ListenerContainerIF
Add a listener to this container.

Specified by:
addListener in interface ListenerContainerIF
Parameters:
listener - the listener to add.
See Also:
ListenerContainerIF.addListener(Object)

removeListener

public boolean removeListener(java.lang.Object listener)
Description copied from interface: ListenerContainerIF
Remove a listener from this container.

Specified by:
removeListener in interface ListenerContainerIF
Parameters:
listener - the listener to be removed.
Returns:
wether the listnener was found and removed or not.
See Also:
ListenerContainerIF.removeListener(Object)

getListenerIterator

protected java.util.Iterator getListenerIterator()
                                          throws java.lang.InterruptedException
Get an iterator containing the listeners in this container. Will return null if this container is empty.

Important: See class documentation regarding releasing of read lock.

Returns:
an iterator containing the listeners in this container or null if it is empty.
Throws:
java.lang.InterruptedException - if the read lock can't be obtained.

releaseReadLock

protected void releaseReadLock()
Release the read lock aquired by the getListenerIterator() method.


isEmpty

public boolean isEmpty()
Description copied from interface: ListenerContainerIF
Get wether this container is empty or not.

Specified by:
isEmpty in interface ListenerContainerIF
Returns:
wether this container is empty or not.
See Also:
ListenerContainerIF.isEmpty()