View Javadoc

1   /*
2    * $Id: ExceptionBean.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.samples.errorhandler;
12  
13  import org.mule.util.ClassUtils;
14  
15  /**
16   * The <code>ExceptionBean</code> is a POJO representing the details of a Throwable
17   */
18  public class ExceptionBean
19  {
20      /**
21       * Specific details about the Throwable. For example, for
22       * <tt>FileNotFoundException</tt>, this contains the name of the file that
23       * could not be found.
24       */
25      private String detailMessage;
26  
27      /**
28       * The throwable that caused this throwable to get thrown, or null if this
29       * throwable was not caused by another throwable, or if the causative throwable
30       * is unknown.
31       */
32  
33      private ExceptionBean cause = null;
34  
35      /**
36       * The stack trace, as returned by getStackTrace().
37       */
38      private String[] stackTrace;
39  
40      static transient boolean showRootStackOnly = true;
41  
42      private String exceptionClass = null;
43  
44      private Throwable originalException = null;
45  
46      public ExceptionBean()
47      {
48          super();
49      }
50  
51      public ExceptionBean(Throwable exception)
52      {
53          if (exception == null) throw new IllegalArgumentException("The exception cannot be null");
54          originalException = exception;
55          exceptionClass = exception.getClass().getName();
56          setDetailMessage(exception.getMessage());
57          setStackTrace((showRootStackOnly ? null : getStackAsString(exception.getStackTrace())));
58          if (exception.getCause() != null)
59          {
60              setCause(new ExceptionBean(exception.getCause()));
61          }
62          else
63          {
64              setStackTrace(exception.getStackTrace());
65          }
66      }
67  
68      public Throwable toException() throws InstantiationException
69      {
70          if (originalException == null)
71          {
72              Throwable t = null;
73              try
74              {
75                  Class aClass = ClassUtils.loadClass(exceptionClass, getClass());
76                  if (cause == null)
77                  {
78                      t = (Throwable)ClassUtils.instanciateClass(aClass, new Object[]{getDetailMessage()});
79                  }
80                  else
81                  {
82                      t = (Throwable)ClassUtils.instanciateClass(aClass, new Object[]{getDetailMessage(),
83                          cause.toException()});
84                  }
85                  if (getStackTrace() != null)
86                  {
87                      // t.setStackTrace( getStackTrace());
88                  }
89                  originalException = t;
90              }
91              catch (Exception e)
92              {
93                  throw new InstantiationException("Failed to create Exception from ExceptionBean: "
94                                                   + e.getMessage());
95              }
96          }
97          return originalException;
98      }
99  
100     public String getDetailMessage()
101     {
102         return detailMessage;
103     }
104 
105     public void setDetailMessage(String detailMessage)
106     {
107         this.detailMessage = detailMessage;
108     }
109 
110     public ExceptionBean getCause()
111     {
112         return cause;
113     }
114 
115     public void setCause(ExceptionBean cause)
116     {
117         this.cause = cause;
118     }
119 
120     public String[] /* List */
121     getStackTrace()
122     {
123         return stackTrace;
124     }
125 
126     // public void addStackTrace(String trace)
127     // {
128     // stackTrace.add(trace);
129     // }
130 
131     public void setStackTrace(StackTraceElement[] stackTrace)
132     {
133         this.stackTrace = getStackAsString(stackTrace);
134     }
135 
136     public void setStackTrace(String[] stackTrace)
137     {
138         this.stackTrace = stackTrace;
139     }
140 
141     public String getExceptionClass()
142     {
143         return exceptionClass;
144     }
145 
146     public void setExceptionClass(String exceptionClass)
147     {
148         this.exceptionClass = exceptionClass;
149     }
150 
151     protected String[] getStackAsString(java.lang.StackTraceElement[] elements)
152     {
153         String[] trace = new String[elements.length];
154         for (int i = 0; i < elements.length; i++)
155         {
156             trace[i] = elements[i].toString();
157         }
158         return trace;
159     }
160 }