1 4 package com.tc.exception; 5 6 import java.util.Iterator ; 7 import java.util.LinkedList ; 8 import java.util.List ; 9 10 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 public void addHelper(ExceptionHelper helper) { 23 helpers.add(helper); 24 } 25 26 public Throwable getProximateCause(Throwable t) { 27 return getHelperFor(t).getProximateCause(t); 28 } 29 30 public Throwable getUltimateCause(Throwable t) { 31 Throwable rv = getProximateCause(t); 32 while (rv != getProximateCause(rv)) { 33 rv = getProximateCause(rv); 34 } 35 return rv; 36 } 38 39 private ExceptionHelper getHelperFor(Throwable t) { 40 ExceptionHelper helper; 41 for (Iterator i = helpers.iterator(); i.hasNext(); ) { 42 helper = (ExceptionHelper) i.next(); 43 if (helper.accepts(t)) return helper; 44 } 45 return nullHelper; 46 } 47 48 private static final class NullExceptionHelper implements ExceptionHelper{ 49 50 public boolean accepts(Throwable t) { 51 return true; 52 } 53 54 public Throwable getProximateCause(Throwable t) { 55 return t; 56 } 57 58 public Throwable getUltimateCause(Throwable t) { 59 return t; 60 } 61 62 } 63 64 } 65 | Popular Tags |