1 10 11 package org.mule.samples.errorhandler; 12 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 import java.util.Map ; 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 30 public class ErrorManager 31 { 32 33 private static transient Log logger = LogFactory.getLog(ErrorManager.class); 34 35 private Map handlers = new HashMap (); 36 private ExceptionHandler defaultHandler = null; 37 38 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 i = eh.getRegisteredClasses(); i.hasNext();) 59 { 60 handlers.put(i.next(), eh); 61 } 62 } 63 64 public ExceptionHandler getHandler(Class exceptionClass) 65 { 66 Object obj = handlers.get(exceptionClass); 67 if (obj == null) 68 { 69 obj = handlers.get(Throwable .class); 70 } 71 72 return (ExceptionHandler)obj; 73 } 74 75 public void onException(ErrorMessage msg) throws UMOException 76 { 77 Class 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 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 t) 117 { 118 ErrorMessage nestedMsg = null; 119 try 122 { 123 nestedMsg = new ErrorMessage(t); 124 } 125 catch (Exception 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 t) 145 { 146 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 |