package faqapp.beans;

import java.io.Serializable;
import java.io.StringWriter;
import java.io.PrintWriter;
import java.io.IOException;

/**
 * This bean class provides a wrapper around an exception. The bean class
 * exposes the message and stacktrace of the exception as attributes of the
 * bean making them easy to access from JSP pages.
 */ 
public class ExceptionWrapperBean implements java.io.Serializable
{
  private Exception m_exception = null;
  private String m_exceptionMessage = null;
  private String m_exceptionStackTrace = null;
  private String m_exceptionType = null;
  
  
  public ExceptionWrapperBean()
  {
  }

  /** 
   * Set the exception that is to be wrapped. Dissects the exception
   * and sets the fields on this bean that correspond to the exception
   * type, the message and the stacktrace.
   * @param the exception to be wrapped.
   */ 
  public void setException(Exception ex) throws java.io.IOException
  {
    m_exception = ex;
    if(getException()!=null)
    {
      setExceptionMessage(getException().getMessage());
      StringWriter sw = new StringWriter();
      PrintWriter pw = new PrintWriter(sw);
      getException().printStackTrace(pw);
      setExceptionStackTrace(sw.getBuffer().toString());
      setExceptionType(getException().getClass().getName());
    }
    else
    {
      setExceptionMessage("A valid exception was not provided");
    }
  }
  

  /**
   * Returns the exception that has been wrapped.
   * @return the exception.
   */
  public Exception getException()
  {
    return m_exception;
  }
  
  /**
   * Returns the type of this exception, such as <code>java.io.IOException
   * </i>>
   * @return the exception type.
   */
  public String getExceptionType()
  {
    return m_exceptionType;
  }
  
  /**
   * Sets the exception type.
   * @param the type of the exception.
   */
  private void setExceptionType(String type)
  {
    m_exceptionType = type;
  }
  
  
  /**
   * Sets the message for the exception.
   * @param the message exception.
   */
  private void setExceptionMessage(String msg)
  {
    m_exceptionMessage=msg;
  }
  
  /**
   * Returns the message from the exception.
   * @return the exception message.
   */
  public String getExceptionMessage()
  {
    return m_exceptionMessage;
  }

  /**
   * Sets the message for the stacktrace of the exception. The stacktrace
   * must be read and converted to a String before calling this method.
   * @param the stacktrace from the exception.
   */
  private void setExceptionStackTrace(String trace)
  {
    m_exceptionStackTrace = trace;
  }
  
  /**
   * Returns the stacktrace from the exception.
   * @return the stacktrace message.
   */
  public String getExceptionStackTrace()
  {
    return m_exceptionStackTrace;
  }

  



  
  
}
