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, getDetailMessage()); |
79 | |
} |
80 | |
else |
81 | |
{ |
82 | 0 | t = (Throwable)ClassUtils.instanciateClass(aClass, getDetailMessage(), cause.toException()); |
83 | |
} |
84 | 0 | if (getStackTrace() != null) |
85 | |
{ |
86 | |
|
87 | |
} |
88 | 0 | originalException = t; |
89 | |
} |
90 | 0 | catch (Exception e) |
91 | |
{ |
92 | 0 | throw new InstantiationException("Failed to create Exception from ExceptionBean: " |
93 | |
+ e.getMessage()); |
94 | 0 | } |
95 | |
} |
96 | 0 | return originalException; |
97 | |
} |
98 | |
|
99 | |
public String getDetailMessage() |
100 | |
{ |
101 | 0 | return detailMessage; |
102 | |
} |
103 | |
|
104 | |
public void setDetailMessage(String detailMessage) |
105 | |
{ |
106 | 0 | this.detailMessage = detailMessage; |
107 | 0 | } |
108 | |
|
109 | |
public ExceptionBean getCause() |
110 | |
{ |
111 | 0 | return cause; |
112 | |
} |
113 | |
|
114 | |
public void setCause(ExceptionBean cause) |
115 | |
{ |
116 | 0 | this.cause = cause; |
117 | 0 | } |
118 | |
|
119 | |
public String[] |
120 | |
getStackTrace() |
121 | |
{ |
122 | 0 | return stackTrace; |
123 | |
} |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
public void setStackTrace(StackTraceElement[] stackTrace) |
131 | |
{ |
132 | 0 | this.stackTrace = getStackAsString(stackTrace); |
133 | 0 | } |
134 | |
|
135 | |
public void setStackTrace(String[] stackTrace) |
136 | |
{ |
137 | 0 | this.stackTrace = stackTrace; |
138 | 0 | } |
139 | |
|
140 | |
public String getExceptionClass() |
141 | |
{ |
142 | 0 | return exceptionClass; |
143 | |
} |
144 | |
|
145 | |
public void setExceptionClass(String exceptionClass) |
146 | |
{ |
147 | 0 | this.exceptionClass = exceptionClass; |
148 | 0 | } |
149 | |
|
150 | |
protected String[] getStackAsString(java.lang.StackTraceElement[] elements) |
151 | |
{ |
152 | 0 | String[] trace = new String[elements.length]; |
153 | 0 | for (int i = 0; i < elements.length; i++) |
154 | |
{ |
155 | 0 | trace[i] = elements[i].toString(); |
156 | |
} |
157 | 0 | return trace; |
158 | |
} |
159 | |
} |