Interface Resource

All Known Subinterfaces:
DBResource, ResourceProvider

public interface Resource
Encapsulates a system resource (file, database connection, etc). It is used to manage usage and availability of shared system resources. This interface allows monitoring a resource and helps preventing dead locks on it as well as being freed as soon as it is not being used. Data providers can provide implementations for their own resources. This is specially interesting when resources require a specific treatment beyond the standard shared file or connection, for instance to manage connections to a server through its own connection pool.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Adds a consumer to this resource.
    void
    If the resource is not in use, calling this method will send a close request to all consumers referencing this resource.
    Executes an action which uses the current Resource.
    get()
    Returns an object that represents the resource.
    int
    Returns this resource's current number of consumers.
    Returns a custom object containing extended data relative to this resource.
    long
    Returns the date and time in which this resource was opened for the last time.
    long
    Returns the date and time in which this resource was accessed for the last time.
    Returns the resource's name.
    Returns the resource parameters.
    boolean
    Returns whether this resource is already in use by someone.
    boolean
    Returns whether this resource is opened.
    int
    Returns the number of times this resource has been opened since it was created.
    void
    Removes a consumer from this resource's consumer list.
    void
    Sets a custom object as this resource's extended data.
  • Method Details

    • getName

      Returns the resource's name.
      Returns:
      resource's name
      Throws:
      AccessResourceException
    • getParameters

      ResourceParameters getParameters()
      Returns the resource parameters. These parameters contain all the necessary information to access the resource.
      Returns:
      resource parameters.
    • getLastTimeOpen

      long getLastTimeOpen()
      Returns the date and time in which this resource was opened for the last time.
      Returns:
      date and time in which this resource was opened for the last time, in milliseconds.
    • getLastTimeUsed

      long getLastTimeUsed()
      Returns the date and time in which this resource was accessed for the last time.
      Returns:
      date and time in which this resource was accessed for the last time, in milliseconds.
    • inUse

      boolean inUse()
      Returns whether this resource is already in use by someone.
      Returns:
      true if this resource is in use, false if not.
    • isOpen

      boolean isOpen()
      Returns whether this resource is opened.
      Returns:
      true if this resource is opened, false if not.
    • openCount

      int openCount()
      Returns the number of times this resource has been opened since it was created.
      Returns:
      number of times this resource has been opened since it was created.
    • execute

      Executes an action which uses the current Resource.
      Parameters:
      runnable - to execute
      Returns:
      the action return value
      Throws:
      ResourceException - if there is an error executing the action
      ResourceExecuteException
    • closeRequest

      void closeRequest() throws ResourceException
      If the resource is not in use, calling this method will send a close request to all consumers referencing this resource. If the resource is in use, calling this method will do nothing.
      Throws:
      ResourceException
    • addConsumer

      void addConsumer(ResourceConsumer consumer)
      Adds a consumer to this resource. This will create a weak reference to the consumer in this resource's consumer list.
      Parameters:
      consumer - the consumer that will be added to this resource's consumer list.
    • removeConsumer

      void removeConsumer(ResourceConsumer consumer)
      Removes a consumer from this resource's consumer list.
      Parameters:
      consumer - the consumer that will be removed.
    • getConsumersCount

      int getConsumersCount()
      Returns this resource's current number of consumers.
      Returns:
      current number of consumers of this resource.
    • get

      Returns an object that represents the resource. The actual type depends on the resource provider. It could be a string with a descriptive name or something more elaborated.
      Returns:
      an object that represents the resource.
      Throws:
      AccessResourceException
    • getData

      Object getData()
      Returns a custom object containing extended data relative to this resource. This is part of a simple mechanism to allow passing further data to the resource that may be necessary for optimal treatment.
      Returns:
      data related to this resource
    • setData

      void setData(Object data)
      Sets a custom object as this resource's extended data. This is part of a simple mechanism to allow passing further data to the resource that may be necessary for optimal treatment.
      Parameters:
      data - a custom object containing data related to this resource.