View Javadoc

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