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