/*
 * The contents of this file are subject to the Mozilla 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 <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2002 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.dynamo.db;

import java.lang.reflect.*;
import java.sql.Connection;
import java.sql.SQLException;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;

public class OpsBase
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static final Class[] ARGS_CLASS = { DBConnectionPool.class };

  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private DBConnectionPool m_pool;

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  protected OpsBase(DBConnectionPool pool)
  {
    m_pool = pool;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Internal operations
   *--------------------------------------------------------------------------------
   */

  protected final DBConnectionPool getPool()
  {
    return m_pool;

  } // end getPool

  protected final Connection getConnection() throws DatabaseException
  {
    return new WrappedConnection(m_pool,m_pool.getConnection());

  } // end getConnection

  protected final DatabaseException generalException(SQLException e)
  {
    DatabaseException dbe = new DatabaseException(OpsBase.class,"DatabaseMessages","general",e);
    dbe.setParameter(0,e.getMessage());
    return dbe;

  } // end generalException

  /*--------------------------------------------------------------------------------
   * External operations
   *--------------------------------------------------------------------------------
   */

  public void dispose()
  {
    m_pool = null;

  } // end dispose

  /*--------------------------------------------------------------------------------
   * External static operations
   *--------------------------------------------------------------------------------
   */

  protected static Object get(DBConnectionPool pool, ClassLoader loader, String stem_name, String type)
      throws ConfigException
  {
    try
    { // load the specified ops class and return a new instance
      String klassname = stem_name + pool.getDatabaseType().toLowerCase();
      Constructor c = loader.loadClass(klassname).getConstructor(ARGS_CLASS);
      Object[] args = new Object[1];
      args[0] = pool;
      return c.newInstance(args);

    } // end try
    catch (Exception e)
    { // error loading the ops class...
      ConfigException ce = new ConfigException(OpsBase.class,"DatabaseMessages","load.opsClass",e);
      ce.setParameter(0,e.getMessage());
      ce.setParameter(1,pool.getDatabaseType());
      ce.setParameter(2,type);
      throw ce;

    } // end catch

  } // end get

} // end class OpsBase
