/*
 * 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 com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.ObjectProvider;

/**
 * A utility class used in constructing implementations of
 * {@link com.silverwrist.dynamo.iface.ObjectProvider ObjectProvider}, which merely forwards all calls to its
 * <CODE>ObjectProvider</CODE> methods to an underlying, or <EM>delegate</EM>, <CODE>ObjectProvider</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 BaseDelegatingObjectProvider implements ObjectProvider
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private String m_context;           // context for error messages
  private ObjectProvider m_delegate;  // object provider to delegate to

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

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

  } // end constructor

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

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface ObjectProvider
   *--------------------------------------------------------------------------------
   */

  /**
   * Retrieves an object from this <CODE>ObjectProvider</CODE>.  This call is forwarded to the delegate for
   * this object, if any.
   *
   * @param namespace The namespace to interpret the name relative to.
   * @param name The name of the object to be retrieved.
   * @return The object reference specified.
   */
  public Object getObject(String namespace, String name)
  {
    try
    { // call through to the delegate
      if (m_delegate==null)
	throw new NoSuchObjectException(m_context,namespace,name);
      else
	return m_delegate.getObject(namespace,name);

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

    } // end catch

  } // end getObject

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

  /**
   * Returns the context string associated with this provider.
   *
   * @return The context string associated with this provider.
   */
  public String getContext()
  {
    return m_context;

  } // end getContext

} // end class BaseDelegatingObjectProvider
