org.d_haven.mpool

Class AbstractPool

public abstract class AbstractPool extends 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.
Field Summary
ObjectFactorym_factory
Collectionm_listeners
Constructor Summary
protected AbstractPool(ObjectFactory factory)
Initialize this instance of an AbstractPool using the supplied ObjectFactory.
Method Summary
Objectacquire()
Acquire an instance of the pooled object.
voidaddPoolListener(PoolListener listener)
Add a pool listener to the pool.
protected voiddisposeInstance(Object pooledObject)
Dispose an instance of a pooled object.
protected abstract ObjectdoAcquire()
Perform the actual work of acquiring the object from the pool.
protected abstract booleandoRelease(Object pooledObject)
Perform the actual logic to release the pooled object back to the pool.
protected voidfireAquiredEvent(Object pooledObject)
Fire the objectAquired event to all the listeners.
protected voidfireCreatedEvent(Object pooledObject)
Fire the objectCreated event to all the listeners.
protected voidfireCreationFailedEvent(Exception exception)
Fire the objectCreationFailed event to all the listeners.
protected voidfireDisposedEvent(Object pooledObject)
Fire the objectDisposed event to all the listeners.
protected voidfireDisposeFailedEvent(Object pooledObject, Exception exception)
Fire the PoolListener event to all the listeners.
protected voidfireReleasedEvent(Object pooledObject)
Fire the objectReleased event to all the listeners.
CollectiongetPoolListeners()
Get the list of pool listeners.
IteratorlistenerIterator()
ObjectnewInstance()
Create a new instance of the object being pooled.
voidrelease(Object pooledObject)
Release the instance of the pooled object.
voidremovePoolListener(PoolListener listener)
Remove a pool listener from the pool.

Field Detail

m_factory

private final ObjectFactory m_factory

m_listeners

private final Collection m_listeners

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

acquire

public Object acquire()
Acquire an instance of the pooled object.

Returns: the pooled Object instance

Throws: Exception if the Pool is not able to return an object.

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.

Parameters: listener the listener to add.

disposeInstance

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

Parameters: pooledObject the object to dispose of

doAcquire

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

Returns: the acquired object or null if there are none left

Throws: Exception if there was a problem acquiring the object

doRelease

protected abstract boolean doRelease(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

fireAquiredEvent

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

Parameters: pooledObject the object acquired from the pool

fireCreatedEvent

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

Parameters: pooledObject the object created

fireCreationFailedEvent

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

Parameters: exception the exception causing the failure

fireDisposedEvent

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

Parameters: pooledObject the object disposed

fireDisposeFailedEvent

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

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

fireReleasedEvent

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

Parameters: pooledObject the object released to the pool

getPoolListeners

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

Returns: all the listeners

listenerIterator

private Iterator listenerIterator()

newInstance

public Object newInstance()
Create a new instance of the object being pooled.

Returns: the pooled Object instance

Throws: Exception if the instance cannot be created

release

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

Parameters: pooledObject The pooled object to release to the pool.

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.

Parameters: listener the listener to remove