org.jcreme.pool
Class ObjectPool

java.lang.Object
  extended byorg.jcreme.pool.ObjectPool
Direct Known Subclasses:
WrappedConnectionPool

public abstract class ObjectPool
extends java.lang.Object

This class represents a pool of objects. That is a number of objects that are shared in an application as a limited resource. This is a lazy implementation, it builds the pooled objects as needed through the use of the buildObject method. This class must be derived to provide an actual implementation of the buildNew method.

Version:
$Revision: 1.3 $
Author:
$Author: dbregeon $

Constructor Summary
protected ObjectPool(int minSize, int maxSize)
          Creates a new instance of ObjectPool If the minSize is greater than the maxSize, it is reduced to the maxSize.
 
Method Summary
protected abstract  java.lang.Object buildNew()
          This method enabled to build a new object when needed.
protected  java.util.Collection getFreeObjects()
          Gives access to the freeObjects Stack.
protected  java.lang.Object getObject()
          This method enables to get an object from the ObjectPool.
protected  java.lang.Object getObject(int waitingPeriod)
          This method enables to get an object from the ObjectPool.
protected  java.lang.Object getObjectFromFree()
          This method enables to retrieve an object from the freeObjects stack.
protected  java.lang.Object getObjectFromFree(int waitingPeriod)
          This method enables to retrieve an object from the freeObjects stack.
protected  java.lang.Object getObjectFromUsed(java.lang.Object o)
          This method enables to remove an object from the usedObjects list.
protected  java.util.Collection getUsedObjects()
          Gives access to the usedObjects Vector.
protected  void initPool()
          This method makes as many calls to buildNew as minSize.
protected  void putInFreeObjects(java.lang.Object o)
          This method enabled to return an object to the freeObjects stack.
protected  void putInUsedObjects(java.lang.Object o)
          This method enabled to register an object to the usedObjects list.
protected  void releaseObject(java.lang.Object obj)
          This method enables to return an object to the ObjectPool.
protected abstract  void removeOld(java.lang.Object obj)
          This method enabled to remove an old object when needed.
 void setMaximumSize(int maxSize)
          This method enables to change the maximum size of the ConnectionPool.
 void setMinimumSize(int minSize)
          This method enables to change the minimum size of the ConnectionPool.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ObjectPool

protected ObjectPool(int minSize,
                     int maxSize)
Creates a new instance of ObjectPool If the minSize is greater than the maxSize, it is reduced to the maxSize. The constructor builds as many objects as minSize through the initPool method.

Parameters:
minSize - the initial size of the pool.
maxSize - the maximum size of the pool.
Method Detail

initPool

protected final void initPool()
This method makes as many calls to buildNew as minSize. It is used to populate the pool.


buildNew

protected abstract java.lang.Object buildNew()
This method enabled to build a new object when needed. Any parameters needed should be provided by the actual ObjectPool implementation.

Returns:
a newly constructed pool element.

removeOld

protected abstract void removeOld(java.lang.Object obj)
This method enabled to remove an old object when needed. It enables to free resources when an object is permanently removed from the ObjectPool.

Parameters:
obj - the object to remove.

getObjectFromFree

protected java.lang.Object getObjectFromFree()
                                      throws java.lang.InterruptedException
This method enables to retrieve an object from the freeObjects stack. If the stack is empty and the maxSize is not exceeded, it builds a new object using the buildNew method. If the stack is empty and the maxSize was reached, it waits for an object to be released. The call is then blocked indefinitely.

Returns:
an object freshly removed from the freeObjects stack.
Throws:
java.lang.InterruptedException - if the wait for a free object was interrupted.

getObjectFromFree

protected java.lang.Object getObjectFromFree(int waitingPeriod)
                                      throws java.lang.InterruptedException
This method enables to retrieve an object from the freeObjects stack. If the stack is empty and the maxSize is not exceeded, it builds a new object using the buildNew method. If the stack is empty and the maxSize was reached, it waits for an object to be released for the given time.

Parameters:
waitingPeriod - the time the method is allowed to wait for an object from the pool.
Returns:
an object freshly removed from the freeObjects stack.
Throws:
java.lang.InterruptedException - if the wait for a free object was interrupted.

getObjectFromUsed

protected java.lang.Object getObjectFromUsed(java.lang.Object o)
This method enables to remove an object from the usedObjects list.

Parameters:
o - the object to remove from the usedObjects.
Returns:
the object actually removed from the list. This is to prevent objects from being introduced in the ObjectPool while they may have been created outside.

putInFreeObjects

protected void putInFreeObjects(java.lang.Object o)
This method enabled to return an object to the freeObjects stack.

Parameters:
o - the object to return to the stack.

putInUsedObjects

protected void putInUsedObjects(java.lang.Object o)
This method enabled to register an object to the usedObjects list.

Parameters:
o - the object to add to the usedObjects list.

getObject

protected java.lang.Object getObject()
This method enables to get an object from the ObjectPool. It will block indefinitely while waiting for a free object.

Returns:
an object from the ObjectPool. Null if the method was interrupted.

getObject

protected java.lang.Object getObject(int waitingPeriod)
This method enables to get an object from the ObjectPool. It will block for the given time while waiting for a free object.

Parameters:
waitingPeriod - the number of millisecond the method will wait for an object to be freed.
Returns:
an object from the ObjectPool. Null if the method was interrupted.

releaseObject

protected void releaseObject(java.lang.Object obj)
This method enables to return an object to the ObjectPool.

Parameters:
obj - the object to release.

getFreeObjects

protected java.util.Collection getFreeObjects()
Gives access to the freeObjects Stack.

Returns:
the freeObjects.

getUsedObjects

protected java.util.Collection getUsedObjects()
Gives access to the usedObjects Vector.

Returns:
the usedObjects.

setMinimumSize

public void setMinimumSize(int minSize)
This method enables to change the minimum size of the ConnectionPool. Sizes lesser than 0 will be ignored.

Parameters:
minSize - the new minimum size to apply.

setMaximumSize

public void setMaximumSize(int maxSize)
This method enables to change the maximum size of the ConnectionPool. If the size is lower than the minimum size, it will be ignored.

Parameters:
maxSize - the new maximum size to apply.


Copyright 2006 null. All Rights Reserved.