Class ResourcePool

java.lang.Object
org.jini.rio.resources.resource.ResourcePool
Direct Known Subclasses:
ThreadPool

public abstract class ResourcePool extends Object
A generic abstract class to handle a finite set of resources
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected LinkedList
    The alloced list
    protected LinkedList
    The free list
    protected String
    Unique identifier for a resourcePool instance
    protected int
    The maximum # of resources allowed
    protected int
    The minimum # of resources to create at init time
    protected int
    The total number of resources currently created
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    allocates a Resource and performs any initilization required to manage the alloced resource
    protected abstract Object
    Abstract method for creating a specific resource in the sub-class
    protected Object
    creates a new Resource and performs any initilization required to manage the resource
    protected void
    Pre-creates the minimum number of resources
    protected void
    createResources(int number)
    Pre-creates the specified number of resources
    protected abstract void
    Abstract method for disposing a specific resource in the sub-class
    protected void
    disposes of a Resource and performs any clean-up required to manage the resource
    protected void
    freeResource(Object resource)
    frees a Resource and performs any clean-up required to manage the resource
    get()
    Get a Resource from the pool, blocking forever until a resource becomes available
    get(long timeout)
    Get a Resource from the pool, blocking for a specified time until a resource becomes available, a zero timeout blocks forever.
    <T> List<T>
     
    int
    Gets the number of resources currently assigned
    int
    Gets the number of resources currently available for assignment
    Gets the unique identifier for this instance
    Gets a registered ResourcePool
    int
    Gets the maximum number of resources allowed
    int
    Gets the minimum number of resources
    int
    Gets the total number of resources created
    protected void
    Registers this resource into the common ResourcePool
    protected void
    Registers this resource into the common ResourcePool
    static void
    Registers a resource into the common ResourcePool
    void
    Release a resource back to the pool after its use is completed.
    void
    Remove a resource all together from the pool and dispose of it
    void
    setIdentifier(String identifier)
    Sets the unique identifier for this instance
    void
    setMax(int max)
    Sets the maximum number of resources
    void
    setMin(int min)
    Sets the minimum number of resources, this method has no effect on the number of created resources, if the new minimum is less than the old minimum.
    Returns a string representation of the ResourcePool
    protected abstract boolean
    Abstract method for validating a resource

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • freed

      protected LinkedList freed
      The free list
    • alloced

      protected LinkedList alloced
      The alloced list
    • min

      protected int min
      The minimum # of resources to create at init time
    • max

      protected int max
      The maximum # of resources allowed
    • numCreated

      protected int numCreated
      The total number of resources currently created
    • identifier

      protected String identifier
      Unique identifier for a resourcePool instance
  • Constructor Details

    • ResourcePool

      public ResourcePool()
  • Method Details

    • create

      protected abstract Object create() throws ResourceUnavailableException
      Abstract method for creating a specific resource in the sub-class
      Returns:
      the resource
      Throws:
      ResourceUnavailableException - if an error occured in creating the resource
    • dispose

      protected abstract void dispose(Object obj)
      Abstract method for disposing a specific resource in the sub-class
      Parameters:
      obj - the resource to dispose
    • validate

      protected abstract boolean validate(Object obj)
      Abstract method for validating a resource
      Parameters:
      obj - the resource to validate
      Returns:
      true if resource is still valid
    • release

      public void release(Object obj)
      Release a resource back to the pool after its use is completed.
      Parameters:
      obj - the resource object
    • remove

      public void remove(Object obj)
      Remove a resource all together from the pool and dispose of it
      Parameters:
      obj - the resource object
    • get

      public Object get() throws ResourceUnavailableException
      Get a Resource from the pool, blocking forever until a resource becomes available
      Returns:
      the resource object
      Throws:
      ResourceUnavailableException - if an error occured in creating the resource
    • get

      public Object get(long timeout) throws ResourceUnavailableException
      Get a Resource from the pool, blocking for a specified time until a resource becomes available, a zero timeout blocks forever.
      Parameters:
      timeout - the time in milliseconds to wait for a resource to become available
      Returns:
      the resource object
      Throws:
      ResourceUnavailableException - if an error occured in creating the resource
      ResourceUnavailableException - if no resource becomes available before the timeout period expires
    • createResource

      protected Object createResource() throws ResourceUnavailableException
      creates a new Resource and performs any initilization required to manage the resource
      Returns:
      the new resource
      Throws:
      ResourceUnavailableException
    • allocResource

      protected void allocResource(Object resource)
      allocates a Resource and performs any initilization required to manage the alloced resource
      Parameters:
      resource - the resource to allocate
    • freeResource

      protected void freeResource(Object resource)
      frees a Resource and performs any clean-up required to manage the resource
      Parameters:
      resource - the resource to free
    • disposeResource

      protected void disposeResource(Object resource)
      disposes of a Resource and performs any clean-up required to manage the resource
      Parameters:
      resource - the resource to dispose
    • getFreedSize

      public int getFreedSize()
      Gets the number of resources currently available for assignment
      Returns:
      the number of resources currently available for assignment
    • getAllocedSize

      public int getAllocedSize()
      Gets the number of resources currently assigned
      Returns:
      the number of resources currently assigned
    • getAllocedOfType

      public <T> List<T> getAllocedOfType(Class<T> type)
    • getSize

      public int getSize()
      Gets the total number of resources created
      Returns:
      the total number of resources created
    • getMin

      public int getMin()
      Gets the minimum number of resources
      Returns:
      the minimum number of resources
    • getMax

      public int getMax()
      Gets the maximum number of resources allowed
      Returns:
      the maximum number of resources allowed
    • setMin

      public void setMin(int min)
      Sets the minimum number of resources, this method has no effect on the number of created resources, if the new minimum is less than the old minimum.
      Parameters:
      min - the minimum number of resources
      Throws:
      IllegalArgumentException - if minimum is less than zero or the minimum is is greater than the maximum
    • setIdentifier

      public void setIdentifier(String identifier)
      Sets the unique identifier for this instance
      Parameters:
      identifier - the unique identifier for this instance
    • getIdentifier

      public String getIdentifier()
      Gets the unique identifier for this instance
      Returns:
      the unique identifier for this instance
    • createResources

      protected void createResources() throws ResourceUnavailableException
      Pre-creates the minimum number of resources
      Throws:
      ResourceUnavailableException
    • createResources

      protected void createResources(int number) throws ResourceUnavailableException
      Pre-creates the specified number of resources
      Parameters:
      number - the number of resources to create
      Throws:
      ResourceUnavailableException
    • setMax

      public void setMax(int max)
      Sets the maximum number of resources
      Parameters:
      max - the maximum number of resources
      Throws:
      IllegalArgumentException - if maximum is less than the minimum
    • getInstance

      public static ResourcePool getInstance(String key)
      Gets a registered ResourcePool
      Parameters:
      key - the identifier for the type of resource pool
      Returns:
      the resource pool or null, if one is not registered for the key.
    • register

      protected void register()
      Registers this resource into the common ResourcePool
    • register

      protected void register(String key)
      Registers this resource into the common ResourcePool
      Parameters:
      key - the identifier for the type of resource pool
    • register

      public static void register(String key, ResourcePool pool)
      Registers a resource into the common ResourcePool
      Parameters:
      key - the identifier for the type of resource pool
      pool - the ResourcePool being registered
    • toString

      public String toString()
      Returns a string representation of the ResourcePool
      Overrides:
      toString in class Object
      Returns:
      a string representation of the ResourcePool