Coverage Report - org.mule.samples.errorhandler.ErrorManager
 
Classes in this File Line Coverage Branch Coverage Complexity
ErrorManager
0%
0/48
0%
0/16
2.143
 
 1  
 /*
 2  
  * $Id: ErrorManager.java 8040 2007-08-24 11:41:28Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.samples.errorhandler;
 12  
 
 13  
 import org.mule.MuleManager;
 14  
 import org.mule.samples.errorhandler.handlers.DefaultHandler;
 15  
 import org.mule.samples.errorhandler.handlers.FatalHandler;
 16  
 import org.mule.umo.UMOException;
 17  
 
 18  
 import java.util.HashMap;
 19  
 import java.util.Iterator;
 20  
 import java.util.Map;
 21  
 
 22  
 import org.apache.commons.logging.Log;
 23  
 import org.apache.commons.logging.LogFactory;
 24  
 
 25  
 /**
 26  
  * <code>ErrorManager</code> TODO (document class)
 27  
  */
 28  
 public class ErrorManager
 29  
 {
 30  
     /** logger used by this class */
 31  0
     private static final Log logger = LogFactory.getLog(ErrorManager.class);
 32  
 
 33  0
     private Map handlers = new HashMap();
 34  0
     private ExceptionHandler defaultHandler = null;
 35  
 
 36  
     /*
 37  
      * (non-Javadoc)
 38  
      * 
 39  
      * @see org.mule.impl.MuleUMO#initialise(java.util.Properties)
 40  
      */
 41  
     public ErrorManager()
 42  0
     {
 43  0
         defaultHandler = new DefaultHandler();
 44  0
     }
 45  
 
 46  
     public void setHandlers(ExceptionHandler[] eh)
 47  
     {
 48  0
         for (int i = 0; i < eh.length; i++)
 49  
         {
 50  0
             addHandler(eh[i]);
 51  
         }
 52  0
     }
 53  
 
 54  
     public void addHandler(ExceptionHandler eh)
 55  
     {
 56  0
         for (Iterator i = eh.getRegisteredClasses(); i.hasNext();)
 57  
         {
 58  0
             handlers.put(i.next(), eh);
 59  
         }
 60  0
     }
 61  
 
 62  
     public ExceptionHandler getHandler(Class exceptionClass)
 63  
     {
 64  0
         Object obj = handlers.get(exceptionClass);
 65  0
         if (obj == null)
 66  
         {
 67  0
             obj = handlers.get(Throwable.class);
 68  
         }
 69  
 
 70  0
         return (ExceptionHandler)obj;
 71  
     }
 72  
 
 73  
     public void onException(ErrorMessage msg) throws UMOException
 74  
     {
 75  0
         Class eClass = null;
 76  0
         ExceptionHandler eh = null;
 77  
 
 78  
         try
 79  
         {
 80  0
             eClass = msg.getException().toException().getClass();
 81  0
             eh = getHandler(eClass);
 82  0
             eh.onException(msg);
 83  
         }
 84  0
         catch (Exception e)
 85  
         {
 86  
         
 87  0
             logger.error(LocaleMessage.handlerFailure(eh));
 88  
 
 89  0
             if (eh instanceof DefaultHandler)
 90  
             {
 91  0
                 logger.error(LocaleMessage.defaultFatalHandling(FatalHandler.class));
 92  0
                 handleFatal(e);
 93  
 
 94  
             }
 95  0
             else if (eh instanceof FatalHandler)
 96  
             {
 97  0
                 logger.fatal(LocaleMessage.fatalHandling(e));
 98  0
                 ((MuleManager)MuleManager.getInstance()).dispose();
 99  
             }
 100  
             else
 101  
             {
 102  0
                 logger.error(LocaleMessage.defaultHandling(DefaultHandler.class, eh, e));
 103  0
                 handleDefault(msg, e);
 104  
             }
 105  0
         }
 106  0
     }
 107  
 
 108  
     private void handleDefault(ErrorMessage msg, Throwable t)
 109  
     {
 110  0
         ErrorMessage nestedMsg = null;
 111  
         // Try wrapping the exception and the Exception message that caused the
 112  
         // exception in a new message
 113  
         try
 114  
         {
 115  0
             nestedMsg = new ErrorMessage(t);
 116  
         }
 117  0
         catch (Exception e)
 118  
         {
 119  0
             logger.fatal(LocaleMessage.defaultException(e), e);
 120  0
             handleFatal(e);
 121  0
         }
 122  
         try
 123  
         {
 124  0
             defaultHandler.onException(nestedMsg);
 125  
         }
 126  0
         catch (HandlerException e)
 127  
         {
 128  0
             logger.fatal(LocaleMessage.defaultHandlerException(e), e);
 129  0
             handleFatal(e);
 130  0
         }
 131  
 
 132  0
     }
 133  
 
 134  
     private void handleFatal(Throwable t)
 135  
     {
 136  
         // If this method has been called, all other handlers failed
 137  
         // this is all we can do
 138  0
         logger.fatal(LocaleMessage.fatalException(t), t);
 139  0
         ((MuleManager)MuleManager.getInstance()).dispose();
 140  0
     }
 141  
 }