View Javadoc

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