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

import com.silverwrist.dynamo.Namespaces;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;

public class GetObjectUtils
{
  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  public GetObjectUtils()
  { // do nothing
  } // end constructor

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

  private static ObjectProvider internalGetObjProv(ServiceProvider prov) throws ConfigException
  {
    try
    { // get the object provider 
      return (ObjectProvider)(prov.queryService(ObjectProvider.class));

    } // end try
    catch (NoSuchServiceException e)
    { // yak! this shouldn't happen!
      throw new ConfigException(GetObjectUtils.class,"GetObjectMessages","internal.objProv");

    } // end catch
    catch (ClassCastException e)
    { // yak! this shouldn't happen!
      throw new ConfigException(GetObjectUtils.class,"GetObjectMessages","internal.objProv");

    } // end catch

  } // end internalGetObjProv

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

  public static DBConnectionPool getDatabaseConnection(ObjectProvider prov, String namespace, String name)
      throws ConfigException
  {
    DBConnectionPool pool = null;
    try
    { // get the database connection pool
      pool = (DBConnectionPool)(prov.getObject(namespace,name));

    } // end try
    catch (NoSuchObjectException e)
    { // the connection pool object was not found
      ConfigException ce = new ConfigException(GetObjectUtils.class,"GetObjectMessages","pool.notFound",e);
      ce.setParameter(0,name);
      throw ce;

    } // end catch
    catch (ClassCastException e)
    { // this shouldn't really happen, but you never know
      ConfigException ce = new ConfigException(GetObjectUtils.class,"GetObjectMessages","pool.badClass",e);
      ce.setParameter(0,name);
      throw ce;

    } // end catch

    return pool;

  } // end getDatabaseConnection

  public static DBConnectionPool getDatabaseConnection(ObjectProvider prov, String name) throws ConfigException
  {
    return getDatabaseConnection(prov,Namespaces.DATABASE_CONNECTIONS_NAMESPACE,name);

  } // end getDatabaseConnection

  public static DBConnectionPool getDatabaseConnection(ServiceProvider prov, String namespace, String name)
      throws ConfigException
  {
    return getDatabaseConnection(internalGetObjProv(prov),namespace,name);

  } // end getDatabaseConnection

  public static DBConnectionPool getDatabaseConnection(ServiceProvider prov, String name)
      throws ConfigException
  {
    return getDatabaseConnection(internalGetObjProv(prov),Namespaces.DATABASE_CONNECTIONS_NAMESPACE,name);

  } // end getDatabaseConnection

  public static Object getDynamoComponent(ObjectProvider prov, Class iface, String namespace, String name)
      throws ConfigException
  {
    try
    { // get the component object
      Object rc = prov.getObject(namespace,name);
      if (iface.isInstance(rc))
	return rc;  // implements interface directly

      // OK, see if it implements the interface as a service...
      if (rc instanceof ServiceProvider)
	return ((ServiceProvider)rc).queryService(iface);

      // No dice - we're gone.
      ConfigException ce = new ConfigException(GetObjectUtils.class,"GetObjectMessages","component.noIFace");
      ce.setParameter(0,name);
      ce.setParameter(1,iface.getName());
      throw ce;

    } // end try
    catch (NoSuchObjectException e)
    { // the connection pool object was not found
      ConfigException ce = new ConfigException(GetObjectUtils.class,"GetObjectMessages",
					       "component.notFound",e);
      ce.setParameter(0,name);
      throw ce;

    } // end catch
    catch (NoSuchServiceException e)
    { // ServiceProvider call failed - bail out
      ConfigException ce = new ConfigException(GetObjectUtils.class,"GetObjectMessages","component.noIFace",e);
      ce.setParameter(0,name);
      ce.setParameter(1,iface.getName());
      throw ce;

    } // end catch

  } // end getDynamoComponent

  public static Object getDynamoComponent(ObjectProvider prov, Class iface, String name)
      throws ConfigException
  {
    return getDynamoComponent(prov,iface,Namespaces.DYNAMO_OBJECT_NAMESPACE,name);

  } // end getDynamoComponent

  public static Object getDynamoComponent(ServiceProvider prov, Class iface, String namespace, String name)
      throws ConfigException
  {
    return getDynamoComponent(internalGetObjProv(prov),iface,namespace,name);

  } // end getDynamoComponent

  public static Object getDynamoComponent(ServiceProvider prov, Class iface, String name)
      throws ConfigException
  {
    return getDynamoComponent(internalGetObjProv(prov),iface,Namespaces.DYNAMO_OBJECT_NAMESPACE,name);

  } // end getDynamoComponent

} // end class GetObjectUtils

