/** Copyright (c) Oracle Corporation 1999.  All Rights Reserved. */

package oracle.jbo.server;

import java.util.Hashtable;
import java.util.Enumeration;
import java.util.Properties;

import java.sql.Connection;
import java.sql.SQLException;

import oracle.jbo.common.PropertyMetadata;
import oracle.jbo.common.JboEnvUtil;

/**
 * Default BC4J implementation of the connection pool manager.
 */
class ConnectionPoolManagerImpl implements ConnectionPoolManager {

   private Hashtable mConnectionPools      = null;
   private int       mInitPoolSize         = 0;
   private int       mMaxPoolSize          = 2147483647;

   private static final String USER        = "user";
   private static final String PASSWORD    = "password";

   /**
    * Constructor
    */
   ConnectionPoolManagerImpl()
   {
      init();
   }

   private void init()
   {
      mInitPoolSize = JboEnvUtil.getPropertyAsInt(
         PropertyMetadata.ENV_INIT_JDBC_POOL_SIZE.pName
         , (new Integer(PropertyMetadata.ENV_INIT_JDBC_POOL_SIZE.pDefault))
            .intValue());

      mMaxPoolSize = JboEnvUtil.getPropertyAsInt(
         PropertyMetadata.ENV_MAX_JDBC_POOL_SIZE.pName
         , (new Integer(PropertyMetadata.ENV_MAX_JDBC_POOL_SIZE.pDefault))
            .intValue());

      mConnectionPools = new Hashtable(10);
   }

   public Connection getConnection(
      String poolKey
      , String url
      , Properties info
      , String user
      , String password)
   {
      return getConnectionPool(poolKey).getConnection(
         url
         , info
         , user
         , password);
   }

   public void addConnection(String poolKey, Connection connection)
   {
      getConnectionPool(poolKey).addConnection(connection);
   }

   public void returnConnection(String poolKey, Connection connection)
   {
      getConnectionPool(poolKey).returnConnection(connection);
   }

   public void removeConnection(String poolKey, Connection connection)
   {
      getConnectionPool(poolKey).removeConnection(connection);
   }

   public String generatePoolKey(String url, Properties info)
   {
      String poolKey = url;

      String user = null;
      String password = null;
      if ((info != null)
         && ((user = (String)info.get(USER)) != null)
         && ((password = (String)info.get(PASSWORD)) != null))
      {
         poolKey = generatePoolKey(url, user, password);
      }

      return poolKey;
   }

   public String generatePoolKey(String url, String user, String password)
   {
      String poolKey = url;

      if ((user != null) && (password != null))
      {
         poolKey = (new StringBuffer(128))
            .append(poolKey)
            .append(":")
            .append(user)
            .append("/")
            .append(password).toString();
      }

      return poolKey;
   }

   public int getInitPoolSize()
   {
      return mInitPoolSize;
   }

   public int getMaxPoolSize()
   {
      return mMaxPoolSize;
   }

   private ConnectionPool getConnectionPool(String poolKey)
   {
      ConnectionPool connectionPool = null;
      synchronized (mConnectionPools)
      {
         connectionPool = (ConnectionPool)mConnectionPools.get(poolKey);

         if (connectionPool == null)
         {
            connectionPool = new ConnectionPool(poolKey);
            mConnectionPools.put(poolKey, connectionPool);
         }
      }

      return connectionPool;
   }

}

