/* 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 org.huihoo.jfox.service.ComponentSupport;

/**
 * the supper class of all object pool
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public abstract class AbstractObjectPool extends ComponentSupport implements ObjectPool {

  protected ObjectFactory factory = null;

  public AbstractObjectPool(ObjectFactory factory){
//    super(factory.getClass().getName());
    if(factory == null) throw new NullPointerException("factory is null.");
    this.factory = factory;
  }

  public AbstractObjectPool(String objectFactoryClassName, String poolableClassName) {
//    super(objectFactoryClassName);
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    try {
      Class factoryClass = loader.loadClass(objectFactoryClassName);
      factory = (ObjectFactory)factoryClass.getConstructor(new Class[]{java.lang.String.class}).newInstance(new String[]{poolableClassName});
    }
    catch(ClassNotFoundException e){
      logger.fatal("Class " + objectFactoryClassName + " not found!" + e);
    }
    catch(NoSuchMethodException e){
      logger.fatal("No such constructor" + e);
    }
    catch(Exception e){
      logger.fatal("ObjectFactory " + objectFactoryClassName + " instantiator error" + e);
    }
  }

  public ObjectFactory getObjectFactory() {
    return factory;
  }

  /**
   * get the pooled object's class type, it return by object pool's factory
   * @return
   */
  public String getObjectClass() {
    return factory.getObjectClass().getName();
  }

  /**
   * Clears any objects sitting idle in the pool, releasing any associated resources
   */
  public void clear() {
    throw new UnsupportedOperationException("clear");
  }

  protected void doInit() throws Exception {
    
  }

  protected void doDestroy() throws Exception {

  }

  /**
   * get the count of working object
   * @return
   */
  public int getWorking() {
    throw new UnsupportedOperationException("getWorking");
  }

  /**
   * get the count of rest object
   * @return
   */
  public int getRest() {
    throw new UnsupportedOperationException("getRest");
  }

}
