View Javadoc
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      private static final Log logger = LogFactory.getLog(ErrorManager.class);
28  
29      private Map handlers = new HashMap();
30      private ExceptionHandler defaultHandler = null;
31  
32      public ErrorManager()
33      {
34          defaultHandler = new DefaultHandler();
35      }
36  
37      public void setHandlers(List handlers)
38      {
39          Iterator handlerIter = handlers.iterator();
40          while (handlerIter.hasNext())
41          {
42              ExceptionHandler handler = (ExceptionHandler)handlerIter.next();
43              this.addHandler(handler);
44          }
45      }
46  
47      public void addHandler(ExceptionHandler eh)
48      {
49          for (Iterator i = eh.getRegisteredClasses(); i.hasNext();)
50          {
51              handlers.put(i.next(), eh);
52          }
53      }
54  
55      public ExceptionHandler getHandler(Class exceptionClass)
56      {
57          Object obj = handlers.get(exceptionClass);
58          if (obj == null)
59          {
60              obj = handlers.get(Throwable.class);
61          }
62  
63          return (ExceptionHandler)obj;
64      }
65  
66      public void onException(ErrorMessage msg) throws MuleException
67      {
68          Class eClass = null;
69          ExceptionHandler eh = null;
70  
71          try
72          {
73              eClass = msg.getException().toException().getClass();
74              eh = getHandler(eClass);
75              eh.onException(msg);
76          }
77          catch (Exception e)
78          {
79              logger.error(e);
80  
81              if (eh instanceof DefaultHandler)
82              {
83                  logger.error(LocaleMessage.defaultFatalHandling(FatalHandler.class));
84                  handleFatal(e);
85  
86              }
87              else if (eh instanceof FatalHandler)
88              {
89                  logger.fatal(LocaleMessage.fatalHandling(e));
90              }
91              else
92              {
93                  logger.error(LocaleMessage.defaultHandling(DefaultHandler.class, eh, e));
94                  handleDefault(msg, e);
95              }
96          }
97      }
98  
99      private void handleDefault(ErrorMessage msg, Throwable t)
100     {
101         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             nestedMsg = new ErrorMessage(t);
107         }
108         catch (Exception e)
109         {
110             logger.fatal(LocaleMessage.defaultException(e), e);
111             handleFatal(e);
112         }
113         try
114         {
115             defaultHandler.onException(nestedMsg);
116         }
117         catch (HandlerException e)
118         {
119             logger.fatal(LocaleMessage.defaultHandlerException(e), e);
120             handleFatal(e);
121         }
122 
123     }
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         logger.fatal(LocaleMessage.fatalException(t), t);
130     }
131 }