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  
  * $Id: ErrorManager.java 19191 2010-08-25 21:05:23Z tcarlson $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.example.errorhandler;
 12  
 
 13  
 import org.mule.api.MuleException;
 14  
 import org.mule.example.errorhandler.handlers.DefaultHandler;
 15  
 import org.mule.example.errorhandler.handlers.FatalHandler;
 16  
 
 17  
 import java.util.HashMap;
 18  
 import java.util.Iterator;
 19  
 import java.util.List;
 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  
     public ErrorManager()
 37  0
     {
 38  0
         defaultHandler = new DefaultHandler();
 39  0
     }
 40  
 
 41  
     public void setHandlers(List handlers)
 42  
     {
 43  0
         Iterator handlerIter = handlers.iterator();
 44  0
         while (handlerIter.hasNext())
 45  
         {
 46  0
             ExceptionHandler handler = (ExceptionHandler)handlerIter.next();
 47  0
             this.addHandler(handler);
 48  0
         }
 49  0
     }
 50  
 
 51  
     public void addHandler(ExceptionHandler eh)
 52  
     {
 53  0
         for (Iterator i = eh.getRegisteredClasses(); i.hasNext();)
 54  
         {
 55  0
             handlers.put(i.next(), eh);
 56  
         }
 57  0
     }
 58  
 
 59  
     public ExceptionHandler getHandler(Class exceptionClass)
 60  
     {
 61  0
         Object obj = handlers.get(exceptionClass);
 62  0
         if (obj == null)
 63  
         {
 64  0
             obj = handlers.get(Throwable.class);
 65  
         }
 66  
 
 67  0
         return (ExceptionHandler)obj;
 68  
     }
 69  
 
 70  
     public void onException(ErrorMessage msg) throws MuleException
 71  
     {
 72  0
         Class eClass = null;
 73  0
         ExceptionHandler eh = null;
 74  
 
 75  
         try
 76  
         {
 77  0
             eClass = msg.getException().toException().getClass();
 78  0
             eh = getHandler(eClass);
 79  0
             eh.onException(msg);
 80  
         }
 81  0
         catch (Exception e)
 82  
         {
 83  0
             logger.error(e);
 84  
 
 85  0
             if (eh instanceof DefaultHandler)
 86  
             {
 87  0
                 logger.error(LocaleMessage.defaultFatalHandling(FatalHandler.class));
 88  0
                 handleFatal(e);
 89  
 
 90  
             }
 91  0
             else if (eh instanceof FatalHandler)
 92  
             {
 93  0
                 logger.fatal(LocaleMessage.fatalHandling(e));
 94  
             }
 95  
             else
 96  
             {
 97  0
                 logger.error(LocaleMessage.defaultHandling(DefaultHandler.class, eh, e));
 98  0
                 handleDefault(msg, e);
 99  
             }
 100  0
         }
 101  0
     }
 102  
 
 103  
     private void handleDefault(ErrorMessage msg, Throwable t)
 104  
     {
 105  0
         ErrorMessage nestedMsg = null;
 106  
         // Try wrapping the exception and the Exception message that caused the
 107  
         // exception in a new message
 108  
         try
 109  
         {
 110  0
             nestedMsg = new ErrorMessage(t);
 111  
         }
 112  0
         catch (Exception e)
 113  
         {
 114  0
             logger.fatal(LocaleMessage.defaultException(e), e);
 115  0
             handleFatal(e);
 116  0
         }
 117  
         try
 118  
         {
 119  0
             defaultHandler.onException(nestedMsg);
 120  
         }
 121  0
         catch (HandlerException e)
 122  
         {
 123  0
             logger.fatal(LocaleMessage.defaultHandlerException(e), e);
 124  0
             handleFatal(e);
 125  0
         }
 126  
 
 127  0
     }
 128  
 
 129  
     private void handleFatal(Throwable t)
 130  
     {
 131  
         // If this method has been called, all other handlers failed
 132  
         // this is all we can do
 133  0
         logger.fatal(LocaleMessage.fatalException(t), t);
 134  0
     }
 135  
 }