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