org.picocontainer.gems.adapters
Class PoolingComponentAdapter

java.lang.Object
  extended by org.picocontainer.defaults.DecoratingComponentAdapter
      extended by org.picocontainer.gems.adapters.PoolingComponentAdapter
All Implemented Interfaces:
java.io.Serializable, org.picocontainer.ComponentAdapter, org.picocontainer.defaults.ComponentMonitorStrategy, org.picocontainer.defaults.LifecycleStrategy, org.picocontainer.LifecycleManager

public class PoolingComponentAdapter
extends org.picocontainer.defaults.DecoratingComponentAdapter
implements org.picocontainer.LifecycleManager

ComponentAdapter implementation that pools components.

The implementation utilizes a delegated ComponentAdapter to create the instances of the pool. The pool can be configured to grow unlimited or to a maximum size. If a component is requested from this adapter, the implementation returns an availailabe instance from the pool or will create a new one, if the maximum pool size is not reached yet. If none is available, the implementation can wait a defined time for a returned object before it throws a PoolException.

This implementation uses the Pool toy from the ProxyToys project. This ensures, that any component, that is out of scope will be automatically returned to the pool by the garbage collector. Additionally will every component instance also implement Poolable, that can be used to return the instance manually. After returning an instance it should not be used in client code anymore.

Before a returning object is added to the available instances of the pool again, it should be reinitialized to a normalized state. By providing a proper Resetter implementation this can be done automatically. If the object cannot be reused anymore it can also be dropped and the pool may request a new instance.

The pool supports components with a lifecylce. If the delegated ComponentAdapter implements a LifecycleStrategy, any component retrieved form the pool will be started before and stopped again, when it returns back into the pool. Also if a component cannot be resetted it will automatically be disposed. If the container of the pool is disposed, that any returning object is also disposed and will not return to the pool anymore. Note, that current implementation cannot dispose pooled objects.

Since:
1.2
Author:
Jörg Schaible, Aslak Hellesøy
See Also:
Serialized Form

Nested Class Summary
static interface PoolingComponentAdapter.Context
          Context of the PoolingComponentAdapter used to initialize it.
static class PoolingComponentAdapter.DefaultContext
          The default context for a PoolingComponentAdapter.
(package private) static class PoolingComponentAdapter.LifecycleResetter
           
 
Field Summary
private  boolean autostartGC
           
static int BLOCK_ON_WAIT
          BLOCK_ON_WAIT forces the pool to wait until an object of the pool is returning in case none is immediately available.
private  java.util.List components
           
static int DEFAULT_MAX_SIZE
          DEFAULT_MAX_SIZE is the default size of the pool.
static com.thoughtworks.proxy.kit.Resetter DEFAULT_RESETTER
          DEFAULT_RESETTER is a NoOperationResetter that is used by default.
private  boolean delegateHasLifecylce
           
private  boolean disposed
           
static int FAIL_ON_WAIT
          FAIL_ON_WAIT forces the pool to fail none is immediately available.
private  int maxPoolSize
           
private  com.thoughtworks.proxy.toys.pool.Pool pool
           
private  int serializationMode
           
private static long serialVersionUID
           
private  boolean started
           
static int UNLIMITED_SIZE
          UNLIMITED_SIZE is the value to set the maximum size of the pool to unlimited (Integer.MAX_VALUE in fact).
private  int waitMilliSeconds
           
 
Constructor Summary
protected PoolingComponentAdapter()
          Construct an empty ComponentAdapter, used for serialization with reflection only.
  PoolingComponentAdapter(org.picocontainer.ComponentAdapter delegate)
          Construct a PoolingComponentAdapter with default settings.
  PoolingComponentAdapter(org.picocontainer.ComponentAdapter delegate, PoolingComponentAdapter.Context context)
          Construct a PoolingComponentAdapter.
 
Method Summary
 void dispose(org.picocontainer.PicoContainer container)
          Dispose of the container will dispose all returning objects.
 java.lang.Object getComponentInstance(org.picocontainer.PicoContainer container)
          
private  void readObject(java.io.ObjectInputStream in)
           
 int size()
          Retrieve the current size of the pool.
 void start(org.picocontainer.PicoContainer container)
          Start of the container ensures that at least one pooled component has been started.
 void stop(org.picocontainer.PicoContainer container)
          Stop of the container has no effect for the pool.
private  void writeObject(java.io.ObjectOutputStream out)
           
 
Methods inherited from class org.picocontainer.defaults.DecoratingComponentAdapter
accept, changeMonitor, currentMonitor, dispose, getComponentImplementation, getComponentKey, getDelegate, hasLifecycle, hasLifecycle, start, stop, toString, verify
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.picocontainer.LifecycleManager
hasLifecycle
 

Field Detail

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

UNLIMITED_SIZE

public static final int UNLIMITED_SIZE
UNLIMITED_SIZE is the value to set the maximum size of the pool to unlimited (Integer.MAX_VALUE in fact).

See Also:
Constant Field Values

DEFAULT_MAX_SIZE

public static final int DEFAULT_MAX_SIZE
DEFAULT_MAX_SIZE is the default size of the pool.

See Also:
Constant Field Values

BLOCK_ON_WAIT

public static final int BLOCK_ON_WAIT
BLOCK_ON_WAIT forces the pool to wait until an object of the pool is returning in case none is immediately available.

See Also:
Constant Field Values

FAIL_ON_WAIT

public static final int FAIL_ON_WAIT
FAIL_ON_WAIT forces the pool to fail none is immediately available.

See Also:
Constant Field Values

DEFAULT_RESETTER

public static final com.thoughtworks.proxy.kit.Resetter DEFAULT_RESETTER
DEFAULT_RESETTER is a NoOperationResetter that is used by default.


maxPoolSize

private int maxPoolSize

waitMilliSeconds

private int waitMilliSeconds

pool

private com.thoughtworks.proxy.toys.pool.Pool pool

serializationMode

private int serializationMode

autostartGC

private boolean autostartGC

started

private boolean started

disposed

private boolean disposed

delegateHasLifecylce

private boolean delegateHasLifecylce

components

private transient java.util.List components
Constructor Detail

PoolingComponentAdapter

public PoolingComponentAdapter(org.picocontainer.ComponentAdapter delegate)
Construct a PoolingComponentAdapter with default settings.

Parameters:
delegate - the delegated ComponentAdapter
Since:
1.2

PoolingComponentAdapter

public PoolingComponentAdapter(org.picocontainer.ComponentAdapter delegate,
                               PoolingComponentAdapter.Context context)
Construct a PoolingComponentAdapter. Remember, that the implementation will request new components from the delegate as long as no component instance is available in the pool and the maximum pool size is not reached. Therefore the delegate may not return the same component instance twice. Ensure, that the used ComponentAdapter does not cache.

Parameters:
delegate - the delegated ComponentAdapter
context - the PoolingComponentAdapter.Context of the pool
Throws:
java.lang.IllegalArgumentException - if the maximum pool size or the serialization mode is invalid
Since:
1.2

PoolingComponentAdapter

protected PoolingComponentAdapter()
Construct an empty ComponentAdapter, used for serialization with reflection only.

Since:
1.2
Method Detail

getComponentInstance

public java.lang.Object getComponentInstance(org.picocontainer.PicoContainer container)

As long as the maximum size of the pool is not reached and the pool is exhausted, the implementation will request its delegate for a new instance, that will be managed by the pool. Only if the maximum size of the pool is reached, the implementation may wait (depends on the initializing PoolingComponentAdapter.Context) for a returning object.

Specified by:
getComponentInstance in interface org.picocontainer.ComponentAdapter
Overrides:
getComponentInstance in class org.picocontainer.defaults.DecoratingComponentAdapter
Throws:
PoolException - if the pool is exhausted or waiting for a returning object timed out or was interrupted

size

public int size()
Retrieve the current size of the pool. The returned value reflects the number of all managed components.

Returns:
the number of components.
Since:
1.2

start

public void start(org.picocontainer.PicoContainer container)
Start of the container ensures that at least one pooled component has been started. Applies only if the delegated ComponentAdapter supports a lifecylce by implementing LifecycleStrategy.

Specified by:
start in interface org.picocontainer.LifecycleManager
Overrides:
start in class org.picocontainer.defaults.DecoratingComponentAdapter
Throws:
java.lang.IllegalStateException - if pool was already disposed

stop

public void stop(org.picocontainer.PicoContainer container)
Stop of the container has no effect for the pool. Applies only if the delegated ComponentAdapter supports a lifecylce by implementing LifecycleStrategy.

Specified by:
stop in interface org.picocontainer.LifecycleManager
Overrides:
stop in class org.picocontainer.defaults.DecoratingComponentAdapter
Throws:
java.lang.IllegalStateException - if pool was already disposed

dispose

public void dispose(org.picocontainer.PicoContainer container)
Dispose of the container will dispose all returning objects. They will not be added to the pool anymore. Applies only if the delegated ComponentAdapter supports a lifecylce by implementing LifecycleStrategy.

Specified by:
dispose in interface org.picocontainer.LifecycleManager
Overrides:
dispose in class org.picocontainer.defaults.DecoratingComponentAdapter
Throws:
java.lang.IllegalStateException - if pool was already disposed

writeObject

private void writeObject(java.io.ObjectOutputStream out)
                  throws java.io.IOException
Throws:
java.io.IOException

readObject

private void readObject(java.io.ObjectInputStream in)
                 throws java.io.IOException,
                        java.lang.ClassNotFoundException
Throws:
java.io.IOException
java.lang.ClassNotFoundException