1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.api; |
12 | |
|
13 | |
import java.io.PrintWriter; |
14 | |
import java.io.StringWriter; |
15 | |
import java.util.HashMap; |
16 | |
import java.util.Iterator; |
17 | |
import java.util.Map; |
18 | |
|
19 | |
import org.mule.config.DefaultMuleConfiguration; |
20 | |
import org.mule.config.ExceptionHelper; |
21 | |
import org.mule.config.i18n.CoreMessages; |
22 | |
import org.mule.config.i18n.Message; |
23 | |
import org.mule.config.i18n.MessageFactory; |
24 | |
import org.mule.util.StringUtils; |
25 | |
import org.mule.util.SystemUtils; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
public abstract class MuleException extends Exception |
32 | |
{ |
33 | 0 | private final Map info = new HashMap(); |
34 | 0 | private int errorCode = -1; |
35 | 0 | private String message = null; |
36 | |
private Message i18nMessage; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public MuleException(Message message) |
42 | |
{ |
43 | 0 | super(); |
44 | 0 | setMessage(message); |
45 | 0 | } |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
public MuleException(Message message, Throwable cause) |
52 | |
{ |
53 | 0 | super(ExceptionHelper.unwrap(cause)); |
54 | 0 | setMessage(message); |
55 | 0 | } |
56 | |
|
57 | |
public MuleException(Throwable cause) |
58 | |
{ |
59 | 0 | super(ExceptionHelper.unwrap(cause)); |
60 | 0 | if (cause != null) |
61 | |
{ |
62 | 0 | setMessage(MessageFactory.createStaticMessage(cause.getMessage() + " (" |
63 | |
+ cause.getClass().getName() + ")")); |
64 | |
} |
65 | |
else |
66 | |
{ |
67 | 0 | initialise(); |
68 | |
} |
69 | 0 | } |
70 | |
|
71 | |
protected MuleException() |
72 | |
{ |
73 | 0 | super(); |
74 | 0 | initialise(); |
75 | 0 | } |
76 | |
|
77 | |
protected void setMessage(Message message) |
78 | |
{ |
79 | 0 | initialise(); |
80 | 0 | this.message = message.getMessage(); |
81 | 0 | i18nMessage = message; |
82 | 0 | } |
83 | |
|
84 | |
protected void setMessage(String message) |
85 | |
{ |
86 | 0 | initialise(); |
87 | 0 | this.message = message; |
88 | 0 | if (i18nMessage == null) |
89 | |
{ |
90 | 0 | i18nMessage = MessageFactory.createStaticMessage(message); |
91 | |
} |
92 | 0 | } |
93 | |
|
94 | |
public int getExceptionCode() |
95 | |
{ |
96 | 0 | return errorCode; |
97 | |
} |
98 | |
|
99 | |
public Message getI18nMessage() |
100 | |
{ |
101 | 0 | return i18nMessage; |
102 | |
} |
103 | |
|
104 | |
public int getMessageCode() |
105 | |
{ |
106 | 0 | return (i18nMessage == null ? 0 : i18nMessage.getCode()); |
107 | |
} |
108 | |
|
109 | |
public void addInfo(String name, Object info) |
110 | |
{ |
111 | 0 | this.info.put(name, info); |
112 | 0 | } |
113 | |
|
114 | |
protected void appendMessage(String s) |
115 | |
{ |
116 | 0 | message += s; |
117 | 0 | } |
118 | |
|
119 | |
protected void prependMessage(String s) |
120 | |
{ |
121 | 0 | message = message + ". " + s; |
122 | 0 | } |
123 | |
|
124 | |
protected void setExceptionCode(int code) |
125 | |
{ |
126 | 0 | errorCode = code; |
127 | 0 | } |
128 | |
|
129 | |
@Override |
130 | |
public final String getMessage() |
131 | |
{ |
132 | 0 | return message; |
133 | |
} |
134 | |
|
135 | |
protected void initialise() |
136 | |
{ |
137 | 0 | setExceptionCode(ExceptionHelper.getErrorCode(getClass())); |
138 | 0 | String javadoc = ExceptionHelper.getJavaDocUrl(getClass()); |
139 | 0 | String doc = ExceptionHelper.getDocUrl(getClass()); |
140 | 0 | if (javadoc != null) |
141 | |
{ |
142 | |
|
143 | 0 | info.put("JavaDoc", javadoc); |
144 | |
} |
145 | 0 | if (doc != null) |
146 | |
{ |
147 | |
|
148 | 0 | info.put("Other Doc", doc); |
149 | |
} |
150 | 0 | } |
151 | |
|
152 | |
public String getDetailedMessage() |
153 | |
{ |
154 | 0 | if (DefaultMuleConfiguration.verboseExceptions) |
155 | |
{ |
156 | 0 | return getVerboseMessage(); |
157 | |
} |
158 | |
else |
159 | |
{ |
160 | 0 | return getSummaryMessage(); |
161 | |
} |
162 | |
} |
163 | |
|
164 | |
public String getVerboseMessage() |
165 | |
{ |
166 | 0 | MuleException e = ExceptionHelper.getRootMuleException(this); |
167 | 0 | if (!e.equals(this)) |
168 | |
{ |
169 | 0 | return getMessage(); |
170 | |
} |
171 | 0 | StringBuffer buf = new StringBuffer(1024); |
172 | 0 | buf.append(SystemUtils.LINE_SEPARATOR).append(StringUtils.repeat('*', 80)).append( |
173 | |
SystemUtils.LINE_SEPARATOR); |
174 | 0 | buf.append("Message : ").append(message).append(SystemUtils.LINE_SEPARATOR); |
175 | 0 | buf.append("Type : ") |
176 | |
.append(getClass().getName()) |
177 | |
.append(SystemUtils.LINE_SEPARATOR); |
178 | 0 | buf.append("Code : ").append("MULE_ERROR-").append( |
179 | |
getExceptionCode() + getMessageCode()).append(SystemUtils.LINE_SEPARATOR); |
180 | |
|
181 | |
|
182 | |
|
183 | 0 | Map info = ExceptionHelper.getExceptionInfo(this); |
184 | 0 | for (Iterator iterator = info.keySet().iterator(); iterator.hasNext();) |
185 | |
{ |
186 | 0 | String s = (String) iterator.next(); |
187 | 0 | int pad = 22 - s.length(); |
188 | 0 | buf.append(s); |
189 | 0 | if (pad > 0) |
190 | |
{ |
191 | 0 | buf.append(StringUtils.repeat(' ', pad)); |
192 | |
} |
193 | 0 | buf.append(": "); |
194 | 0 | buf.append(info.get(s)).append(SystemUtils.LINE_SEPARATOR); |
195 | 0 | } |
196 | |
|
197 | |
|
198 | 0 | buf.append(StringUtils.repeat('*', 80)).append(SystemUtils.LINE_SEPARATOR); |
199 | 0 | buf.append(CoreMessages.exceptionStackIs()).append(SystemUtils.LINE_SEPARATOR); |
200 | 0 | buf.append(StringUtils.abbreviate(ExceptionHelper.getExceptionStack(this), 5000)); |
201 | |
|
202 | 0 | buf.append(StringUtils.repeat('*', 80)).append(SystemUtils.LINE_SEPARATOR); |
203 | 0 | buf.append(CoreMessages.rootStackTrace()).append(SystemUtils.LINE_SEPARATOR); |
204 | 0 | Throwable root = ExceptionHelper.getRootException(this); |
205 | 0 | StringWriter w = new StringWriter(); |
206 | 0 | PrintWriter p = new PrintWriter(w); |
207 | 0 | root.printStackTrace(p); |
208 | 0 | buf.append(StringUtils.abbreviate(w.toString(), 5000)).append(SystemUtils.LINE_SEPARATOR); |
209 | 0 | buf.append(StringUtils.repeat('*', 80)).append(SystemUtils.LINE_SEPARATOR); |
210 | |
|
211 | 0 | return buf.toString(); |
212 | |
} |
213 | |
|
214 | |
public String getSummaryMessage() |
215 | |
{ |
216 | 0 | MuleException e = ExceptionHelper.getRootMuleException(this); |
217 | 0 | if (!e.equals(this)) |
218 | |
{ |
219 | 0 | return getMessage(); |
220 | |
} |
221 | 0 | StringBuffer buf = new StringBuffer(1024); |
222 | 0 | buf.append(SystemUtils.LINE_SEPARATOR).append(StringUtils.repeat('*', 80)).append( |
223 | |
SystemUtils.LINE_SEPARATOR); |
224 | 0 | buf.append("Message : ").append(message).append(SystemUtils.LINE_SEPARATOR); |
225 | 0 | buf.append("Code : ").append("MULE_ERROR-").append( |
226 | |
getExceptionCode() + getMessageCode()).append(SystemUtils.LINE_SEPARATOR); |
227 | |
|
228 | 0 | buf.append(StringUtils.repeat('-', 80)).append(SystemUtils.LINE_SEPARATOR); |
229 | 0 | buf.append(CoreMessages.exceptionStackIs()).append(SystemUtils.LINE_SEPARATOR); |
230 | 0 | buf.append(StringUtils.abbreviate(ExceptionHelper.getExceptionStack(this), 5000)); |
231 | |
|
232 | 0 | buf.append(StringUtils.repeat('-', 80)).append(SystemUtils.LINE_SEPARATOR); |
233 | 0 | buf.append(CoreMessages.rootStackTrace()).append(SystemUtils.LINE_SEPARATOR); |
234 | 0 | Throwable root = ExceptionHelper.getRootException(this); |
235 | 0 | Throwable rootSummary = ExceptionHelper.summarise(root, 3); |
236 | 0 | StringWriter w = new StringWriter(); |
237 | 0 | PrintWriter p = new PrintWriter(w); |
238 | 0 | rootSummary.printStackTrace(p); |
239 | 0 | buf.append(StringUtils.abbreviate(w.toString(), 5000)); |
240 | 0 | buf.append( |
241 | |
" + " |
242 | |
+ root.getStackTrace().length |
243 | |
+ " more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)") |
244 | |
.append(SystemUtils.LINE_SEPARATOR); |
245 | 0 | buf.append(StringUtils.repeat('*', 80)).append(SystemUtils.LINE_SEPARATOR); |
246 | |
|
247 | 0 | return buf.toString(); |
248 | |
} |
249 | |
|
250 | |
@Override |
251 | |
public boolean equals(Object o) |
252 | |
{ |
253 | 0 | if (this == o) |
254 | |
{ |
255 | 0 | return true; |
256 | |
} |
257 | 0 | if (!(o instanceof MuleException)) |
258 | |
{ |
259 | 0 | return false; |
260 | |
} |
261 | |
|
262 | 0 | final MuleException exception = (MuleException) o; |
263 | |
|
264 | 0 | if (errorCode != exception.errorCode) |
265 | |
{ |
266 | 0 | return false; |
267 | |
} |
268 | 0 | if (i18nMessage != null ? !i18nMessage.equals(exception.i18nMessage) : exception.i18nMessage != null) |
269 | |
{ |
270 | 0 | return false; |
271 | |
} |
272 | 0 | if (message != null ? !message.equals(exception.message) : exception.message != null) |
273 | |
{ |
274 | 0 | return false; |
275 | |
} |
276 | |
|
277 | 0 | return true; |
278 | |
} |
279 | |
|
280 | |
@Override |
281 | |
public int hashCode() |
282 | |
{ |
283 | |
int result; |
284 | 0 | result = errorCode; |
285 | 0 | result = 29 * result + (message != null ? message.hashCode() : 0); |
286 | 0 | result = 29 * result + (i18nMessage != null ? i18nMessage.hashCode() : 0); |
287 | 0 | return result; |
288 | |
} |
289 | |
|
290 | |
public Map getInfo() |
291 | |
{ |
292 | 0 | return info; |
293 | |
} |
294 | |
} |