View Javadoc

1   /*
2    * $Id: ExceptionBean.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.example.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      private static boolean showRootStackOnly = true;
21  
22      /**
23       * Specific details about the Throwable. For example, for
24       * <tt>FileNotFoundException</tt>, this contains the name of the file that
25       * could not be found.
26       */
27      private String detailMessage;
28  
29      /**
30       * The throwable that caused this throwable to get thrown, or null if this
31       * throwable was not caused by another throwable, or if the causative throwable
32       * is unknown.
33       */
34  
35      private ExceptionBean cause = null;
36  
37      /**
38       * The stack trace, as returned by getStackTrace().
39       */
40      private String[] stackTrace;
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, getDetailMessage());
79                  }
80                  else
81                  {
82                      t = (Throwable)ClassUtils.instanciateClass(aClass, getDetailMessage(), cause.toException());
83                  }
84                  if (getStackTrace() != null)
85                  {
86                      // t.setStackTrace( getStackTrace());
87                  }
88                  originalException = t;
89              }
90              catch (Exception e)
91              {
92                  throw new InstantiationException("Failed to create Exception from ExceptionBean: "
93                                                   + e.getMessage());
94              }
95          }
96          return originalException;
97      }
98  
99      public String getDetailMessage()
100     {
101         return detailMessage;
102     }
103 
104     public void setDetailMessage(String detailMessage)
105     {
106         this.detailMessage = detailMessage;
107     }
108 
109     public ExceptionBean getCause()
110     {
111         return cause;
112     }
113 
114     public void setCause(ExceptionBean cause)
115     {
116         this.cause = cause;
117     }
118 
119     public String[] /* List */
120     getStackTrace()
121     {
122         return stackTrace;
123     }
124 
125     // public void addStackTrace(String trace)
126     // {
127     // stackTrace.add(trace);
128     // }
129 
130     public void setStackTrace(StackTraceElement[] stackTrace)
131     {
132         this.stackTrace = getStackAsString(stackTrace);
133     }
134 
135     public void setStackTrace(String[] stackTrace)
136     {
137         this.stackTrace = stackTrace;
138     }
139 
140     public String getExceptionClass()
141     {
142         return exceptionClass;
143     }
144 
145     public void setExceptionClass(String exceptionClass)
146     {
147         this.exceptionClass = exceptionClass;
148     }
149 
150     protected String[] getStackAsString(java.lang.StackTraceElement[] elements)
151     {
152         String[] trace = new String[elements.length];
153         for (int i = 0; i < elements.length; i++)
154         {
155             trace[i] = elements[i].toString();
156         }
157         return trace;
158     }
159 }