/* JFox, the OpenSource J2EE Application Server
 *
 * Distributable under GNU LGPL license by gun.org
 * more details please visit http://www.huihoo.org/jfox
 */

package org.jfox.pool;

import org.jfox.system.AbstractComponent;

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

public abstract class AbstractObjectPool extends AbstractComponent 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(Class factoryClass, Class poolableClass) {
//    super(objectFactoryClassName);
        try {
            factory = (ObjectFactory) factoryClass.getConstructor(new Class[]{java.lang.Class.class}).newInstance(new Class[]{poolableClass});
        }
        catch(NoSuchMethodException e) {
            logger.fatal("No such constructor" + e);
        }
        catch(Exception e) {
            logger.fatal("ObjectFactory " + factoryClass.getName() + " 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");
    }

    public int getInitNum() {
        throw new UnsupportedOperationException("getInitNum");
    }

    public int getMaxRest() {
        throw new UnsupportedOperationException("getMaxRest");
    }
}
