01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.exception;
05:
06: import java.util.Iterator;
07: import java.util.LinkedList;
08: import java.util.List;
09:
10: /**
11: * Helper for extracting proximate cause and ultimate cause from exceptions.
12: */
13: public class ExceptionHelperImpl implements ExceptionHelper {
14:
15: private final List helpers = new LinkedList();
16: private final ExceptionHelper nullHelper = new NullExceptionHelper();
17:
18: public boolean accepts(Throwable t) {
19: return true;
20: }
21:
22: /**
23: * Add another helper to this helper.
24: * @param helper Helper
25: */
26: public void addHelper(ExceptionHelper helper) {
27: helpers.add(helper);
28: }
29:
30: public Throwable getProximateCause(Throwable t) {
31: return getHelperFor(t).getProximateCause(t);
32: }
33:
34: public Throwable getUltimateCause(Throwable t) {
35: Throwable rv = getProximateCause(t);
36: while (rv != getProximateCause(rv)) {
37: rv = getProximateCause(rv);
38: }
39: return rv;
40: //return getHelperFor(t).getUltimateCause(t);
41: }
42:
43: private ExceptionHelper getHelperFor(Throwable t) {
44: ExceptionHelper helper;
45: for (Iterator i = helpers.iterator(); i.hasNext();) {
46: helper = (ExceptionHelper) i.next();
47: if (helper.accepts(t))
48: return helper;
49: }
50: return nullHelper;
51: }
52:
53: private static final class NullExceptionHelper implements
54: ExceptionHelper {
55:
56: public boolean accepts(Throwable t) {
57: return true;
58: }
59:
60: public Throwable getProximateCause(Throwable t) {
61: return t;
62: }
63:
64: public Throwable getUltimateCause(Throwable t) {
65: return t;
66: }
67:
68: }
69:
70: }
|