KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > samples > errorhandler > ErrorManager


1 /*
2  * $Id: ErrorManager.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
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 java.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19 import org.mule.MuleManager;
20 import org.mule.samples.errorhandler.handlers.DefaultHandler;
21 import org.mule.samples.errorhandler.handlers.FatalHandler;
22 import org.mule.umo.UMOException;
23
24 /**
25  * <code>ErrorManager</code> TODO (document class)
26  *
27  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
28  * @version $Revision: 3798 $
29  */

30 public class ErrorManager
31 {
32     /** logger used by this class */
33     private static transient Log logger = LogFactory.getLog(ErrorManager.class);
34
35     private Map JavaDoc handlers = new HashMap JavaDoc();
36     private ExceptionHandler defaultHandler = null;
37
38     /*
39      * (non-Javadoc)
40      *
41      * @see org.mule.impl.MuleUMO#initialise(java.util.Properties)
42      */

43     public ErrorManager()
44     {
45         defaultHandler = new DefaultHandler();
46     }
47
48     public void setHandlers(ExceptionHandler[] eh)
49     {
50         for (int i = 0; i < eh.length; i++)
51         {
52             addHandler(eh[i]);
53         }
54     }
55
56     public void addHandler(ExceptionHandler eh)
57     {
58         for (Iterator JavaDoc i = eh.getRegisteredClasses(); i.hasNext();)
59         {
60             handlers.put(i.next(), eh);
61         }
62     }
63
64     public ExceptionHandler getHandler(Class JavaDoc exceptionClass)
65     {
66         Object JavaDoc obj = handlers.get(exceptionClass);
67         if (obj == null)
68         {
69             obj = handlers.get(Throwable JavaDoc.class);
70         }
71
72         return (ExceptionHandler)obj;
73     }
74
75     public void onException(ErrorMessage msg) throws UMOException
76     {
77         Class JavaDoc eClass = null;
78         ExceptionHandler eh = null;
79
80         try
81         {
82             eClass = msg.getException().toException().getClass();
83             eh = getHandler(eClass);
84             eh.onException(msg);
85         }
86         catch (Exception JavaDoc e)
87         {
88             logger.error("Failed to handle Exception using handler: "
89                          + (eh != null ? (eh.getClass().getName() + " : " + e) : "null"));
90
91             if (eh instanceof DefaultHandler)
92             {
93                 logger.error("As the failure happened in the Default Exception handler, now using Fatal Behaviour "
94                              + FatalHandler.class.getName()
95                              + " which will cause the Exception Manager to shutdown");
96
97                 handleFatal(e);
98
99             }
100             else if (eh instanceof FatalHandler)
101             {
102                 logger.fatal("Exception caught handling Fatal exception: " + e);
103                 ((MuleManager)MuleManager.getInstance()).shutdown(e, false);
104             }
105             else
106             {
107                 logger.error("Exception Handler resorting to Default Behaviour : "
108                              + DefaultHandler.class.getName()
109                              + ", due to exception in configured behavour : "
110                              + (eh != null ? (eh.getClass().getName() + " : " + e) : "null"));
111                 handleDefault(msg, e);
112             }
113         }
114     }
115
116     private void handleDefault(ErrorMessage msg, Throwable JavaDoc t)
117     {
118         ErrorMessage nestedMsg = null;
119         // Try wrapping the exception and the Exception message that caused the
120
// exception in a new message
121
try
122         {
123             nestedMsg = new ErrorMessage(t);
124         }
125         catch (Exception JavaDoc e)
126         {
127             logger.fatal("Exception happened while handling and exception using the Default behaviour: " + e,
128                 e);
129             handleFatal(e);
130         }
131         try
132         {
133             defaultHandler.onException(nestedMsg);
134         }
135         catch (HandlerException e)
136         {
137             logger.fatal("Exception happened while handling and exception using the Default behaviour: " + e,
138                 e);
139             handleFatal(e);
140         }
141
142     }
143
144     private void handleFatal(Throwable JavaDoc t)
145     {
146         // If this method has been called, all other handlers failed
147
// this is all we can do
148
logger.fatal("An exception has been caught be the Fatal Exception Behaviour");
149         logger.fatal("Exception is: " + t, t);
150         ((MuleManager)MuleManager.getInstance()).shutdown(t, false);
151     }
152 }
153
Popular Tags