KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > samples > errorhandler > ExceptionBean


1 /*
2  * $Id: ExceptionBean.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
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  * <code>ExceptionBean</code> TODO -document class
17  *
18  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
19  * @version 1.0
20  */

21 public class ExceptionBean
22 {
23     /**
24      * Specific details about the Throwable. For example, for
25      * <tt>FileNotFoundException</tt>, this contains the name of the file that
26      * could not be found.
27      */

28     private String JavaDoc detailMessage;
29
30     /**
31      * The throwable that caused this throwable to get thrown, or null if this
32      * throwable was not caused by another throwable, or if the causative throwable
33      * is unknown.
34      */

35
36     private ExceptionBean cause = null;
37
38     /**
39      * The stack trace, as returned by getStackTrace().
40      */

41     private String JavaDoc[] stackTrace;
42
43     static transient boolean showRootStackOnly = true;
44
45     private String JavaDoc exceptionClass = null;
46
47     private Throwable JavaDoc originalException = null;
48
49     public ExceptionBean()
50     {
51         super();
52     }
53
54     public ExceptionBean(Throwable JavaDoc exception)
55     {
56         if (exception == null) throw new IllegalArgumentException JavaDoc("The exception cannot be null");
57         originalException = exception;
58         exceptionClass = exception.getClass().getName();
59         setDetailMessage(exception.getMessage());
60         setStackTrace((showRootStackOnly ? null : getStackAsString(exception.getStackTrace())));
61         if (exception.getCause() != null)
62         {
63             setCause(new ExceptionBean(exception.getCause()));
64         }
65         else
66         {
67             setStackTrace(exception.getStackTrace());
68         }
69     }
70
71     public Throwable JavaDoc toException() throws InstantiationException JavaDoc
72     {
73         if (originalException == null)
74         {
75             Throwable JavaDoc t = null;
76             try
77             {
78                 Class JavaDoc aClass = ClassUtils.loadClass(exceptionClass, getClass());
79                 if (cause == null)
80                 {
81                     t = (Throwable JavaDoc)ClassUtils.instanciateClass(aClass, new Object JavaDoc[]{getDetailMessage()});
82                 }
83                 else
84                 {
85                     t = (Throwable JavaDoc)ClassUtils.instanciateClass(aClass, new Object JavaDoc[]{getDetailMessage(),
86                         cause.toException()});
87                 }
88                 if (getStackTrace() != null)
89                 {
90                     // t.setStackTrace( getStackTrace());
91
}
92                 originalException = t;
93             }
94             catch (Exception JavaDoc e)
95             {
96                 throw new InstantiationException JavaDoc("Failed to create Exception from ExceptionBean: "
97                                                  + e.getMessage());
98             }
99         }
100         return originalException;
101     }
102
103     public String JavaDoc getDetailMessage()
104     {
105         return detailMessage;
106     }
107
108     public void setDetailMessage(String JavaDoc detailMessage)
109     {
110         this.detailMessage = detailMessage;
111     }
112
113     public ExceptionBean getCause()
114     {
115         return cause;
116     }
117
118     public void setCause(ExceptionBean cause)
119     {
120         this.cause = cause;
121     }
122
123     public String JavaDoc[] /* List */
124     getStackTrace()
125     {
126         return stackTrace;
127     }
128
129     // public void addStackTrace(String trace)
130
// {
131
// stackTrace.add(trace);
132
// }
133

134     public void setStackTrace(StackTraceElement JavaDoc[] stackTrace)
135     {
136         this.stackTrace = getStackAsString(stackTrace);
137     }
138
139     public void setStackTrace(String JavaDoc[] stackTrace)
140     {
141         this.stackTrace = stackTrace;
142     }
143
144     public String JavaDoc getExceptionClass()
145     {
146         return exceptionClass;
147     }
148
149     public void setExceptionClass(String JavaDoc exceptionClass)
150     {
151         this.exceptionClass = exceptionClass;
152     }
153
154     protected String JavaDoc[] getStackAsString(java.lang.StackTraceElement JavaDoc[] elements)
155     {
156         String JavaDoc[] trace = new String JavaDoc[elements.length];
157         for (int i = 0; i < elements.length; i++)
158         {
159             trace[i] = elements[i].toString();
160         }
161         return trace;
162     }
163 }
164
Popular Tags