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