org.makumba.db.makumba
Class ResourcePool

java.lang.Object
  extended by org.makumba.db.makumba.ResourcePool

public abstract class ResourcePool
extends Object

Keeps a number of identical creation-expensive resources. Avoids resource re-creation/deletion TODO: this class should be replaced by one more generic pool that exists out there

Author:
Cristian Bogdan

Field Summary
(package private)  Vector<Object> all
           
(package private)  WeakReference<ResourcePool> poolRef
          a weak reference to ourselves, for usage by foreign objects
(package private)  long sleeping
           
(package private)  Stack<Object> stack
           
(package private)  long stale
           
(package private)  Thread stalePreventionThread
           
(package private)  Stack<Date> timeStack
           
 
Constructor Summary
ResourcePool()
           
 
Method Summary
 void close()
          clear all resource containers. if we have a stale prevention thread, we interrupt it
 void close(Object o)
          close a resource
abstract  Object create()
          re-define this method to express how to create a resource
(package private)  Object createAndCount()
          create a new resource
protected  void finalize()
           
 Object get()
          get one resource
 int getSize()
           
 void init(int n)
          initialize the pool with n resources
 void put(Object o)
          put back one resource
 void renew(Object o)
          refresh a resource that was unused for a long time to prevent it from staling
protected  void renewAll()
          check for stale resources and renew the rotten ones
 void startStalePreventionThread(long sleepingTime, long staleTime)
          start a stale prevention thread
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

stack

Stack<Object> stack

timeStack

Stack<Date> timeStack

all

Vector<Object> all

poolRef

WeakReference<ResourcePool> poolRef
a weak reference to ourselves, for usage by foreign objects


sleeping

long sleeping

stale

long stale

stalePreventionThread

Thread stalePreventionThread
Constructor Detail

ResourcePool

public ResourcePool()
Method Detail

create

public abstract Object create()
                       throws Exception
re-define this method to express how to create a resource

Throws:
Exception

createAndCount

Object createAndCount()
                throws Exception
create a new resource

Throws:
Exception

init

public void init(int n)
          throws Exception
initialize the pool with n resources

Throws:
Exception

get

public Object get()
           throws Exception
get one resource

Throws:
Exception

put

public void put(Object o)
put back one resource


close

public void close()
clear all resource containers. if we have a stale prevention thread, we interrupt it


finalize

protected void finalize()
Overrides:
finalize in class Object

renew

public void renew(Object o)
refresh a resource that was unused for a long time to prevent it from staling


close

public void close(Object o)
close a resource


startStalePreventionThread

public void startStalePreventionThread(long sleepingTime,
                                       long staleTime)
start a stale prevention thread


renewAll

protected void renewAll()
check for stale resources and renew the rotten ones


getSize

public int getSize()