001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.mina.util;
021
022/**
023 * Monitors uncaught exceptions.  {@link #exceptionCaught(Throwable)} is
024 * invoked when there are any uncaught exceptions.
025 * <p>
026 * You can monitor any uncaught exceptions by setting {@link ExceptionMonitor}
027 * by calling {@link #setInstance(ExceptionMonitor)}.  The default
028 * monitor logs all caught exceptions in <tt>WARN</tt> level using
029 * SLF4J.
030 *
031 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
032 *
033 * @see DefaultExceptionMonitor
034 */
035public abstract class ExceptionMonitor {
036    private static ExceptionMonitor instance = new DefaultExceptionMonitor();
037
038    /**
039     * @return the current exception monitor.
040     */
041    public static ExceptionMonitor getInstance() {
042        return instance;
043    }
044
045    /**
046     * Sets the uncaught exception monitor.  If <code>null</code> is specified,
047     * the default monitor will be set.
048     *
049     * @param monitor A new instance of {@link DefaultExceptionMonitor} is set
050     *                if <tt>null</tt> is specified.
051     */
052    public static void setInstance(ExceptionMonitor monitor) {
053        if (monitor == null) {
054            monitor = new DefaultExceptionMonitor();
055        }
056
057        instance = monitor;
058    }
059
060    /**
061     * Invoked when there are any uncaught exceptions.
062     * 
063     * @param cause The caught exception
064     */
065    public abstract void exceptionCaught(Throwable cause);
066}