/* 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.jdbc.datasource;

import java.sql.Connection;
import java.sql.DriverManager;

import org.jfox.pool.PoolableObject;
import org.jfox.pool.SimpleObjectFactory;

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

public class ConnectionFactory extends SimpleObjectFactory {
    private String dbDriver = null; // database dirver class name
    private String dbURL = null; // database url
    private String user = null; // database user name.
    private String password = null; // database password

    public ConnectionFactory(Class classType, String dbDriver, String dbURL, String user, String password) throws Exception {
        super(classType);
        this.dbDriver = dbDriver;
        this.dbURL = dbURL;
        this.user = user;
        this.password = password;
        Class.forName(this.dbDriver);
    }

    public PoolableObject makeObject() throws Exception {
        PoolableConnection pconn = (PoolableConnection) super.makeObject();
        Connection conn = DriverManager.getConnection(dbURL, user, password);
        pconn.setConnection(conn);
        return pconn;
    }

    public void destroyObject(PoolableObject object) throws Exception {
        if(object instanceof PoolableConnection) {
            ((PoolableConnection) object).getConnection().close();
        }
        super.destroyObject(object);
    }

    public static void main(String[] args) {

    }
}