src/core/net/sf/basedb/util/error/ClassMapErrorHandler.java

Code
Comments
Other
Rev Date Author Line
2751 20 Oct 06 nicklas 1 /**
2751 20 Oct 06 nicklas 2   $Id$
2751 20 Oct 06 nicklas 3
3675 16 Aug 07 jari 4   Copyright (C) 2006 Nicklas Nordborg
2751 20 Oct 06 nicklas 5
2751 20 Oct 06 nicklas 6   This file is part of BASE - BioArray Software Environment.
2751 20 Oct 06 nicklas 7   Available at http://base.thep.lu.se/
2751 20 Oct 06 nicklas 8
2751 20 Oct 06 nicklas 9   BASE is free software; you can redistribute it and/or
2751 20 Oct 06 nicklas 10   modify it under the terms of the GNU General Public License
4479 05 Sep 08 jari 11   as published by the Free Software Foundation; either version 3
2751 20 Oct 06 nicklas 12   of the License, or (at your option) any later version.
2751 20 Oct 06 nicklas 13
2751 20 Oct 06 nicklas 14   BASE is distributed in the hope that it will be useful,
2751 20 Oct 06 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
2751 20 Oct 06 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2751 20 Oct 06 nicklas 17   GNU General Public License for more details.
2751 20 Oct 06 nicklas 18
2751 20 Oct 06 nicklas 19   You should have received a copy of the GNU General Public License
4515 11 Sep 08 jari 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
2751 20 Oct 06 nicklas 21 */
2751 20 Oct 06 nicklas 22 package net.sf.basedb.util.error;
2751 20 Oct 06 nicklas 23
2751 20 Oct 06 nicklas 24 import java.util.ArrayList;
2751 20 Oct 06 nicklas 25 import java.util.HashMap;
2751 20 Oct 06 nicklas 26 import java.util.List;
2751 20 Oct 06 nicklas 27 import java.util.Map;
2751 20 Oct 06 nicklas 28
2751 20 Oct 06 nicklas 29 /**
2751 20 Oct 06 nicklas 30   Error handler that allows the registration of one or more
2751 20 Oct 06 nicklas 31   error handler for subclasses to <code>Throwable</code>.
2751 20 Oct 06 nicklas 32   <p>
2751 20 Oct 06 nicklas 33   Use the {@link #addErrorHandler(Class, ErrorHandler)} to register
2751 20 Oct 06 nicklas 34   error handlers for specific exceptions. When the {@link #handleError(Throwable)}
2751 20 Oct 06 nicklas 35   is invoked it checks the class of the throwable and looks for error handlers
2751 20 Oct 06 nicklas 36   for that class. If it can't find an error handler it will check the superclass
2751 20 Oct 06 nicklas 37   and so on. If no error handler is found the orginal error is re-thrown.
2751 20 Oct 06 nicklas 38
2751 20 Oct 06 nicklas 39   @author nicklas
2751 20 Oct 06 nicklas 40   @version 2.0
2751 20 Oct 06 nicklas 41   @base.modified $Date$
2751 20 Oct 06 nicklas 42 */
2751 20 Oct 06 nicklas 43 public class ClassMapErrorHandler
2751 20 Oct 06 nicklas 44   implements ErrorHandler
2751 20 Oct 06 nicklas 45 {
2751 20 Oct 06 nicklas 46
2751 20 Oct 06 nicklas 47   /**
2751 20 Oct 06 nicklas 48     Map of registered error handlers.
2751 20 Oct 06 nicklas 49   */
2751 20 Oct 06 nicklas 50   private Map<Class<? extends Throwable>, List<ErrorHandler>> errorHandlers;
2751 20 Oct 06 nicklas 51   private final ErrorHandler defaultErrorHandler;
2751 20 Oct 06 nicklas 52
2751 20 Oct 06 nicklas 53   /**
2751 20 Oct 06 nicklas 54     Create a new <code>ClassMapErrorHandler</code>.
2751 20 Oct 06 nicklas 55     
2751 20 Oct 06 nicklas 56     @param defaultErrorHandler The default error handler to use if no
2751 20 Oct 06 nicklas 57       other error handler is found, if null the error is re-thrown
2751 20 Oct 06 nicklas 58   */
2751 20 Oct 06 nicklas 59   public ClassMapErrorHandler(ErrorHandler defaultErrorHandler)
2751 20 Oct 06 nicklas 60   {
2751 20 Oct 06 nicklas 61     this.defaultErrorHandler = defaultErrorHandler;
2751 20 Oct 06 nicklas 62     this.errorHandlers = new HashMap<Class<? extends Throwable>, List<ErrorHandler>>();
2751 20 Oct 06 nicklas 63   }
2751 20 Oct 06 nicklas 64   
2751 20 Oct 06 nicklas 65   /**
2751 20 Oct 06 nicklas 66     Add an error handler for throwable:s of a specific class. The
2751 20 Oct 06 nicklas 67     error handler will also handle errors of all subclasses unless
2751 20 Oct 06 nicklas 68     a specific error handler exists for that subclass.
2751 20 Oct 06 nicklas 69     
2751 20 Oct 06 nicklas 70     @param t The class of throwable:s the error handler should handle
2751 20 Oct 06 nicklas 71     @param handler The error handler
2751 20 Oct 06 nicklas 72   */
2751 20 Oct 06 nicklas 73   public void addErrorHandler(Class<? extends Throwable> t, ErrorHandler handler)
2751 20 Oct 06 nicklas 74   {
2751 20 Oct 06 nicklas 75     List<ErrorHandler> handlers = errorHandlers.get(t);
2751 20 Oct 06 nicklas 76     if (handlers == null) 
2751 20 Oct 06 nicklas 77     {
2751 20 Oct 06 nicklas 78       handlers = new ArrayList<ErrorHandler>();
2751 20 Oct 06 nicklas 79       errorHandlers.put(t, handlers);
2751 20 Oct 06 nicklas 80     }
2751 20 Oct 06 nicklas 81     handlers.add(0, handler);
2751 20 Oct 06 nicklas 82   }
2751 20 Oct 06 nicklas 83
2751 20 Oct 06 nicklas 84   /*
2751 20 Oct 06 nicklas 85     From the ErrorHandler interface
2751 20 Oct 06 nicklas 86     -------------------------------------------
2751 20 Oct 06 nicklas 87   */
6127 14 Sep 12 nicklas 88   @Override
2751 20 Oct 06 nicklas 89   public boolean handleError(Throwable t) 
2751 20 Oct 06 nicklas 90     throws Throwable
2751 20 Oct 06 nicklas 91   {
6875 20 Apr 15 nicklas 92     Class<?> c = t.getClass();
2751 20 Oct 06 nicklas 93     List<ErrorHandler> handlers = null;
2751 20 Oct 06 nicklas 94     do
2751 20 Oct 06 nicklas 95     {
2751 20 Oct 06 nicklas 96       handlers = errorHandlers.get(c);
2751 20 Oct 06 nicklas 97       if (handlers != null)
2751 20 Oct 06 nicklas 98       {
2751 20 Oct 06 nicklas 99         for (ErrorHandler handler : handlers)
2751 20 Oct 06 nicklas 100         {
2751 20 Oct 06 nicklas 101           if (handler.handleError(t)) return true;
2751 20 Oct 06 nicklas 102         }
2751 20 Oct 06 nicklas 103         handlers = null;
2751 20 Oct 06 nicklas 104       }
2751 20 Oct 06 nicklas 105       c = c.getSuperclass();
2751 20 Oct 06 nicklas 106     } while (c != null && handlers == null);
2751 20 Oct 06 nicklas 107     if (defaultErrorHandler != null && defaultErrorHandler.handleError(t)) return true;
2751 20 Oct 06 nicklas 108     throw t;
2751 20 Oct 06 nicklas 109   }
2751 20 Oct 06 nicklas 110   // -------------------------------------------
2751 20 Oct 06 nicklas 111
2751 20 Oct 06 nicklas 112 }