View Javadoc

1   /*
2    * $Id: ErrorManager.java 7976 2007-08-21 14:26:13Z dirk.olmes $
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.samples.errorhandler;
12  
13  import org.mule.MuleManager;
14  import org.mule.samples.errorhandler.handlers.DefaultHandler;
15  import org.mule.samples.errorhandler.handlers.FatalHandler;
16  import org.mule.umo.UMOException;
17  
18  import java.util.HashMap;
19  import java.util.Iterator;
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 transient Log logger = LogFactory.getLog(ErrorManager.class);
32  
33      private Map handlers = new HashMap();
34      private ExceptionHandler defaultHandler = null;
35  
36      /*
37       * (non-Javadoc)
38       * 
39       * @see org.mule.impl.MuleUMO#initialise(java.util.Properties)
40       */
41      public ErrorManager()
42      {
43          defaultHandler = new DefaultHandler();
44      }
45  
46      public void setHandlers(ExceptionHandler[] eh)
47      {
48          for (int i = 0; i < eh.length; i++)
49          {
50              addHandler(eh[i]);
51          }
52      }
53  
54      public void addHandler(ExceptionHandler eh)
55      {
56          for (Iterator i = eh.getRegisteredClasses(); i.hasNext();)
57          {
58              handlers.put(i.next(), eh);
59          }
60      }
61  
62      public ExceptionHandler getHandler(Class exceptionClass)
63      {
64          Object obj = handlers.get(exceptionClass);
65          if (obj == null)
66          {
67              obj = handlers.get(Throwable.class);
68          }
69  
70          return (ExceptionHandler)obj;
71      }
72  
73      public void onException(ErrorMessage msg) throws UMOException
74      {
75          Class eClass = null;
76          ExceptionHandler eh = null;
77  
78          try
79          {
80              eClass = msg.getException().toException().getClass();
81              eh = getHandler(eClass);
82              eh.onException(msg);
83          }
84          catch (Exception e)
85          {
86          
87              logger.error(LocaleMessage.handlerFailure(eh));
88  
89              if (eh instanceof DefaultHandler)
90              {
91                  logger.error(LocaleMessage.defaultFatalHandling(FatalHandler.class));
92                  handleFatal(e);
93  
94              }
95              else if (eh instanceof FatalHandler)
96              {
97                  logger.fatal(LocaleMessage.fatalHandling(e));
98                  ((MuleManager)MuleManager.getInstance()).shutdown(e, false);
99              }
100             else
101             {
102                 logger.error(LocaleMessage.defaultHandling(DefaultHandler.class, eh, e));
103                 handleDefault(msg, e);
104             }
105         }
106     }
107 
108     private void handleDefault(ErrorMessage msg, Throwable t)
109     {
110         ErrorMessage nestedMsg = null;
111         // Try wrapping the exception and the Exception message that caused the
112         // exception in a new message
113         try
114         {
115             nestedMsg = new ErrorMessage(t);
116         }
117         catch (Exception e)
118         {
119             logger.fatal(LocaleMessage.defaultException(e), e);
120             handleFatal(e);
121         }
122         try
123         {
124             defaultHandler.onException(nestedMsg);
125         }
126         catch (HandlerException e)
127         {
128             logger.fatal(LocaleMessage.defaultHandlerException(e), e);
129             handleFatal(e);
130         }
131 
132     }
133 
134     private void handleFatal(Throwable t)
135     {
136         // If this method has been called, all other handlers failed
137         // this is all we can do
138         logger.fatal(LocaleMessage.fatalException(t), t);
139         ((MuleManager)MuleManager.getInstance()).shutdown(t, false);
140     }
141 }