1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.config; |
12 | |
|
13 | |
import org.mule.api.MuleException; |
14 | |
import org.mule.api.MuleRuntimeException; |
15 | |
import org.mule.api.config.ExceptionReader; |
16 | |
import org.mule.api.registry.ServiceType; |
17 | |
import org.mule.config.i18n.CoreMessages; |
18 | |
import org.mule.util.ClassUtils; |
19 | |
import org.mule.util.MapUtils; |
20 | |
import org.mule.util.SpiUtils; |
21 | |
|
22 | |
import java.io.IOException; |
23 | |
import java.io.InputStream; |
24 | |
import java.lang.reflect.InvocationTargetException; |
25 | |
import java.util.ArrayList; |
26 | |
import java.util.HashMap; |
27 | |
import java.util.Iterator; |
28 | |
import java.util.List; |
29 | |
import java.util.Map; |
30 | |
import java.util.Properties; |
31 | |
|
32 | |
import org.apache.commons.logging.Log; |
33 | |
import org.apache.commons.logging.LogFactory; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
public final class ExceptionHelper |
45 | |
{ |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
public static final String ERROR_CODE_PROPERTY = "error.code.property"; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | 0 | protected static final Log logger = LogFactory.getLog(ExceptionHelper.class); |
57 | |
|
58 | 0 | private static String J2SE_VERSION = ""; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
private static final String J2EE_VERSION = "1.3ee"; |
64 | |
|
65 | 0 | private static Properties errorDocs = new Properties(); |
66 | 0 | private static Properties errorCodes = new Properties(); |
67 | 0 | private static Map reverseErrorCodes = null; |
68 | 0 | private static Map errorMappings = new HashMap(); |
69 | |
|
70 | 0 | private static int exceptionThreshold = 0; |
71 | 0 | private static boolean verbose = true; |
72 | |
|
73 | 0 | private static boolean initialised = false; |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | 0 | private static List<ExceptionReader> exceptionReaders = new ArrayList<ExceptionReader>(); |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | 0 | private static ExceptionReader defaultExceptionReader = new DefaultExceptionReader(); |
84 | |
|
85 | |
static |
86 | |
{ |
87 | 0 | initialise(); |
88 | 0 | } |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
private ExceptionHelper() |
94 | 0 | { |
95 | |
|
96 | 0 | } |
97 | |
|
98 | |
private static void initialise() |
99 | |
{ |
100 | |
try |
101 | |
{ |
102 | 0 | if (initialised) |
103 | |
{ |
104 | 0 | return; |
105 | |
} |
106 | |
|
107 | 0 | registerExceptionReader(new MuleExceptionReader()); |
108 | 0 | registerExceptionReader(new NamingExceptionReader()); |
109 | 0 | J2SE_VERSION = System.getProperty("java.specification.version"); |
110 | |
|
111 | 0 | String name = SpiUtils.SERVICE_ROOT + ServiceType.EXCEPTION.getPath() |
112 | |
+ "/mule-exception-codes.properties"; |
113 | 0 | InputStream in = ExceptionHelper.class.getClassLoader().getResourceAsStream(name); |
114 | 0 | if (in == null) |
115 | |
{ |
116 | 0 | throw new IllegalArgumentException("Failed to load resource: " + name); |
117 | |
} |
118 | 0 | errorCodes.load(in); |
119 | 0 | in.close(); |
120 | |
|
121 | 0 | reverseErrorCodes = MapUtils.invertMap(errorCodes); |
122 | |
|
123 | 0 | name = SpiUtils.SERVICE_ROOT + ServiceType.EXCEPTION.getPath() |
124 | |
+ "/mule-exception-config.properties"; |
125 | 0 | in = ExceptionHelper.class.getClassLoader().getResourceAsStream(name); |
126 | 0 | if (in == null) |
127 | |
{ |
128 | 0 | throw new IllegalArgumentException("Failed to load resource: " + name); |
129 | |
} |
130 | 0 | errorDocs.load(in); |
131 | 0 | in.close(); |
132 | |
|
133 | 0 | initialised = true; |
134 | |
} |
135 | 0 | catch (Exception e) |
136 | |
{ |
137 | 0 | throw new MuleRuntimeException(CoreMessages.failedToLoad("Exception resources"), e); |
138 | 0 | } |
139 | 0 | } |
140 | |
|
141 | |
public static int getErrorCode(Class exception) |
142 | |
{ |
143 | 0 | String code = errorCodes.getProperty(exception.getName(), "-1"); |
144 | 0 | return Integer.parseInt(code); |
145 | |
} |
146 | |
|
147 | |
public static Class getErrorClass(int code) |
148 | |
{ |
149 | 0 | String key = String.valueOf(code); |
150 | 0 | Object clazz = reverseErrorCodes.get(key); |
151 | 0 | if (clazz == null) |
152 | |
{ |
153 | 0 | return null; |
154 | |
} |
155 | 0 | else if (clazz instanceof Class) |
156 | |
{ |
157 | 0 | return (Class) clazz; |
158 | |
} |
159 | |
else |
160 | |
{ |
161 | |
try |
162 | |
{ |
163 | 0 | clazz = ClassUtils.loadClass(clazz.toString(), ExceptionHelper.class); |
164 | |
} |
165 | 0 | catch (ClassNotFoundException e) |
166 | |
{ |
167 | 0 | logger.error(e.getMessage(), e); |
168 | 0 | return null; |
169 | 0 | } |
170 | 0 | reverseErrorCodes.put(key, clazz); |
171 | 0 | return (Class) clazz; |
172 | |
} |
173 | |
} |
174 | |
|
175 | |
private static Properties getErrorMappings(String protocol) |
176 | |
{ |
177 | 0 | Object m = errorMappings.get(protocol); |
178 | 0 | if (m != null) |
179 | |
{ |
180 | 0 | if (m instanceof Properties) |
181 | |
{ |
182 | 0 | return (Properties) m; |
183 | |
} |
184 | |
else |
185 | |
{ |
186 | 0 | return null; |
187 | |
} |
188 | |
} |
189 | |
else |
190 | |
{ |
191 | 0 | String name = SpiUtils.SERVICE_ROOT + ServiceType.EXCEPTION.getPath() + "/" + protocol + "-exception-mappings.properties"; |
192 | 0 | InputStream in = ExceptionHelper.class.getClassLoader().getResourceAsStream(name); |
193 | 0 | if (in == null) |
194 | |
{ |
195 | 0 | errorMappings.put(protocol, "not found"); |
196 | 0 | if (logger.isDebugEnabled()) |
197 | |
{ |
198 | 0 | logger.debug("Failed to load error mappings from: " + name |
199 | |
+ " This may be because there are no error code mappings for protocol: " |
200 | |
+ protocol); |
201 | |
} |
202 | 0 | return null; |
203 | |
} |
204 | |
|
205 | 0 | Properties p = new Properties(); |
206 | |
try |
207 | |
{ |
208 | 0 | p.load(in); |
209 | 0 | in.close(); |
210 | |
} |
211 | 0 | catch (IOException iox) |
212 | |
{ |
213 | 0 | throw new IllegalArgumentException("Failed to load resource: " + name); |
214 | 0 | } |
215 | |
|
216 | 0 | errorMappings.put(protocol, p); |
217 | 0 | return p; |
218 | |
} |
219 | |
} |
220 | |
|
221 | |
public static String getErrorCodePropertyName(String protocol) |
222 | |
{ |
223 | 0 | protocol = protocol.toLowerCase(); |
224 | 0 | Properties mappings = getErrorMappings(protocol); |
225 | 0 | if (mappings == null) |
226 | |
{ |
227 | 0 | return null; |
228 | |
} |
229 | 0 | return mappings.getProperty(ERROR_CODE_PROPERTY); |
230 | |
} |
231 | |
|
232 | |
public static String getErrorMapping(String protocol, Class exception) |
233 | |
{ |
234 | 0 | protocol = protocol.toLowerCase(); |
235 | 0 | Properties mappings = getErrorMappings(protocol); |
236 | 0 | if (mappings == null) |
237 | |
{ |
238 | 0 | logger.info("No mappings found for protocol: " + protocol); |
239 | 0 | return String.valueOf(getErrorCode(exception)); |
240 | |
} |
241 | |
|
242 | 0 | Class clazz = exception; |
243 | 0 | String code = null; |
244 | 0 | while (!clazz.equals(Object.class)) |
245 | |
{ |
246 | 0 | code = mappings.getProperty(clazz.getName()); |
247 | 0 | if (code == null) |
248 | |
{ |
249 | 0 | clazz = clazz.getSuperclass(); |
250 | |
} |
251 | |
else |
252 | |
{ |
253 | 0 | return code; |
254 | |
} |
255 | |
} |
256 | 0 | code = String.valueOf(getErrorCode(exception)); |
257 | |
|
258 | |
|
259 | 0 | return mappings.getProperty(code, code); |
260 | |
} |
261 | |
|
262 | |
public static String getJavaDocUrl(Class<?> exception) |
263 | |
{ |
264 | 0 | return getDocUrl("javadoc.", exception.getName()); |
265 | |
} |
266 | |
|
267 | |
public static String getDocUrl(Class<?> exception) |
268 | |
{ |
269 | 0 | return getDocUrl("doc.", exception.getName()); |
270 | |
} |
271 | |
|
272 | |
private static String getDocUrl(String prefix, String packageName) |
273 | |
{ |
274 | 0 | String key = prefix; |
275 | 0 | if (packageName.startsWith("java.") || packageName.startsWith("javax.")) |
276 | |
{ |
277 | 0 | key += J2SE_VERSION; |
278 | |
} |
279 | 0 | String url = getUrl(key, packageName); |
280 | 0 | if (url == null && (packageName.startsWith("java.") || packageName.startsWith("javax."))) |
281 | |
{ |
282 | 0 | key = prefix + J2EE_VERSION; |
283 | 0 | url = getUrl(key, packageName); |
284 | |
} |
285 | 0 | if (url != null) |
286 | |
{ |
287 | 0 | if (!url.endsWith("/")) |
288 | |
{ |
289 | 0 | url += "/"; |
290 | |
} |
291 | 0 | String s = packageName.replaceAll("[.]", "/"); |
292 | 0 | s += ".html"; |
293 | 0 | url += s; |
294 | |
} |
295 | 0 | return url; |
296 | |
} |
297 | |
|
298 | |
private static String getUrl(String key, String packageName) |
299 | |
{ |
300 | 0 | String url = null; |
301 | 0 | if (!key.endsWith(".")) |
302 | |
{ |
303 | 0 | key += "."; |
304 | |
} |
305 | 0 | while (packageName.length() > 0) |
306 | |
{ |
307 | 0 | url = errorDocs.getProperty(key + packageName, null); |
308 | 0 | if (url == null) |
309 | |
{ |
310 | 0 | int i = packageName.lastIndexOf("."); |
311 | 0 | if (i == -1) |
312 | |
{ |
313 | 0 | packageName = ""; |
314 | |
} |
315 | |
else |
316 | |
{ |
317 | 0 | packageName = packageName.substring(0, i); |
318 | |
} |
319 | 0 | } |
320 | |
else |
321 | |
{ |
322 | |
break; |
323 | |
} |
324 | |
} |
325 | 0 | return url; |
326 | |
} |
327 | |
|
328 | |
public static Throwable getRootException(Throwable t) |
329 | |
{ |
330 | 0 | Throwable cause = t; |
331 | 0 | Throwable root = null; |
332 | 0 | while (cause != null) |
333 | |
{ |
334 | 0 | root = cause; |
335 | 0 | cause = getExceptionReader(cause).getCause(cause); |
336 | |
|
337 | 0 | if (t == cause) |
338 | |
{ |
339 | 0 | break; |
340 | |
} |
341 | |
} |
342 | |
|
343 | 0 | return DefaultMuleConfiguration.fullStackTraces ? root : sanitize(root); |
344 | |
} |
345 | |
|
346 | |
|
347 | |
|
348 | |
|
349 | |
|
350 | |
public static Throwable sanitize(Throwable t) |
351 | |
{ |
352 | 0 | StackTraceElement[] trace = t.getStackTrace(); |
353 | 0 | List<StackTraceElement> newTrace = new ArrayList<StackTraceElement>(); |
354 | 0 | for (StackTraceElement stackTraceElement : trace) |
355 | |
{ |
356 | 0 | if (isMuleInternalClass(stackTraceElement.getClassName())) |
357 | |
{ |
358 | 0 | newTrace.add(stackTraceElement); |
359 | |
} |
360 | |
} |
361 | |
|
362 | 0 | StackTraceElement[] clean = new StackTraceElement[newTrace.size()]; |
363 | 0 | newTrace.toArray(clean); |
364 | 0 | t.setStackTrace(clean); |
365 | 0 | return t; |
366 | |
} |
367 | |
|
368 | |
|
369 | |
|
370 | |
|
371 | |
|
372 | |
|
373 | |
public static Throwable summarise(Throwable t, int depth) |
374 | |
{ |
375 | 0 | t = sanitize(t); |
376 | 0 | StackTraceElement[] trace = t.getStackTrace(); |
377 | |
|
378 | 0 | int newStackDepth = Math.min(trace.length, depth); |
379 | 0 | StackTraceElement[] newTrace = new StackTraceElement[newStackDepth]; |
380 | |
|
381 | 0 | System.arraycopy(trace, 0, newTrace, 0, newStackDepth); |
382 | 0 | t.setStackTrace(newTrace); |
383 | |
|
384 | 0 | return t; |
385 | |
} |
386 | |
|
387 | |
private static boolean isMuleInternalClass(String className) |
388 | |
{ |
389 | |
|
390 | |
|
391 | |
|
392 | |
|
393 | 0 | for (String mulePackage : DefaultMuleConfiguration.stackTraceFilter) |
394 | |
{ |
395 | 0 | if (className.startsWith(mulePackage)) |
396 | |
{ |
397 | 0 | return false; |
398 | |
} |
399 | |
} |
400 | 0 | return true; |
401 | |
} |
402 | |
|
403 | |
public static Throwable getRootParentException(Throwable t) |
404 | |
{ |
405 | 0 | Throwable cause = t; |
406 | 0 | Throwable parent = t; |
407 | 0 | while (cause != null) |
408 | |
{ |
409 | 0 | if (cause.getCause() == null) |
410 | |
{ |
411 | 0 | return parent; |
412 | |
} |
413 | 0 | parent = cause; |
414 | 0 | cause = getExceptionReader(cause).getCause(cause); |
415 | |
|
416 | 0 | if (t == cause) |
417 | |
{ |
418 | 0 | break; |
419 | |
} |
420 | |
} |
421 | 0 | return t; |
422 | |
} |
423 | |
|
424 | |
public static MuleException getRootMuleException(Throwable t) |
425 | |
{ |
426 | 0 | Throwable cause = t; |
427 | 0 | MuleException exception = null; |
428 | 0 | while (cause != null) |
429 | |
{ |
430 | 0 | if (cause instanceof MuleException) |
431 | |
{ |
432 | 0 | exception = (MuleException) cause; |
433 | |
} |
434 | 0 | cause = getExceptionReader(cause).getCause(cause); |
435 | |
|
436 | 0 | if (t == cause) |
437 | |
{ |
438 | 0 | break; |
439 | |
} |
440 | |
} |
441 | 0 | return exception; |
442 | |
} |
443 | |
|
444 | |
public static List getExceptionsAsList(Throwable t) |
445 | |
{ |
446 | 0 | List exceptions = new ArrayList(); |
447 | 0 | Throwable cause = t; |
448 | 0 | while (cause != null) |
449 | |
{ |
450 | 0 | exceptions.add(0, cause); |
451 | 0 | cause = getExceptionReader(cause).getCause(cause); |
452 | |
|
453 | 0 | if (t == cause) |
454 | |
{ |
455 | 0 | break; |
456 | |
} |
457 | |
} |
458 | 0 | return exceptions; |
459 | |
} |
460 | |
|
461 | |
public static Map getExceptionInfo(Throwable t) |
462 | |
{ |
463 | 0 | Map info = new HashMap(); |
464 | 0 | Throwable cause = t; |
465 | 0 | while (cause != null) |
466 | |
{ |
467 | 0 | info.putAll(getExceptionReader(cause).getInfo(cause)); |
468 | 0 | cause = getExceptionReader(cause).getCause(cause); |
469 | |
|
470 | 0 | if (t == cause) |
471 | |
{ |
472 | 0 | break; |
473 | |
} |
474 | |
} |
475 | 0 | return info; |
476 | |
} |
477 | |
|
478 | |
public static String getExceptionStack(Throwable t) |
479 | |
{ |
480 | 0 | StringBuffer buf = new StringBuffer(); |
481 | |
|
482 | 0 | List exceptions = getExceptionsAsList(t); |
483 | |
|
484 | 0 | int i = 1; |
485 | 0 | for (Iterator iterator = exceptions.iterator(); iterator.hasNext(); i++) |
486 | |
{ |
487 | 0 | if (i > exceptionThreshold && exceptionThreshold > 0) |
488 | |
{ |
489 | 0 | buf.append("(").append(exceptions.size() - i + 1).append(" more...)"); |
490 | 0 | break; |
491 | |
} |
492 | 0 | Throwable throwable = (Throwable) iterator.next(); |
493 | 0 | ExceptionReader er = getExceptionReader(throwable); |
494 | 0 | buf.append(i).append(". ").append(er.getMessage(throwable)).append(" ("); |
495 | 0 | buf.append(throwable.getClass().getName()).append(")\n"); |
496 | 0 | if (verbose && throwable.getStackTrace().length > 0) |
497 | |
{ |
498 | 0 | StackTraceElement e = throwable.getStackTrace()[0]; |
499 | 0 | buf.append(" ") |
500 | |
.append(e.getClassName()) |
501 | |
.append(":") |
502 | |
.append(e.getLineNumber()) |
503 | |
.append(" (") |
504 | |
.append(getJavaDocUrl(throwable.getClass())) |
505 | |
.append(")\n"); |
506 | |
} |
507 | |
} |
508 | 0 | return buf.toString(); |
509 | |
} |
510 | |
|
511 | |
|
512 | |
|
513 | |
|
514 | |
|
515 | |
|
516 | |
public static void registerExceptionReader(ExceptionReader reader) |
517 | |
{ |
518 | 0 | exceptionReaders.add(reader); |
519 | 0 | } |
520 | |
|
521 | |
|
522 | |
|
523 | |
|
524 | |
|
525 | |
|
526 | |
|
527 | |
|
528 | |
public static ExceptionReader getExceptionReader(Throwable t) |
529 | |
{ |
530 | 0 | for (ExceptionReader exceptionReader : exceptionReaders) |
531 | |
{ |
532 | 0 | if (exceptionReader.getExceptionType().isInstance(t)) |
533 | |
{ |
534 | 0 | return exceptionReader; |
535 | |
} |
536 | |
} |
537 | 0 | return defaultExceptionReader; |
538 | |
} |
539 | |
|
540 | |
public static String writeException(Throwable t) |
541 | |
{ |
542 | 0 | ExceptionReader er = getExceptionReader(t); |
543 | 0 | StringBuffer msg = new StringBuffer(); |
544 | 0 | msg.append(er.getMessage(t)).append(". Type: ").append(t.getClass()); |
545 | 0 | return msg.toString(); |
546 | |
} |
547 | |
|
548 | |
public static <T extends Throwable>T unwrap(T t) |
549 | |
{ |
550 | 0 | if(t instanceof InvocationTargetException) |
551 | |
{ |
552 | 0 | return (T)((InvocationTargetException)t).getTargetException(); |
553 | |
} |
554 | 0 | return t; |
555 | |
|
556 | |
} |
557 | |
} |