com.gargoylesoftware.base.resource
Class PooledResourceFactory

java.lang.Object
  extended by com.gargoylesoftware.base.resource.ResourceFactory
      extended by com.gargoylesoftware.base.resource.PooledResourceFactory

public class PooledResourceFactory
extends ResourceFactory

A resource factory that provides object pooling

Version:
$Revision: 1.6 $
Author:
Mike Bowler

Field Summary
private  java.util.List cache_
           
private  int preferredCacheSize_
           
private  ResourceFactory sourceFactory_
           
 
Constructor Summary
PooledResourceFactory(ResourceFactory sourceFactory)
          Create an instance
 
Method Summary
 int getPreferredCacheSize()
          Return the preferredCacheSize
protected  ManagedResource getResourceImpl(ResourceManager resourceManager)
          Get a resource
 boolean reinitializeResourceIfPossible(ManagedResource resource)
          Reinitialize the resource to a known state.
protected  void releaseResourceImpl(ResourceManager resourceManager, ManagedResource resource)
          Release a resource
 void setPreferredCacheSize(int size)
          Set the preferredCacheSize
 
Methods inherited from class com.gargoylesoftware.base.resource.ResourceFactory
getResource, releaseAllResources, releaseResource
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

sourceFactory_

private final ResourceFactory sourceFactory_

preferredCacheSize_

private int preferredCacheSize_

cache_

private java.util.List cache_
Constructor Detail

PooledResourceFactory

public PooledResourceFactory(ResourceFactory sourceFactory)
Create an instance

Parameters:
sourceFactory - The factory that will be used to actually create and destroy the pooled resources
Method Detail

setPreferredCacheSize

public void setPreferredCacheSize(int size)
Set the preferredCacheSize

Parameters:
size - The new size. May not be negative

getPreferredCacheSize

public int getPreferredCacheSize()
Return the preferredCacheSize

Returns:
The size

reinitializeResourceIfPossible

public boolean reinitializeResourceIfPossible(ManagedResource resource)
Reinitialize the resource to a known state. This is required for resource pooling as all resources being returned from a pool must have been initialized to a known state.

Specified by:
reinitializeResourceIfPossible in class ResourceFactory
Parameters:
resource - the resource to reinitialize
Returns:
true if the resource was successfully reinitialized

getResourceImpl

protected ManagedResource getResourceImpl(ResourceManager resourceManager)
                                   throws java.lang.Exception
Get a resource

Specified by:
getResourceImpl in class ResourceFactory
Parameters:
resourceManager - The manager that owns this factory
Returns:
A resource
Throws:
java.lang.Exception - If an error occurs

releaseResourceImpl

protected void releaseResourceImpl(ResourceManager resourceManager,
                                   ManagedResource resource)
                            throws java.lang.Exception
Release a resource

Specified by:
releaseResourceImpl in class ResourceFactory
Parameters:
resource - The resource to release
resourceManager - The manager that owns this factory
Throws:
java.lang.Exception - If an error occurs