org.d_haven.mpool
Class AbstractPool

java.lang.Object
  extended byorg.d_haven.mpool.AbstractPool
All Implemented Interfaces:
Pool
Direct Known Subclasses:
FixedSizePool, VariableSizePool

public abstract class AbstractPool
extends java.lang.Object
implements Pool

The AbstractPool is the base class for all the pool types. It handles the interaction with the pool listeners to make it easier to focus on the core issues of storing the pooled objects in the final implementations.


Constructor Summary
protected AbstractPool(ObjectFactory factory)
          Initialize this instance of an AbstractPool using the supplied ObjectFactory.
 
Method Summary
 java.lang.Object acquire()
          Acquire an instance of the pooled object.
 void addPoolListener(PoolListener listener)
          Add a pool listener to the pool.
protected  void disposeInstance(java.lang.Object pooledObject)
          Dispose an instance of a pooled object.
protected abstract  java.lang.Object doAcquire()
          Perform the actual work of acquiring the object from the pool.
protected abstract  boolean doRelease(java.lang.Object pooledObject)
          Perform the actual logic to release the pooled object back to the pool.
protected  void fireAquiredEvent(java.lang.Object pooledObject)
          Fire the PoolListener.objectAquired(Object) event to all the listeners.
protected  void fireCreatedEvent(java.lang.Object pooledObject)
          Fire the PoolListener.objectCreated(Object) event to all the listeners.
protected  void fireCreationFailedEvent(java.lang.Exception exception)
          Fire the PoolListener.objectCreationFailed(Exception) event to all the listeners.
protected  void fireDisposedEvent(java.lang.Object pooledObject)
          Fire the PoolListener.objectDisposed(Object) event to all the listeners.
protected  void fireDisposeFailedEvent(java.lang.Object pooledObject, java.lang.Exception exception)
          Fire the PoolListener.objectDisposalFailed(Object, Exception) event to all the listeners.
protected  void fireReleasedEvent(java.lang.Object pooledObject)
          Fire the PoolListener.objectReleased(Object) event to all the listeners.
 java.util.Collection getPoolListeners()
          Get the list of pool listeners.
 java.lang.Object newInstance()
          Create a new instance of the object being pooled.
 void release(java.lang.Object pooledObject)
          Release the instance of the pooled object.
 void removePoolListener(PoolListener listener)
          Remove a pool listener from the pool.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractPool

protected AbstractPool(ObjectFactory factory)
Initialize this instance of an AbstractPool using the supplied ObjectFactory.

Parameters:
factory - the ObjectFactory used to create new instances.
Method Detail

addPoolListener

public void addPoolListener(PoolListener listener)
Add a pool listener to the pool. If you add a listener twice it will be notified twice and you will have to remove it twice.

Specified by:
addPoolListener in interface Pool
Parameters:
listener - the listener to add.

removePoolListener

public void removePoolListener(PoolListener listener)
Remove a pool listener from the pool. If you add a listener twice it will be notified twice and you will have to remove it twice.

Specified by:
removePoolListener in interface Pool
Parameters:
listener - the listener to remove

getPoolListeners

public java.util.Collection getPoolListeners()
Get the list of pool listeners.

Returns:
all the listeners

acquire

public java.lang.Object acquire()
                         throws java.lang.Exception
Acquire an instance of the pooled object.

Specified by:
acquire in interface Pool
Returns:
the pooled Object instance
Throws:
java.lang.Exception - if the Pool is not able to return an object.

fireCreationFailedEvent

protected void fireCreationFailedEvent(java.lang.Exception exception)
Fire the PoolListener.objectCreationFailed(Exception) event to all the listeners.

Parameters:
exception - the exception causing the failure

fireAquiredEvent

protected void fireAquiredEvent(java.lang.Object pooledObject)
Fire the PoolListener.objectAquired(Object) event to all the listeners.

Parameters:
pooledObject - the object acquired from the pool

fireReleasedEvent

protected void fireReleasedEvent(java.lang.Object pooledObject)
Fire the PoolListener.objectReleased(Object) event to all the listeners.

Parameters:
pooledObject - the object released to the pool

fireDisposeFailedEvent

protected void fireDisposeFailedEvent(java.lang.Object pooledObject,
                                      java.lang.Exception exception)
Fire the PoolListener.objectDisposalFailed(Object, Exception) event to all the listeners.

Parameters:
pooledObject - the object disposed
exception - the exception thrown during disposal

fireCreatedEvent

protected void fireCreatedEvent(java.lang.Object pooledObject)
Fire the PoolListener.objectCreated(Object) event to all the listeners.

Parameters:
pooledObject - the object created

doAcquire

protected abstract java.lang.Object doAcquire()
                                       throws java.lang.Exception
Perform the actual work of acquiring the object from the pool.

Returns:
the acquired object or null if there are none left
Throws:
java.lang.Exception - if there was a problem acquiring the object

release

public void release(java.lang.Object pooledObject)
Release the instance of the pooled object.

Specified by:
release in interface Pool
Parameters:
pooledObject - The pooled object to release to the pool.

disposeInstance

protected void disposeInstance(java.lang.Object pooledObject)
Dispose an instance of a pooled object. This is typically called during a ManagablePool.shrink(int) call.

Parameters:
pooledObject - the object to dispose of

fireDisposedEvent

protected void fireDisposedEvent(java.lang.Object pooledObject)
Fire the PoolListener.objectDisposed(Object) event to all the listeners.

Parameters:
pooledObject - the object disposed

doRelease

protected abstract boolean doRelease(java.lang.Object pooledObject)
Perform the actual logic to release the pooled object back to the pool.

Parameters:
pooledObject - the object to release
Returns:
true if we also need to dispose of the object

newInstance

public java.lang.Object newInstance()
                             throws java.lang.Exception
Create a new instance of the object being pooled.

Returns:
the pooled Object instance
Throws:
java.lang.Exception - if the instance cannot be created


Copyright © 2004-2005 D-Haven.org. All Rights Reserved.