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