org.sadun.util.pool
Class ObjectPool

java.lang.Object
  extended byorg.sadun.util.pool.ObjectPool
All Implemented Interfaces:
java.io.Serializable
Direct Known Subclasses:
ObjectPool

public class ObjectPool
extends java.lang.Object
implements java.io.Serializable

An object pool, which holds n identical copies of an object and allows access and release of each of them.

An object can be created by providing its class - in which case the default constructor will be used to create the instances; or by providing the construction parameters as an object array.

Optionally, after creation, a Setup object can be used to differentiate or further set up the created object.

This version supports Enterprise Java Beans creation via a JNDI name and home interface

Version:
2.2
Author:
C. Sadun
See Also:
Serialized Form

Nested Class Summary
static class ObjectPool.BaseFactory
          A base implementation of ExtendedObjectPool.Factory relying on reflection, holding a Class object, an optional parameter array and an optional Setup object.
static interface ObjectPool.Factory
          If an object to be pooled can't be constructed directly by invoking a constructor, a Factory can be provided to do the construction job.
static class ObjectPool.ObjectFactory
          An ExtendedObjectPool.Factory implementation which uses reflection to create instances of a certain class.
static class ObjectPool.ObjectPoolException
          An exception thrown in case of pooling operation errors.
protected static class ObjectPool.PooledObjectWrapper
           
 
Field Summary
protected  java.util.Set free
           
protected  ObjectPool.PooledObjectWrapper[] pool
           
protected  java.util.Set used
           
 
Constructor Summary
ObjectPool(int n, java.lang.Class cls)
          Create a pool of n object of the given class using the default constructor.
ObjectPool(int n, java.lang.Class cls, java.lang.Object[] params)
          Create a pool of n object of the given class using the given construction parameters.
ObjectPool(int n, java.lang.Class cls, java.lang.Object[] params, Setup ps)
          Create a pool of n object of the given class using the given construction parameters.
ObjectPool(int n, ObjectPool.Factory factory)
          Create a pool of n objects using the given factory
ObjectPool(int n, java.lang.String clsName)
          Create a pool of n object of the given class (by name) using the default constructor.
ObjectPool(int n, java.lang.String clsName, java.lang.Object[] params)
          Create a pool of n object of the given class (by name) using the given construction parameters.
ObjectPool(int n, java.lang.String clsName, java.lang.Object[] params, Setup ps)
          Create a pool of n object of the given class (by name) using the given construction parameters.
 
Method Summary
 java.lang.Object acquire()
          Attempt to acquire an object.
 java.lang.Object acquire(boolean waitIfUnavailable)
          Attempt to acquire an object.
 java.lang.Object acquire(int i)
          Attempt to acquire the i-th object.
 void destroy(boolean waitForReleasedObjects)
          Destroys all the objects in the pool by invoking the Factory.destroy() method on the pool's object factory.
protected  ObjectPool.Factory getFactory()
          Returns the factory.
 int getFreeCount()
          Return the number of available objects in the pool
 java.lang.Class getObjectType()
          Return the object type pooled by this pool
 int getSize()
          Return the size of the pool
 int getUsedCount()
          Return the number of used objects in the pool
static ObjectPool newPool(int poolSize, java.lang.String clsName)
          A convenience method to create a new pool of objects of a given class, by name, using their default constructor.
static ObjectPool newPool(int poolSize, java.lang.String clsName, java.lang.Object[] params)
          A convenience method to create a new pool of objects of a given class, by name, using the given parameters.
 void release(int i)
          Releases the i-th object, notifying the waiting thread (if any) that one pooled object has become available.
 void release(java.lang.Object obj)
          Release an object, notifying waiting thread (if any) that one pooled object has become available.
 void release(java.lang.Object obj, boolean releaseWaitingCalls)
          Release an object.
 java.lang.Object renew(java.lang.Object obj)
          Renew one object in the pool.
protected  void setFactory(ObjectPool.Factory factory)
          Sets the factory.
 java.lang.String toString()
          Return a string description of the pool
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

free

protected java.util.Set free

used

protected java.util.Set used

pool

protected ObjectPool.PooledObjectWrapper[] pool
Constructor Detail

ObjectPool

public ObjectPool(int n,
                  ObjectPool.Factory factory)
Create a pool of n objects using the given factory


ObjectPool

public ObjectPool(int n,
                  java.lang.String clsName,
                  java.lang.Object[] params,
                  Setup ps)
           throws java.lang.ClassNotFoundException
Create a pool of n object of the given class (by name) using the given construction parameters.

If some post-construction setup is needed, it can be provided as an Setup object.

Parameters:
n - the size of the pool
clsName - the name of the class of the objects to pool
params - the construction parameters, or null
Throws:
java.lang.ClassNotFoundException - if the given class name cannot be resolved

ObjectPool

public ObjectPool(int n,
                  java.lang.String clsName,
                  java.lang.Object[] params)
           throws java.lang.ClassNotFoundException
Create a pool of n object of the given class (by name) using the given construction parameters.

Parameters:
n - the size of the pool
clsName - the name of the class of the objects to pool
params - the construction parameters, or null
Throws:
java.lang.ClassNotFoundException - if the given class name cannot be resolved

ObjectPool

public ObjectPool(int n,
                  java.lang.String clsName)
           throws java.lang.ClassNotFoundException
Create a pool of n object of the given class (by name) using the default constructor.

Parameters:
n - the size of the pool
clsName - the name of the class of the objects to pool
Throws:
java.lang.ClassNotFoundException - if the given class name cannot be resolved

ObjectPool

public ObjectPool(int n,
                  java.lang.Class cls,
                  java.lang.Object[] params,
                  Setup ps)
Create a pool of n object of the given class using the given construction parameters.

If some post-construction setup is needed, it can be provided as an Setup object.

Parameters:
n - the size of the pool
cls - the class of the objects to pool
params - the construction parameters, or null

ObjectPool

public ObjectPool(int n,
                  java.lang.Class cls,
                  java.lang.Object[] params)
Create a pool of n object of the given class using the given construction parameters.

Parameters:
n - the size of the pool
cls - the class of the objects to pool
params - the construction parameters, or null

ObjectPool

public ObjectPool(int n,
                  java.lang.Class cls)
Create a pool of n object of the given class using the default constructor.

Parameters:
n - the size of the pool
cls - the class of the objects to pool
Method Detail

getFreeCount

public int getFreeCount()
Return the number of available objects in the pool

Returns:
the number of available objects in the pool

getUsedCount

public int getUsedCount()
Return the number of used objects in the pool

Returns:
the number of used objects in the pool

getSize

public int getSize()
Return the size of the pool

Returns:
the size of the pool

acquire

public java.lang.Object acquire(boolean waitIfUnavailable)
Attempt to acquire an object.

Parameters:
waitIfUnavailable - if true, in case all the pooled objects are used, the call will block until an object is released. If false, in the same condition the method returns null.
Returns:
a pooled object

acquire

public java.lang.Object acquire()
Attempt to acquire an object.

If there aren't any objects available, this method blocks until one becomes available.

Returns:
a pooled object

acquire

public java.lang.Object acquire(int i)
Attempt to acquire the i-th object.

If the object is not available, this method blocks until the object becomes available.

Returns:
a pooled object

release

public void release(int i)
Releases the i-th object, notifying the waiting thread (if any) that one pooled object has become available.


release

public void release(java.lang.Object obj,
                    boolean releaseWaitingCalls)
Release an object.

Parameters:
releaseWaitingCalls - if true, the method notifies waiting objects that one pooled object has become available.

release

public void release(java.lang.Object obj)
Release an object, notifying waiting thread (if any) that one pooled object has become available.


renew

public java.lang.Object renew(java.lang.Object obj)
Renew one object in the pool.

A new instance is created substituting the passed object in the pool, and the new instance is returned. The object is released() but any thread waiting on that object shifts waiting for another object.


toString

public java.lang.String toString()
Return a string description of the pool

Returns:
a string description of the pool

newPool

public static ObjectPool newPool(int poolSize,
                                 java.lang.String clsName)
                          throws ObjectPool.ObjectPoolException
A convenience method to create a new pool of objects of a given class, by name, using their default constructor.

Parameters:
poolSize - the size of the pool
clsName - the name of the class of the pooled objects
Throws:
ObjectPool.ObjectPoolException - if the pool cannot be initialized

newPool

public static ObjectPool newPool(int poolSize,
                                 java.lang.String clsName,
                                 java.lang.Object[] params)
                          throws ObjectPool.ObjectPoolException
A convenience method to create a new pool of objects of a given class, by name, using the given parameters.

Parameters:
poolSize - the size of the pool
clsName - the name of the class of the pooled objects
params - the parameters used to initialize the objects of the pool
Throws:
ObjectPool.ObjectPoolException - if the pool cannot be initialized

getObjectType

public java.lang.Class getObjectType()
Return the object type pooled by this pool


getFactory

protected ObjectPool.Factory getFactory()
Returns the factory.

Returns:
Factory

setFactory

protected void setFactory(ObjectPool.Factory factory)
Sets the factory.

Parameters:
factory - The factory to set

destroy

public void destroy(boolean waitForReleasedObjects)
Destroys all the objects in the pool by invoking the Factory.destroy() method on the pool's object factory.

Parameters:
waitForReleasedObjects - waits until all the objects are released before destroying them