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

import java.sql.Connection;

import org.huihoo.jfox.pool.ObjectPool;
import org.huihoo.jfox.pool.ObjectFactory;
import org.huihoo.jfox.pool.SimpleObjectPool;
import org.huihoo.jfox.pool.PoolableObject;
import org.huihoo.jfox.service.ComponentSupport;
/**
 * Get connections from this pool, all connections get from this pool can be recycled
 * for resue.
 * @author <a href="mailto:kelvin_wym@hotmail.com">Kelvin Wu</a>
 */
public class SimpleConnectionPool extends ComponentSupport implements SimpleConnectionPoolMBean, ConnectionPool {
  // A generic object pool used by the connection pool.
  private ObjectPool pool = null;
  private ConnectionFactory factory = null;

  /**
   * Default constructor
   * @param dbDriver database driver class name.
   * @param dbURL database url for connect to the database.
   * @param user user name.
   * @param password password
   */
  public SimpleConnectionPool(String dbDriver, String dbURL, String user, String password) throws Exception {
    factory = new ConnectionFactory(SimplePoolableConnection.class,PoolableConnectionInvocationHandler.class);
    factory.setDbDriver(dbDriver);
    factory.setDbURL(dbURL);
    factory.setUser(user);
    factory.setPassword(password);
    pool = new SimpleObjectPool(factory);
  }

  /**
   * Get the pooled connection that will be used by user.
   * @return Connection that will be used by user
   */
  public Connection getConnection() throws Exception {
    PoolableObject obj = pool.retrieveObject();
    // 设置这个 PoolableConnection 所在的 pool, 以便在调用 connection.close 的时候，调用 pool.restore
    ((PoolableConnection)obj).setPool(pool);
    return (Connection)obj;
  }

  protected void doInit() throws Exception {
    pool.init();
  }

  protected void doDestroy() throws Exception {
    pool.destroy();
  }

  public void clear() {
    pool.clear();
  }

  public ObjectFactory getObjectFactory() {
    return pool.getObjectFactory();
  }

  public String getObjectClass() {
    return pool.getObjectClass();
  }

  public int getWorking() {
    return pool.getWorking();
  }

  public int getRest() {
    return pool.getRest();
  }

  public void setDbURL(String dbUrl) {
    pool.clear();
    factory.setDbURL(dbUrl);
  }

  public String getDbURL() {
    return factory.getDbURL();
  }

  public void setDbDriver(String driver) {
    pool.clear();
    factory.setDbDriver(driver);
  }

  public String getDbDriver() {
    return factory.getDbDriver();
  }

  public void setUser(String user) {
    pool.clear();
    factory.setUser(user);
  }

  public String getUser() {
    return factory.getUser();
  }

  public void setPassword(String password) {
    pool.clear();
    factory.setPassword(password);
  }

  public String getPassword() {
    return factory.getPassword();
  }
}
