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

/**
 * An implementation of the {@link com.silverwrist.dynamo.iface.ServiceProvider ServiceProvider} interface
 * that contains service implementations.  The caller may register any number of service interfaces (with
 * optional service IDs) witht his class, which are then accessible through its <CODE>ServiceProvider</CODE>
 * methods.
 *
 * @author Eric J. Bowersox &lt;erbo@silcom.com&gt;
 * @version X
 */
public class SimpleServiceProvider extends BaseDelegatingServiceProvider
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private Hashtable m_store;            // store for simple interfaces
  private Hashtable m_store2;           // store for interfaces/service IDs

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

  /**
   * Constructs a new <CODE>SimpleServiceProvider</CODE> object.
   *
   * @param context Context string which is used in exceptions thrown by this class.
   */
  public SimpleServiceProvider(String context)
  {
    super(context);
    m_store = new Hashtable();
    m_store2 = new Hashtable();

  } // end constructor

  /**
   * Constructs a new <CODE>SimpleServiceProvider</CODE> object.
   *
   * @param context Context string which is used in exceptions thrown by this class.
   * @param delegate Implementation of {@link com.silverwrist.dynamo.iface.ServiceProvider ServiceProvider} to
   *                 use to delegate service queries that this object can't handle.
   */
  public SimpleServiceProvider(String context, ServiceProvider delegate)
  {
    super(context,delegate);
    m_store = new Hashtable();
    m_store2 = new Hashtable();

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Overrides from class BaseDelegatngServiceProvider
   *--------------------------------------------------------------------------------
   */

  /**
   * Queries this object for a specified service.  If the service is not registered with this object,
   * queries the delegate {@link com.silverwrist.dynamo.iface.ServiceProvider ServiceProvider}, 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)
  {
    Object rc = m_store.get(klass);
    if (rc!=null)
      return rc;
    return super.queryService(klass);

  } // end queryService

  /**
   * Queries this object for a specified service.  If the service is not registered with this object,
   * queries the delegate {@link com.silverwrist.dynamo.iface.ServiceProvider ServiceProvider}, 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)
  {
    Object rc = null;
    Hashtable tmp = (Hashtable)(m_store2.get(klass));
    if (tmp==null)
      rc = m_store.get(klass);
    else
      rc = tmp.get(serviceid);
    if (rc!=null)
      return rc;
    return super.queryService(klass,serviceid);

  } // end queryService

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

  /**
   * Registers a new service with this object.
   *
   * @param klass Classname of service interface.
   * @param item Object of that class type that provides the service.
   * @exception java.lang.ClassCastException If the object is not of the specified class type.
   */
  public void addService(Class klass, Object item)
  {
    if (klass.isInstance(item))
      m_store.put(klass,item);
    else
      throw new ClassCastException("service of wrong class type");

  } // end addService

  /**
   * Registers a new service with this object.
   *
   * @param klass Classname of service interface.
   * @param serviceid Service ID to register this service under.
   * @param item Object of that class type that provides the service.
   * @exception java.lang.ClassCastException If the object is not of the specified class type.
   */
  public void addService(Class klass, String serviceid, Object item)
  {
    if (klass.isInstance(item))
    { // register new service
      Hashtable tmp = (Hashtable)(m_store2.get(klass));
      if (tmp==null)
      { // create second hashtable and add it
	tmp = new Hashtable();
	m_store2.put(klass,tmp);

      } // end if

      tmp.put(serviceid,item);

    } // end if
    else
      throw new ClassCastException("service of wrong class type");

  } // end addService

  /**
   * Removes a service registration from this object
   *
   * @param klass Classname of service to remove.
   */
  public void removeService(Class klass)
  {
    m_store.remove(klass);

  } // end removeService

  /**
   * Removes a service registration from this object
   *
   * @param klass Classname of service to remove.
   * @param serviceid Service ID of service to remove.
   */
  public void removeService(Class klass, String serviceid)
  {
    Hashtable tmp = (Hashtable)(m_store2.get(klass));
    if (tmp!=null)
      tmp.remove(serviceid);

  } // end removeService

} // end class SimpleServiceProvider
