KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > ThrowableHandler


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util;
23
24 import java.util.List JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collections JavaDoc;
27
28 /**
29  * A static singleton that handles processing throwables that otherwise would
30  * be ignored or dumped to System.err.
31  *
32  * @version <tt>$Revision: 1958 $</tt>
33  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
34  */

35 public final class ThrowableHandler
36 {
37    /**
38     * Container for throwable types.
39     */

40    public static interface Type
41    {
42       /** Unknown throwable. */
43       int UNKNOWN = 0;
44
45       /** Error throwable. */
46       int ERROR = 1;
47
48       /** Warning throwable. */
49       int WARNING = 2;
50    }
51    
52
53    /////////////////////////////////////////////////////////////////////////
54
// Listener Methods //
55
/////////////////////////////////////////////////////////////////////////
56

57    /** The list of listeners */
58    protected static List JavaDoc listeners = Collections.synchronizedList(new ArrayList JavaDoc());
59
60    /**
61     * Add a ThrowableListener to the listener list. Listener is added only
62     * if if it is not already in the list.
63     *
64     * @param listener ThrowableListener to add to the list.
65     */

66    public static void addThrowableListener(ThrowableListener listener) {
67       // only add the listener if it isn't already in the list
68
if (!listeners.contains(listener)) {
69          listeners.add(listener);
70       }
71    }
72
73    /**
74     * Remove a ThrowableListener from the listener list.
75     *
76     * @param listener ThrowableListener to remove from the list.
77     */

78    public static void removeThrowableListener(ThrowableListener listener) {
79       listeners.remove(listener);
80    }
81
82    /**
83     * Fire onThrowable to all registered listeners.
84     *
85     * @param type The type off the throwable.
86     * @param t Throwable
87     */

88    protected static void fireOnThrowable(int type, Throwable JavaDoc t) {
89       Object JavaDoc[] 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    /////////////////////////////////////////////////////////////////////////
98
// Throwable Processing //
99
/////////////////////////////////////////////////////////////////////////
100

101    /**
102     * Add a throwable that is to be handled.
103     *
104     * @param type The type off the throwable.
105     * @param t Throwable to be handled.
106     */

107    public static void add(int type, Throwable JavaDoc t) {
108       // don't add null throwables
109
if (t == null) return;
110
111       try {
112          fireOnThrowable(type, t);
113       }
114       catch (Throwable JavaDoc bad) {
115          // don't let these propagate, that could introduce unwanted side-effects
116
System.err.println("Unable to handle throwable: " + t + " because of:");
117          bad.printStackTrace();
118       }
119    }
120       
121    /**
122     * Add a throwable that is to be handled with unknown type.
123     *
124     * @param t Throwable to be handled.
125     */

126    public static void add(Throwable JavaDoc t) {
127       add(Type.UNKNOWN, t);
128    }
129
130    /**
131     * Add a throwable that is to be handled with error type.
132     *
133     * @param t Throwable to be handled.
134     */

135    public static void addError(Throwable JavaDoc t) {
136       add(Type.ERROR, t);
137    }
138
139    /**
140     * Add a throwable that is to be handled with warning type.
141     *
142     * @param t Throwable to be handled.
143     */

144    public static void addWarning(Throwable JavaDoc t) {
145       add(Type.ERROR, t);
146    }
147 }
148
Popular Tags