org.jboss.util.ThrowableHandler Maven / Gradle / Ivy
/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.util;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
/**
* A static singleton that handles processing throwables that otherwise would
* be ignored or dumped to System.err.
*
* @version $Revision: 2787 $
* @author Jason Dillon
*/
public final class ThrowableHandler
{
/**
* Container for throwable types.
*/
public static interface Type
{
/** Unknown throwable. */
int UNKNOWN = 0;
/** Error throwable. */
int ERROR = 1;
/** Warning throwable. */
int WARNING = 2;
}
/////////////////////////////////////////////////////////////////////////
// Listener Methods //
/////////////////////////////////////////////////////////////////////////
/** The list of listeners */
protected static List listeners = Collections.synchronizedList(new ArrayList());
/**
* Add a ThrowableListener to the listener list. Listener is added only
* if if it is not already in the list.
*
* @param listener ThrowableListener to add to the list.
*/
public static void addThrowableListener(ThrowableListener listener) {
// only add the listener if it isn't already in the list
if (!listeners.contains(listener)) {
listeners.add(listener);
}
}
/**
* Remove a ThrowableListener from the listener list.
*
* @param listener ThrowableListener to remove from the list.
*/
public static void removeThrowableListener(ThrowableListener listener) {
listeners.remove(listener);
}
/**
* Fire onThrowable to all registered listeners.
*
* @param type The type off the throwable.
* @param t Throwable
*/
protected static void fireOnThrowable(int type, Throwable t) {
Object[] list = listeners.toArray();
for (int i=0; i