org.apache.fulcrum.pool
Class DefaultPoolService

java.lang.Object
  extended by org.apache.avalon.framework.logger.AbstractLogEnabled
      extended by org.apache.fulcrum.pool.DefaultPoolService
All Implemented Interfaces:
org.apache.avalon.framework.activity.Disposable, org.apache.avalon.framework.activity.Initializable, org.apache.avalon.framework.configuration.Configurable, org.apache.avalon.framework.logger.LogEnabled, org.apache.avalon.framework.service.Serviceable, PoolService

public class DefaultPoolService
extends org.apache.avalon.framework.logger.AbstractLogEnabled
implements PoolService, org.apache.avalon.framework.service.Serviceable, org.apache.avalon.framework.activity.Disposable, org.apache.avalon.framework.activity.Initializable, org.apache.avalon.framework.configuration.Configurable

The Pool Service extends the Factory Service by adding support for pooling instantiated objects. When a new instance is requested, the service first checks its pool if one is available. If the the pool is empty, a new instance will be requested from the FactoryService. For objects implementing the Recyclable interface, a recycle method will be called, when they taken from the pool, and a dispose method, when they are returned to the pool.

Version:
$Id: DefaultPoolService.java 670330 2008-06-22 09:37:21Z tv $
Author:
Ilkka Priha, Stephen McConnell

Field Summary
static String POOL_CAPACITY
          The property specifying the pool capacity.
 
Fields inherited from interface org.apache.fulcrum.pool.PoolService
DEFAULT_POOL_CAPACITY, ROLE
 
Constructor Summary
DefaultPoolService()
           
 
Method Summary
 void clearPool()
          Clears all instances from the pool.
 void clearPool(String className)
          Clears instances of a named class from the pool.
 void configure(org.apache.avalon.framework.configuration.Configuration conf)
          Avalon component lifecycle method
 void dispose()
          Avalon component lifecycle method
 int getCapacity(String className)
          Gets the capacity of the pool for a named class.
protected  org.apache.fulcrum.factory.FactoryService getFactory()
          Gets the factory service.
 Object getInstance(Class clazz)
          Gets an instance of a specified class either from the pool or by instatiating from the class if the pool is empty.
 Object getInstance(Class clazz, Object[] params, String[] signature)
          Gets an instance of a specified class either from the pool or by instatiating from the class if the pool is empty.
 Object getInstance(String className)
          Gets an instance of a named class either from the pool or by calling the Factory Service if the pool is empty.
 Object getInstance(String className, ClassLoader loader)
          Gets an instance of a named class either from the pool or by calling the Factory Service if the pool is empty.
 Object getInstance(String className, ClassLoader loader, Object[] params, String[] signature)
          Gets an instance of a named class either from the pool or by calling the Factory Service if the pool is empty.
 Object getInstance(String className, Object[] params, String[] signature)
          Gets an instance of a named class either from the pool or by calling the Factory Service if the pool is empty.
 int getSize(String className)
          Gets the current size of the pool for a named class.
 void initialize()
          Avalon component lifecycle method Initializes the service by loading default class loaders and customized object factories.
 boolean isLoaderSupported(String className)
          Tests if specified class loaders are supported for a named class.
 boolean putInstance(Object instance)
          Puts a used object back to the pool.
 void service(org.apache.avalon.framework.service.ServiceManager manager)
          Avalon component lifecycle method
 void setCapacity(String className, int capacity)
          Sets the capacity of the pool for a named class.
 
Methods inherited from class org.apache.avalon.framework.logger.AbstractLogEnabled
enableLogging, getLogger, setupLogger, setupLogger, setupLogger
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

POOL_CAPACITY

public static final String POOL_CAPACITY
The property specifying the pool capacity.

See Also:
Constant Field Values
Constructor Detail

DefaultPoolService

public DefaultPoolService()
Method Detail

getInstance

public Object getInstance(String className)
                   throws PoolException
Gets an instance of a named class either from the pool or by calling the Factory Service if the pool is empty.

Parameters:
className - the name of the class.
Returns:
the instance.
Throws:
PoolException - if recycling fails.

getInstance

public Object getInstance(String className,
                          ClassLoader loader)
                   throws PoolException
Gets an instance of a named class either from the pool or by calling the Factory Service if the pool is empty. The specified class loader will be passed to the Factory Service.

Parameters:
className - the name of the class.
loader - the class loader.
Returns:
the instance.
Throws:
PoolException - if recycling fails.

getInstance

public Object getInstance(String className,
                          Object[] params,
                          String[] signature)
                   throws PoolException
Gets an instance of a named class either from the pool or by calling the Factory Service if the pool is empty. Parameters for its constructor are given as an array of objects, primitive types must be wrapped with a corresponding class.

Parameters:
className - the name of the class.
loader - the class loader.
params - an array containing the parameters of the constructor.
signature - an array containing the signature of the constructor.
Returns:
the instance.
Throws:
PoolException - if recycling fails.

getInstance

public Object getInstance(String className,
                          ClassLoader loader,
                          Object[] params,
                          String[] signature)
                   throws PoolException
Gets an instance of a named class either from the pool or by calling the Factory Service if the pool is empty. Parameters for its constructor are given as an array of objects, primitive types must be wrapped with a corresponding class. The specified class loader will be passed to the Factory Service.

Parameters:
className - the name of the class.
loader - the class loader.
params - an array containing the parameters of the constructor.
signature - an array containing the signature of the constructor.
Returns:
the instance.
Throws:
PoolException - if recycling fails.

isLoaderSupported

public boolean isLoaderSupported(String className)
                          throws org.apache.fulcrum.factory.FactoryException
Tests if specified class loaders are supported for a named class.

Parameters:
className - the name of the class.
Returns:
true if class loaders are supported, false otherwise.
Throws:
PoolException - if test fails.
org.apache.fulcrum.factory.FactoryException

getInstance

public Object getInstance(Class clazz)
                   throws PoolException
Gets an instance of a specified class either from the pool or by instatiating from the class if the pool is empty.

Specified by:
getInstance in interface PoolService
Parameters:
clazz - the class.
Returns:
the instance.
Throws:
PoolException - if recycling fails.

getInstance

public Object getInstance(Class clazz,
                          Object[] params,
                          String[] signature)
                   throws PoolException
Gets an instance of a specified class either from the pool or by instatiating from the class if the pool is empty.

Specified by:
getInstance in interface PoolService
Parameters:
clazz - the class.
params - an array containing the parameters of the constructor.
signature - an array containing the signature of the constructor.
Returns:
the instance.
Throws:
PoolException - if recycling fails.

putInstance

public boolean putInstance(Object instance)
Puts a used object back to the pool. Objects implementing the Recyclable interface can provide a recycle method to be called when they are reused and a dispose method to be called when they are returned to the pool.

Specified by:
putInstance in interface PoolService
Parameters:
instance - the object instance to recycle.
Returns:
true if the instance was accepted.

getCapacity

public int getCapacity(String className)
Gets the capacity of the pool for a named class.

Specified by:
getCapacity in interface PoolService
Parameters:
className - the name of the class.

setCapacity

public void setCapacity(String className,
                        int capacity)
Sets the capacity of the pool for a named class. Note that the pool will be cleared after the change.

Specified by:
setCapacity in interface PoolService
Parameters:
className - the name of the class.
capacity - the new capacity.

getSize

public int getSize(String className)
Gets the current size of the pool for a named class.

Specified by:
getSize in interface PoolService
Parameters:
className - the name of the class.

clearPool

public void clearPool(String className)
Clears instances of a named class from the pool.

Specified by:
clearPool in interface PoolService
Parameters:
className - the name of the class.

clearPool

public void clearPool()
Clears all instances from the pool.

Specified by:
clearPool in interface PoolService

getFactory

protected org.apache.fulcrum.factory.FactoryService getFactory()
Gets the factory service.

Returns:
the factory service.

configure

public void configure(org.apache.avalon.framework.configuration.Configuration conf)
Avalon component lifecycle method

Specified by:
configure in interface org.apache.avalon.framework.configuration.Configurable

service

public void service(org.apache.avalon.framework.service.ServiceManager manager)
Avalon component lifecycle method

Specified by:
service in interface org.apache.avalon.framework.service.Serviceable

initialize

public void initialize()
                throws Exception
Avalon component lifecycle method Initializes the service by loading default class loaders and customized object factories.

Specified by:
initialize in interface org.apache.avalon.framework.activity.Initializable
Throws:
InitializationException - if initialization fails.
Exception

dispose

public void dispose()
Avalon component lifecycle method

Specified by:
dispose in interface org.apache.avalon.framework.activity.Disposable


Copyright © 2005-2009 The Apache Software Foundation. All Rights Reserved.