package es.ejie.frmk.infraestructura.poolCache;

import java.util.Enumeration;
import java.util.Hashtable;


/**
 * Implementacion de Almacen de Pool de Geremua, basada en HashTable.
 */
public class Q70AlmacenRecursosPoolImpl implements Q70AlmacenRecursosPool {
    private java.util.Hashtable resourcePools;

    /**
     * Constructor. 
     */
    public Q70AlmacenRecursosPoolImpl() {
      this.resourcePools = new Hashtable();
    }

    /**
     * @see es.ejie.frmk.infraestructura.poolCache.Q70AlmacenRecursosPool#getResource(java.lang.Object)
     */
    public Q70RecursoPoolable getResource(Object key) throws Q70PoolException {
      Q70RecursoPoolable resource = null;

      Q70RecursoPool pool = getResourcePool(key);

      if (pool != null)
      {
        resource = pool.getResource();
      }

      return resource;
    }

    protected Q70RecursoPool getResourcePool(Object key) {
      return (Q70RecursoPool)this.resourcePools.get(key);
    }

    /**
     * @see es.ejie.frmk.infraestructura.poolCache.Q70AlmacenRecursosPool#freeResource(java.lang.Object, es.ejie.frmk.infraestructura.poolCache.Q70RecursoPoolable)
     */
    public void freeResource(Object key, Q70RecursoPoolable resource) {
      Q70RecursoPool pool = getResourcePool(key);

      if (pool != null)
      {
        pool.freeResource(resource);
      }
      else
      {
        // ignorara. Se ha dado una clave inexistente
      }
    }

    /**
     * @see es.ejie.frmk.infraestructura.poolCache.Q70AlmacenRecursosPool#putResourcePool(java.lang.Object, es.ejie.frmk.infraestructura.poolCache.Q70RecursoPool)
     */
    public void putResourcePool(Object key, Q70RecursoPool resourcePool) {
      this.resourcePools.put(key, resourcePool);
    }

    /**
     * @see es.ejie.frmk.infraestructura.poolCache.Q70AlmacenRecursosPool#removeResourcePool(java.lang.Object)
     */
    public void removeResourcePool(Object key) {
      this.resourcePools.remove(key);
    }

    /**
     * @see es.ejie.frmk.infraestructura.poolCache.Q70AlmacenRecursosPool#release(java.lang.Object)
     */
    public synchronized void release(Object key) {
        Q70RecursoPool pool = getResourcePool(key);
        pool.release();
    }
    
    /**
     * @see es.ejie.frmk.infraestructura.poolCache.Q70AlmacenRecursosPool#release()
     */
    public synchronized void release() {
      for (Enumeration x = this.resourcePools.keys(); x.hasMoreElements();){
         release(x.nextElement());
      }
    }
}
