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