/* JFox, the OpenSource J2EE Application Server
 *
 * Copyright (C) 2002 huihoo.org
 * Distributable under GNU LGPL license
 * See the GNU Lesser General Public License for more details.
 */

package org.huihoo.jfox.pool;

import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;

/**
 * 
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

class PoolManager {

  private static Map pools = new HashMap();

  /**
   * create a object pool with the poolable object factory
   * @param factory
   * @return
   */
  public static ObjectPool createPool(ObjectFactory factory) {
    ObjectPool pool = new SimpleObjectPool(factory);
    synchronized(pools){
      pools.put(factory.getClass(),pool);
    }
    return pool;
  }

  /**
   * obtain a object pool , when not exists, will create one
   * @param factory
   * @return
   */
  public static ObjectPool obtainPool(ObjectFactory factory) {
    synchronized(pools){
      if(existsPool(factory.getClass())) {
        return (ObjectPool)pools.get(factory.getClass());
      }
      else {
        return createPool(factory);
      }
    }
  }

  public static boolean existsPool(Class factoryClass){
    synchronized(pools){
      return pools.containsKey(factoryClass);
    }
  }

  public static synchronized Iterator poolsIterator(){
    return pools.values().iterator();
  }


  public static void main(String[] args) {

  }
}
