1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.example.errorhandler; |
12 | |
|
13 | |
import org.mule.util.ClassUtils; |
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
public class ExceptionBean |
19 | |
{ |
20 | 0 | private static boolean showRootStackOnly = true; |
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
private String detailMessage; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | 0 | private ExceptionBean cause = null; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
private String[] stackTrace; |
41 | |
|
42 | 0 | private String exceptionClass = null; |
43 | |
|
44 | 0 | private Throwable originalException = null; |
45 | |
|
46 | |
public ExceptionBean() |
47 | |
{ |
48 | 0 | super(); |
49 | 0 | } |
50 | |
|
51 | |
public ExceptionBean(Throwable exception) |
52 | 0 | { |
53 | 0 | if (exception == null) throw new IllegalArgumentException("The exception cannot be null"); |
54 | 0 | originalException = exception; |
55 | 0 | exceptionClass = exception.getClass().getName(); |
56 | 0 | setDetailMessage(exception.getMessage()); |
57 | 0 | setStackTrace((showRootStackOnly ? null : getStackAsString(exception.getStackTrace()))); |
58 | 0 | if (exception.getCause() != null) |
59 | |
{ |
60 | 0 | setCause(new ExceptionBean(exception.getCause())); |
61 | |
} |
62 | |
else |
63 | |
{ |
64 | 0 | setStackTrace(exception.getStackTrace()); |
65 | |
} |
66 | 0 | } |
67 | |
|
68 | |
public Throwable toException() throws InstantiationException |
69 | |
{ |
70 | 0 | if (originalException == null) |
71 | |
{ |
72 | 0 | Throwable t = null; |
73 | |
try |
74 | |
{ |
75 | 0 | Class aClass = ClassUtils.loadClass(exceptionClass, getClass()); |
76 | 0 | if (cause == null) |
77 | |
{ |
78 | 0 | t = (Throwable)ClassUtils.instanciateClass(aClass, new Object[]{getDetailMessage()}); |
79 | |
} |
80 | |
else |
81 | |
{ |
82 | 0 | t = (Throwable)ClassUtils.instanciateClass(aClass, new Object[]{getDetailMessage(), |
83 | |
cause.toException()}); |
84 | |
} |
85 | 0 | if (getStackTrace() != null) |
86 | |
{ |
87 | |
|
88 | |
} |
89 | 0 | originalException = t; |
90 | |
} |
91 | 0 | catch (Exception e) |
92 | |
{ |
93 | 0 | throw new InstantiationException("Failed to create Exception from ExceptionBean: " |
94 | |
+ e.getMessage()); |
95 | 0 | } |
96 | |
} |
97 | 0 | return originalException; |
98 | |
} |
99 | |
|
100 | |
public String getDetailMessage() |
101 | |
{ |
102 | 0 | return detailMessage; |
103 | |
} |
104 | |
|
105 | |
public void setDetailMessage(String detailMessage) |
106 | |
{ |
107 | 0 | this.detailMessage = detailMessage; |
108 | 0 | } |
109 | |
|
110 | |
public ExceptionBean getCause() |
111 | |
{ |
112 | 0 | return cause; |
113 | |
} |
114 | |
|
115 | |
public void setCause(ExceptionBean cause) |
116 | |
{ |
117 | 0 | this.cause = cause; |
118 | 0 | } |
119 | |
|
120 | |
public String[] |
121 | |
getStackTrace() |
122 | |
{ |
123 | 0 | return stackTrace; |
124 | |
} |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
public void setStackTrace(StackTraceElement[] stackTrace) |
132 | |
{ |
133 | 0 | this.stackTrace = getStackAsString(stackTrace); |
134 | 0 | } |
135 | |
|
136 | |
public void setStackTrace(String[] stackTrace) |
137 | |
{ |
138 | 0 | this.stackTrace = stackTrace; |
139 | 0 | } |
140 | |
|
141 | |
public String getExceptionClass() |
142 | |
{ |
143 | 0 | return exceptionClass; |
144 | |
} |
145 | |
|
146 | |
public void setExceptionClass(String exceptionClass) |
147 | |
{ |
148 | 0 | this.exceptionClass = exceptionClass; |
149 | 0 | } |
150 | |
|
151 | |
protected String[] getStackAsString(java.lang.StackTraceElement[] elements) |
152 | |
{ |
153 | 0 | String[] trace = new String[elements.length]; |
154 | 0 | for (int i = 0; i < elements.length; i++) |
155 | |
{ |
156 | 0 | trace[i] = elements[i].toString(); |
157 | |
} |
158 | 0 | return trace; |
159 | |
} |
160 | |
} |