KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > exception > ExceptionHelperImpl


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.exception;
5
6 import java.util.Iterator JavaDoc;
7 import java.util.LinkedList JavaDoc;
8 import java.util.List JavaDoc;
9
10 /**
11  * Helper for extracting proximate cause and ultimate cause from exceptions.
12  */

13 public class ExceptionHelperImpl implements ExceptionHelper {
14   
15   private final List JavaDoc helpers = new LinkedList JavaDoc();
16   private final ExceptionHelper nullHelper = new NullExceptionHelper();
17   
18   public boolean accepts(Throwable JavaDoc t) {
19     return true;
20   }
21   
22   public void addHelper(ExceptionHelper helper) {
23     helpers.add(helper);
24   }
25
26   public Throwable JavaDoc getProximateCause(Throwable JavaDoc t) {
27     return getHelperFor(t).getProximateCause(t);
28   }
29
30   public Throwable JavaDoc getUltimateCause(Throwable JavaDoc t) {
31     Throwable JavaDoc rv = getProximateCause(t);
32     while (rv != getProximateCause(rv)) {
33       rv = getProximateCause(rv);
34     }
35     return rv;
36     //return getHelperFor(t).getUltimateCause(t);
37
}
38   
39   private ExceptionHelper getHelperFor(Throwable JavaDoc t) {
40     ExceptionHelper helper;
41     for (Iterator JavaDoc 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 JavaDoc t) {
51       return true;
52     }
53     
54     public Throwable JavaDoc getProximateCause(Throwable JavaDoc t) {
55       return t;
56     }
57
58     public Throwable JavaDoc getUltimateCause(Throwable JavaDoc t) {
59       return t;
60     }
61     
62   }
63
64 }
65
Popular Tags