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

import java.io.PrintStream;
import java.io.PrintWriter;

/**
 * The exception which may be thrown by <CODE>ObjectFactory.newObject</CODE>, which wraps any exception
 * that might be thrown by constructors or other method calls within the method.
 *
 * @author Eric J. Bowersox &lt;erbo@silcom.com&gt;
 * @version X
 * @see ObjectFactory#newObject(java.lang.Object)
 */
public class ObjectFactoryException extends Exception
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private Throwable inner = null;           // internal "root cause" exception

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  /**
   * Creates a new <CODE>ObjectFactoryException</CODE>.
   *
   * @param t Exception being wrapped by this one.
   */
  public ObjectFactoryException(Throwable t)
  {
    super("Error creating new object: " + t.getMessage());
    inner = t;

  } // end constructor

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

  /**
   * Prints this exception and its backtrace to the standard error stream.  Also prints the backtrace
   * of any "wrapped" exception.
   *
   * @see java.lang.System#err
   */
  public void printStackTrace()
  {
    this.printStackTrace(System.err);

  } // end printStackTrace

  /**
   * Prints this exception and its backtrace to the specified <CODE>PrintStream</CODE>.  Also prints the
   * backtrace of any "wrapped" exception.
   *
   * @param s <CODE>PrintStream</CODE> to use for output.
   */
  public void printStackTrace(PrintStream s)
  {
    super.printStackTrace(s);
    if (inner!=null)
    { // print the inner stack trace
      s.print("Root cause: ");
      inner.printStackTrace(s);

    } // end if

  } // end printStackTrace

  /**
   * Prints this exception and its backtrace to the specified <CODE>PrintWriter</CODE>.  Also prints the
   * backtrace of any "wrapped" exception.
   *
   * @param s <CODE>PrintWriter</CODE> to use for output.
   */
  public void printStackTrace(PrintWriter s)
  {
    super.printStackTrace(s);
    if (inner!=null)
    { // print the inner stack trace
      s.print("Root cause: ");
      inner.printStackTrace(s);

    } // end if

  } // end printStackTrace

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

  /**
   * Returns the exception wrapped by this exception, or <CODE>null</CODE> if there is none.
   *
   * @return See above.
   */
  public Throwable getException()
  {
    return inner;

  } // end getException

} // end class ObjectFactoryException
