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

import java.text.MessageFormat;
import java.util.*;
import com.silverwrist.util.StringUtils;

/**
 * An exception type which is externally reflected and contains a localizable message.  The message is loaded
 * from a <CODE>ResourceBundle</CODE> as needed.
 *
 * @author Eric J. Bowersox &lt;erbo@silcom.com&gt;
 * @version X
 */
public class ExternalRuntimeException extends DynamoRuntimeException
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private ClassLoader m_classloader;      // classloader to load resource bundle from
  private String m_bundle_name;           // resource bundle name
  private String m_message_id;            // message ID to load message from
  private String m_message = null;        // non-localized message (cached)
  private ArrayList m_args = null;        // argument list

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

  /**
   * Constructs a new <CODE>ExternalRuntimeException</CODE> instance.
   *
   * @param caller The classname of the class that's creating the exception.  Its class loader
   *               and package name will be used, together with <CODE>bundle</CODE>, to find the
   *               resource bundle.
   * @param bundle The name of the resource bundle to be loaded.
   * @param message_id The identifier of the message to be loaded from the bundle.
   */
  public ExternalRuntimeException(Class caller, String bundle, String message_id)
  {
    super("");
    setup(caller,bundle,message_id);

  } // end constructor

  /**
   * Constructs a new <CODE>ExternalRuntimeException</CODE> instance.
   *
   * @param caller The classname of the class that's creating the exception.  Its class loader
   *               and package name will be used, together with <CODE>bundle</CODE>, to find the
   *               resource bundle.
   * @param bundle The name of the resource bundle to be loaded.
   * @param message_id The identifier of the message to be loaded from the bundle.
   * @param inner The exception to be nested inside this one.
   */
  public ExternalRuntimeException(Class caller, String bundle, String message_id, Throwable inner)
  {
    super("",inner);
    setup(caller,bundle,message_id);

  } // end constructor

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

  /**
   * Sets up the instance variables of this <CODE>ExternalRuntimeException</CODE> instance.
   *
   * @param caller The classname of the class that's creating the exception.  Its class loader
   *               and package name will be used, together with <CODE>bundle</CODE>, to find the
   *               resource bundle.
   * @param bundle The name of the resource bundle to be loaded.
   * @param message_id The identifier of the message to be loaded from the bundle.
   */
  private final void setup(Class caller, String bundle, String message_id)
  {
    m_classloader = caller.getClassLoader();
    String name = caller.getName();
    int p = name.lastIndexOf('.');
    m_bundle_name = name.substring(0,p+1) + bundle;
    m_message_id = message_id;

  } // end setup

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

  /**
   * Returns the detail message string of this exception.
   *
   * @return The detail message string of this <CODE>Throwable</CODE> instance (which may be <CODE>null</CODE>).
   */
  public String getMessage()
  {
    if (m_message==null)
      m_message = getLocalizedMessage(Locale.US);
    return m_message;

  } // end getMessage

  /**
   * 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 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(m_bundle_name,locale,m_classloader);
    if (m_args==null)
      return b.getString(m_message_id);
    else
      return MessageFormat.format(b.getString(m_message_id),m_args.toArray());

  } // end getLocalizedMessage

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

  /**
   * Sets a replacement parameter for the message.
   *
   * @param index The index of the parameter to set.
   * @param param The parameter to be set for the message.
   */
  public void setParameter(int index, String param)
  {
    if (m_args==null)
      m_args = new ArrayList();
    if (m_args.size()>index)
    { // setting an index that already exist
      m_args.set(index,param);
      return;

    } // end if

    // this needs to be tacked onto the end somewhere
    while (m_args.size()<index)
      m_args.add(null);
    m_args.add(param);

  } // end setParameter

} // end class ExternalRuntimeException
