/*
 * 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-03 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.dynamo.util;

import org.apache.log4j.Logger;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.ServiceProvider;

/**
 * A utility class used in constructing implementations of
 * {@link com.silverwrist.dynamo.iface.ServiceProvider ServiceProvider}, which merely forwards all calls to its
 * <CODE>ServiceProvider</CODE> methods to an underlying, or <EM>delegate</EM>, <CODE>ServiceProvider</CODE>,
 * or fails the calls to those methods if it has no delegate.
 *
 * @author Eric J. Bowersox &lt;erbo@silcom.com&gt;
 * @version X
 */
public class BaseDelegatingServiceProvider implements ServiceProvider
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static Logger logger = Logger.getLogger(BaseDelegatingServiceProvider.class);

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

  private String m_context;             // context for error messages
  private ServiceProvider m_delegate;   // delegate here if not found at this level

  /*--------------------------------------------------------------------------------
   * Constructors
   *--------------------------------------------------------------------------------
   */

  /**
   * Creates a <CODE>BaseDelegatingServiceProvider</CODE> with no delegate.
   *
   * @param context Context string; used in constructing exceptions.
   */
  public BaseDelegatingServiceProvider(String context)
  {
    m_context = context;
    m_delegate = null;

  } // end constructor

  /**
   * Creates a <CODE>BaseDelegatingServiceProvider</CODE> with a delegate to which it will forward its calls.
   *
   * @param context Context string; used in constructing exceptions.
   * @param delegate The delegate <CODE>ServiceProvider</CODE>.
   */
  public BaseDelegatingServiceProvider(String context, ServiceProvider delegate)
  {
    m_context = context;
    m_delegate = delegate;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface ServiceProvider
   *--------------------------------------------------------------------------------
   */

  /**
   * Queries this object for a specified service.  This call is forwarded to the delegate for this object, if any.
   *
   * @param klass The class of the object that should be returned as a service.
   * @return A service object.  The service object is guaranteed to be of the class
   *         specified by <CODE>klass</CODE>; that is, if <CODE>queryService(klass)</CODE>
   *         yields some object <CODE>x</CODE>, then the expression <CODE>klass.isInstance(x)</CODE>
   *         is true.
   * @exception com.silverwrist.dynamo.except.NoSuchServiceException If no service is available in
   *            the specified class.
   */
  public Object queryService(Class klass)
  {
    try
    { // call through to delegate
      if (m_delegate==null)
	throw new NoSuchServiceException(m_context,klass);
      else
	return m_delegate.queryService(klass);

    } // end try
    catch (NoSuchServiceException e)
    { // chain the exception...
      throw new NoSuchServiceException(m_context,e);

    } // end catch

  } // end queryService

  /**
   * Queries this object for a specified service.  This call is forwarded to the delegate for this object, if any.
   *
   * @param klass The class of the object that should be returned as a service.
   * @param serviceid ID for the service to be requested, to further discriminate between requests.
   * @return A service object.  The service object is guaranteed to be of the class
   *         specified by <CODE>klass</CODE>; that is, if <CODE>queryService(klass)</CODE>
   *         yields some object <CODE>x</CODE>, then the expression <CODE>klass.isInstance(x)</CODE>
   *         is true.
   * @exception com.silverwrist.dynamo.except.NoSuchServiceException If no service is available in
   *            the specified class.
   */
  public Object queryService(Class klass, String serviceid)
  {
    try
    { // call through to delegate
      if (m_delegate==null)
	throw new NoSuchServiceException(m_context,klass,serviceid);
      else
	return m_delegate.queryService(klass,serviceid);

    } // end try
    catch (NoSuchServiceException e)
    { // chain the exception...
      throw new NoSuchServiceException(m_context,e);

    } // end catch

  } // end queryService

} // end class BaseDelegatingServiceProvider
