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

package org.jfox.jdbc.datasource;

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

import org.huihoo.jfox.pool.PoolableObject;
import org.huihoo.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) {

  }
}