FileDocCategorySizeDatePackage
ResourceCache.javaAPI DocGlassfish v2 API5614Fri May 04 22:32:08 BST 2007com.sun.enterprise.util

ResourceCache

public abstract class ResourceCache extends Object

Fields Summary
static Logger
_logger
private Hashtable
resources
private int
count
Constructors Summary
public ResourceCache(int count)

    // END OF IASRI 4660742

         
      
    
	resources = new Hashtable();
	this.count = count;
    
Methods Summary
public abstract java.lang.ObjectcreateResource(java.lang.Object name)
Typically a long operation.

exception
Exception on exception

public java.lang.ObjectgetResource(java.lang.Object name)

exception
Exception on exception

	Object resource = null;
	Vector list     = null;
	Resource r      = null;

	// get the list of resources
	synchronized (resources) {

	    list = (Vector) resources.get(name);

	    if (list == null) {
		list = new Vector(count);
	    	resources.put(name, list);
	    }
	}

	// check for available resource
	boolean newR = false; // this thread needs to create a new resource
	synchronized (list) {

	    while (resource == null) {

	        // try to find an available resource
	        for (int i=0; i < list.size(); i++) {
		    if ((r = (Resource) list.elementAt(i)).isAvailable()) {
		        r.markInUse();
		        resource = r.getResource();
			break;
		    }
	        }
    
	        // create new resource if limit not reached
	        if (resource == null) {
		    if (list.size() < count) {
		        r = new Resource();
		        list.addElement(r);
		        newR = true;
			break;
		    }

		    // wait for the resource to free up
		    if (!newR) list.wait();
		}
	    }
	}

	// if new resource created, initialize the resource
	if (newR) {

	    try {
	    	resource = createResource(name);

	        // XXX need to handle case when resource is null

	    } catch (Exception ex) {
		
                /** IASRI 4660742
                ex.printStackTrace(); 
                **/
		            // START OF IASRI 4660742
                _logger.log(Level.SEVERE,"enterprise_util.excep_rescache_getres",ex);
                // END OF IASRI 4660742
	    }

	    synchronized (list) {
	        r.setResource(resource);
	        r.markInUse();
	    }
	}

	return resource;
    
public voidreturnResource(java.lang.Object name, java.lang.Object oldResource, java.lang.Object newResource)

	Resource r = null;
	Vector list = (Vector) resources.get(name);

	if (list != null) {
	    synchronized (list) {
	        for (int i=0; i < list.size(); i++) {
		    r = (Resource) list.elementAt(i);
		    if (r.getResource() == oldResource) {
			r.setResource(newResource);
		        r.markAvailable();
			list.notifyAll();
			break;
		    }
	        }
	    }
	}