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

/**
 * An implementation of {@link com.silverwrist.dynamo.iface.ObjectProvider ObjectProvider} which delegates
 * queries to other instances of <CODE>ObjectProvider</CODE> based on the namespace URI.
 *
 * @author Eric J. Bowersox &lt;erbo@silcom.com&gt;
 * @version X
 */
public class NamespaceDelegatingObjectProvider extends BaseDelegatingObjectProvider
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private Hashtable m_object_providers = new Hashtable();  // list of object providers by namespace

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

  /**
   * Creates a new instance of <CODE>NamespaceDelegatingObjectProvider</CODE>.
   *
   * @param context The context for this <CODE>ObjectProvider</CODE>, used to create error messages.
   */
  public NamespaceDelegatingObjectProvider(String context)
  {
    super(context);

  } // end constructor

  /**
   * Creates a new instance of <CODE>NamespaceDelegatingObjectProvider</CODE>.
   *
   * @param context The context for this <CODE>ObjectProvider</CODE>, used to create error messages.
   * @param delegate An <CODE>ObjectProvider</CODE> to delegate calls to if a named object can't be found
   *                 in this object.
   */
  public NamespaceDelegatingObjectProvider(String context, ObjectProvider delegate)
  {
    super(context,delegate);

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Overrides from class BaseDelegatingObjectProvider
   *--------------------------------------------------------------------------------
   */

  /**
   * Retrieves an object from this <CODE>ObjectProvider</CODE>.
   *
   * @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)
  {
    ObjectProvider prov = (ObjectProvider)(m_object_providers.get(namespace));
    if (prov!=null)
    { // we have a provider for this namespace - try using it
      try
      { // get the object from this provider
	return prov.getObject(namespace,name);

      } // end try
      catch (NoSuchObjectException e)
      { // rethrow the exception with our context
	throw new NoSuchObjectException(getContext(),e);

      } // end catch

    } // end if

    return super.getObject(namespace,name);

  } // end getObject

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

  /**
   * Adds a new delegate {@link com.silverwrist.dynamo.iface.ObjectProvider ObjectProvider} to this object.
   *
   * @param namespace Namespace to be delegated to another <CODE>ObjectProvider</CODE>.
   * @param provider The <CODE>ObjectProvider</CODE> to be delegated to.
   */
  public void addProvider(String namespace, ObjectProvider provider)
  {
    m_object_providers.put(namespace,provider);

  } // end addProvider

  /**
   * Removes a delegate {@link com.silverwrist.dynamo.iface.ObjectProvider ObjectProvider} from this object.
   *
   * @param namespace Namespace of the delegate <CODE>ObjectProvider</CODE> to be removed.
   */
  public void removeProvider(String namespace)
  {
    m_object_providers.remove(namespace);

  } // end removeProvider

  /**
   * Removes all delegate {@link com.silverwrist.dynamo.iface.ObjectProvider ObjectProvider}s from this object.
   */
  public void clearAllProviders()
  {
    m_object_providers.clear();

  } // end clearAllProviders

} // end class NamespaceDelegatingObjectProvider
