/*
 * 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} that responds
 * to queries on a single namespace only. It uses an internal <CODE>Hashtable</CODE> to map names within
 * that namespace to objects.
 *
 * @author Eric J. Bowersox &lt;erbo@silcom.com&gt;
 * @version X
 */
public class SingleNamespaceObjectProvider extends BaseDelegatingObjectProvider
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private String m_namespace;         // the namespace implemented by this provider
  private Hashtable m_store;          // the store backing this provider

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

  /**
   * Constructs a new <CODE>SingleNamespaceObjectProvider</CODE>.
   *
   * @param namespace The namespace URI this {@link com.silverwrist.dynamo.iface.ObjectProvider ObjectProvider}
   *                  will respond to.
   * @param context The context for this <CODE>ObjectProvider</CODE>, used to create error messages.
   */
  public SingleNamespaceObjectProvider(String namespace, String context)
  {
    super(context);
    m_namespace = namespace;
    m_store = new Hashtable();

  } // end constructor

  /**
   * Constructs a new <CODE>SingleNamespaceObjectProvider</CODE>.
   *
   * @param namespace The namespace URI this {@link com.silverwrist.dynamo.iface.ObjectProvider ObjectProvider}
   *                  will respond to.
   * @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 SingleNamespaceObjectProvider(String namespace, String context, ObjectProvider delegate)
  {
    super(context,delegate);
    m_namespace = namespace;
    m_store = new Hashtable();

  } // 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)
  {
    Object rc = null;
    if (m_namespace.equals(namespace))
      rc = m_store.get(name);
    if (rc!=null)
      return rc;
    return super.getObject(namespace,name);

  } // end getObject

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

  /**
   * Sets an object into this <CODE>ObjectProvider</CODE>.
   *
   * @param name Name to use for the object.
   * @param value Value of the object.
   */
  public void setObject(String name, Object value)
  {
    m_store.put(name,value);

  } // end setObject

  /**
   * Removes an object from this <CODE>ObjectProvider</CODE>.
   *
   * @param Name of the object to be removed.
   */
  public void removeObject(String name)
  {
    m_store.remove(name);

  } // end removeObject

} // end class SingleNamespaceObjectProvider
