/*
 * 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 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.dynamo.except;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 * An exception thrown by objects that implement <CODE>ServiceProvider</CODE> when they
 * don't implement a service of a specified class and ID.
 *
 * @author Eric J. Bowersox &lt;erbo@silcom.com&gt;
 * @version X
 * @see com.silverwrist.dynamo.iface.ServiceProvider
 */
public class NoSuchServiceException extends DynamoRuntimeException
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static final String BUNDLE = "com.silverwrist.dynamo.except.DynamoExceptionMessages";

  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private String m_context;           // context string
  private String m_message_id;        // ID of the message to generate
  private Class m_klass;              // class of the service we were asking for
  private String m_serviceid;         // service ID of the service we were asking for
  private Object[] m_args;            // arguments list to generate

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

  /**
   * Constructs a new <CODE>NoSuchServiceException</CODE>.
   *
   * @param context Context for the generated exception.
   * @param klass Class of the service that was requested.
   */
  public NoSuchServiceException(String context, Class klass)
  {
    super(context + ": no service with class " + klass.getName());
    m_context = context;
    m_klass = klass;
    m_serviceid = null;
    m_message_id = "noSuchServiceException.1";
    m_args = new Object[2];
    m_args[0] = context;
    m_args[1] = klass.getName();

  } // end constructor

  /**
   * Constructs a new <CODE>NoSuchServiceException</CODE>.
   *
   * @param context Context for the generated exception.
   * @param klass Class of the service that was requested.
   * @param inner Inner exception which caused this one.
   */
  public NoSuchServiceException(String context, Class klass, Throwable inner)
  {
    super(context + ": no service with class " + klass.getName(),inner);
    m_context = context;
    m_klass = klass;
    m_serviceid = null;
    m_message_id = "noSuchServiceException.1";
    m_args = new Object[2];
    m_args[0] = context;
    m_args[1] = klass.getName();

  } // end constructor

  /**
   * Constructs a new <CODE>NoSuchServiceException</CODE>.
   *
   * @param context Context for the generated exception.
   * @param klass Class of the service that was requested.
   * @param serviceid ID of the service that was requested.
   */
  public NoSuchServiceException(String context, Class klass, String serviceid)
  {
    super(context + ": no service with class " + klass.getName() + " and ID " + serviceid);
    m_context = context;
    m_klass = klass;
    m_serviceid = serviceid;
    m_message_id = "noSuchServiceException.2";
    m_args = new Object[3];
    m_args[0] = context;
    m_args[1] = klass.getName();
    m_args[2] = serviceid;

  } // end constructor

  /**
   * Constructs a new <CODE>NoSuchServiceException</CODE>.
   *
   * @param context Context for the generated exception.
   * @param klass Class of the service that was requested.
   * @param serviceid ID of the service that was requested.
   * @param inner Inner exception which caused this one.
   */
  public NoSuchServiceException(String context, Class klass, String serviceid, Throwable inner)
  {
    super(context + ": no service with class " + klass.getName() + " and ID " + serviceid,inner);
    m_context = context;
    m_klass = klass;
    m_serviceid = serviceid;
    m_message_id = "noSuchServiceException.2";
    m_args = new Object[3];
    m_args[0] = context;
    m_args[1] = klass.getName();
    m_args[2] = serviceid;

  } // end constructor

  public NoSuchServiceException(String context, NoSuchServiceException other)
  {
    super(morphMessage(other,context),other);
    m_context = context;
    m_klass = other.m_klass;
    m_serviceid = other.m_serviceid;
    m_message_id = other.m_message_id;
    m_args = new Object[other.m_args.length];
    System.arraycopy(other.m_args,0,m_args,0,other.m_args.length);
    m_args[0] = context;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Internal operations
   *--------------------------------------------------------------------------------
   */

  private static final String morphMessage(NoSuchServiceException other, String new_context)
  {
    return new_context + other.getMessage().substring(other.m_context.length());

  } // end morphMessage

  /*--------------------------------------------------------------------------------
   * Overrides from class Throwable
   *--------------------------------------------------------------------------------
   */

  /**
   * Creates a localized description of this exception. Subclasses may override this method in
   * order to produce a locale-specific message. For subclasses that do not override this method,
   * the default implementation returns the same result as <CODE>getMessage()</CODE>.
   *
   * @return The localized description of this exception.
   */
  public String getLocalizedMessage()
  {
    return this.getLocalizedMessage(Locale.getDefault());

  } // end getLocalizedMessage

  /*--------------------------------------------------------------------------------
   * Overrides from class DynamoRuntimeException
   *--------------------------------------------------------------------------------
   */

  /**
   * Creates a localized description of this exception. Subclasses may override this method in
   * order to produce a locale-specific message. For subclasses that do not override this method,
   * the default implementation returns the same result as <CODE>getLocalizedMessage()</CODE>.
   *
   * @param locale The locale to render the message in.
   * @return The localized description of this exception.
   */
  public String getLocalizedMessage(Locale locale)
  {
    ResourceBundle b = ResourceBundle.getBundle(BUNDLE,locale,getClass().getClassLoader());
    return MessageFormat.format(b.getString(m_message_id),m_args);

  } // end getLocalizedMessage

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

  public String getContext()
  {
    return m_context;

  } // end getContext

  public Class getServiceClass()
  {
    return m_klass;

  } // end getServiceClass

  public String getServiceID()
  {
    return m_serviceid;

  } // end getServiceID

} // end class NoSuchServiceException
