/*-----------------------------------------------------------------------------
 *
 * Enhydra JdbcS
 * The Initial Developer of the Original Code is Lutris Technologies Inc.
 * Portions created by Lutris are Copyright 1997-2000 Lutris Technologies Inc.
 * All Rights Reserved.
 *
 * The contents of this file are subject to the Enhydra Public License
 * Version 1.1 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 * www.enhydra.org/license/epl.html
 *
 * Software distributed under the License is distributed on an "ASIS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 *
 * -----------------------------------------------------------------------------
 * $Id$
 * -----------------------------------------------------------------------------
 */
package org.enhydra.jdbc.standard;

import java.sql.SQLException;
import java.util.Hashtable;
import javax.sql.ConnectionPoolDataSource;
import javax.sql.PooledConnection;
import java.util.Enumeration;
import org.enhydra.jdbc.util.LRUCache;


/**
 * A data source used to create generic pooled connections (factory)
 */
public class StandardConnectionPoolDataSource extends StandardDataSource implements ConnectionPoolDataSource {

    private Hashtable masterPrepStmtCache;
    int	preparedStmtCacheSize;	// size of prepared statement cache
    public static final int DEFAULT_PREPAREDSTMTCACHESIZE = 25;
    
    /**
     * Constructor.
     */
    public StandardConnectionPoolDataSource () {
	super();
	masterPrepStmtCache = new Hashtable();
	preparedStmtCacheSize = DEFAULT_PREPAREDSTMTCACHESIZE;
	log("StandardConnectionPoolDataSource is created");
    } // constructor
    
    /**
     * Create a pooled connection using the default username and password.
     */
    public PooledConnection getPooledConnection () throws SQLException {
	log("StandardConnectionPoolDataSource:getPooledConnection(0) return a pooled connection");
	return getPooledConnection (user, password);
    } // method getPooledConnection
    
    /**
     * Create a standard pooled connection using the supplied username and password.
     */
    synchronized public PooledConnection getPooledConnection (String user, String password) throws SQLException {
	log("StandardConnectionPoolDataSource:getPooledConnection(2) return a pooled connection");
	return new StandardPooledConnection (this, user, password);
    } // method getPooledConnection

    public Hashtable getMasterPrepStmtCache() {
	return masterPrepStmtCache;
    }

    /**
     * Gets the size of the prepared statement cache
     */
    public int getPreparedStmtCacheSize() {
	return preparedStmtCacheSize;
    }

    /**
     * Sets the size of the prepared statement cache
     */
    public void setPreparedStmtCacheSize(int value) {
	preparedStmtCacheSize = value;
	if (preparedStmtCacheSize <= 0) {
	    masterPrepStmtCache.clear();
	} else {
	    Enumeration enum = masterPrepStmtCache.elements();
	    while (enum.hasMoreElements()) {
		((LRUCache)enum.nextElement()).resize(value);
	    }
	}
    }
   
} // class StandardConnectionPoolDataSource
