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

/**
 * An implementation of the {@link com.silverwrist.dynamo.iface.ServiceProvider ServiceProvider} interface
 * that contains a single service implementation.
 *
 * @author Eric J. Bowersox &lt;erbo@silcom.com&gt;
 * @version X
 */
public class SingletonServiceProvider extends BaseDelegatingServiceProvider
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private Class m_class;    // class of the contained service provider
  private Object m_obj;     // object to return for queries for the above class

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

  /**
   * Constructs a new <CODE>SingletonServiceProvider</CODE>.
   *
   * @param context Context string which is used in exceptions thrown by this class.
   * @param klass Class of the service provided by this
   *              {@link com.silverwrist.dynamo.iface.ServiceProvider ServiceProvider}.
   * @param obj Object which is retuned in response to queries for the above class.
   * @exception java.lang.ClassCastException If the object is not of the specified class type.
   */
  public SingletonServiceProvider(String context, Class klass, Object obj)
  {
    super(context);
    if (!(klass.isInstance(obj)))
      throw new ClassCastException("service of wrong class type");
    m_class = klass;
    m_obj = obj;

  } // end constructor

  /**
   * Constructs a new <CODE>SingletonServiceProvider</CODE>.
   *
   * @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.
   * @param klass Class of the service provided by this <CODE>ServiceProvider</CODE>.
   * @param obj Object which is retuned in response to queries for the above class.
   * @exception java.lang.ClassCastException If the object is not of the specified class type.
   */
  public SingletonServiceProvider(String context, ServiceProvider delegate, Class klass, Object obj)
  {
    super(context,delegate);
    if (!(klass.isInstance(obj)))
      throw new ClassCastException("service of wrong class type");
    m_class = klass;
    m_obj = obj;

  } // 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)
  {
    if (klass==m_class)
      return m_obj;
    return super.queryService(klass);

  } // end queryService

} // end class SingletonServiceProvider
