1
2
3
4
5
6
7
8
9
10
11 package org.mule.samples.errorhandler;
12
13 import org.mule.util.ClassUtils;
14
15
16
17
18 public class ExceptionBean
19 {
20
21
22
23
24
25 private String detailMessage;
26
27
28
29
30
31
32
33 private ExceptionBean cause = null;
34
35
36
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
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[]
121 getStackTrace()
122 {
123 return stackTrace;
124 }
125
126
127
128
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 }