1 22 package org.jboss.util; 23 24 import java.util.List ; 25 import java.util.ArrayList ; 26 import java.util.Collections ; 27 28 35 public final class ThrowableHandler 36 { 37 40 public static interface Type 41 { 42 43 int UNKNOWN = 0; 44 45 46 int ERROR = 1; 47 48 49 int WARNING = 2; 50 } 51 52 53 57 58 protected static List listeners = Collections.synchronizedList(new ArrayList ()); 59 60 66 public static void addThrowableListener(ThrowableListener listener) { 67 if (!listeners.contains(listener)) { 69 listeners.add(listener); 70 } 71 } 72 73 78 public static void removeThrowableListener(ThrowableListener listener) { 79 listeners.remove(listener); 80 } 81 82 88 protected static void fireOnThrowable(int type, Throwable t) { 89 Object [] list = listeners.toArray(); 90 91 for (int i=0; i<list.length; i++) { 92 ((ThrowableListener)list[i]).onThrowable(type, t); 93 } 94 } 95 96 97 101 107 public static void add(int type, Throwable t) { 108 if (t == null) return; 110 111 try { 112 fireOnThrowable(type, t); 113 } 114 catch (Throwable bad) { 115 System.err.println("Unable to handle throwable: " + t + " because of:"); 117 bad.printStackTrace(); 118 } 119 } 120 121 126 public static void add(Throwable t) { 127 add(Type.UNKNOWN, t); 128 } 129 130 135 public static void addError(Throwable t) { 136 add(Type.ERROR, t); 137 } 138 139 144 public static void addWarning(Throwable t) { 145 add(Type.ERROR, t); 146 } 147 } 148 | Popular Tags |