org.apache.cxf.common.util
Class AbstractTwoStageCache<E>

java.lang.Object
  extended by org.apache.cxf.common.util.AbstractTwoStageCache<E>

public abstract class AbstractTwoStageCache<E>
extends java.lang.Object

This class pools objects, for efficiency accross a lightweight fixed-size primary cache and a variable-size secondary cache - the latter uses soft references to allow the polled object be GCed if necessary.

To use the cache, a subclass is defined which provides an implementation of the abstract get() method - this may be conveniently achieved via an anonymous subclass. The cache is then populated by calling the populate_cache() method - the reason a two-stage process is used is to avoid problems with the inner class create() method accessing outer class data members from the inner class ctor (before its reference to the outer class is initialized).


Constructor Summary
AbstractTwoStageCache(int pCacheSize, int highWaterMark, int prealloc)
          Constructor.
AbstractTwoStageCache(int pCacheSize, int highWaterMark, int prealloc, java.lang.Object mutexParam)
          Constructor.
 
Method Summary
protected abstract  E create()
          Over-ride this method to create objects to populate the pool
 E get()
          Return a cached or newly created object
 E poll()
          Return a cached object if one is available
 void populateCache()
          Populate the cache
 boolean recycle(E oldObject)
          Recycle an old Object.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AbstractTwoStageCache

public AbstractTwoStageCache(int pCacheSize,
                             int highWaterMark,
                             int prealloc)
Constructor.

Parameters:
pCacheSize - primary cache size
secondary_cache_max - secondary cache high water mark
preallocation - the number of object to preallocation when the cache is created

AbstractTwoStageCache

public AbstractTwoStageCache(int pCacheSize,
                             int highWaterMark,
                             int prealloc,
                             java.lang.Object mutexParam)
Constructor.

Parameters:
pCacheSize - primary cache size
secondary_cache_max - secondary cache high water mark
preallocation - the number of object to preallocation when the cache is created
mutex - object to use as a monitor
Method Detail

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

create

protected abstract E create()
                     throws java.lang.Exception
Over-ride this method to create objects to populate the pool

Returns:
newly created object
Throws:
java.lang.Exception

populateCache

public void populateCache()
                   throws java.lang.Exception
Populate the cache

Throws:
java.lang.Exception

get

public E get()
      throws java.lang.Exception
Return a cached or newly created object

Returns:
an object
Throws:
java.lang.Exception

poll

public E poll()
Return a cached object if one is available

Returns:
an object

recycle

public boolean recycle(E oldObject)
Recycle an old Object.

Parameters:
oldObject - the object to recycle
Returns:
true iff the object can be accomodated in the cache


Apache CXF