1 10 11 package org.mule.samples.errorhandler; 12 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 16 22 public abstract class AbstractExceptionHandler implements ExceptionHandler 23 { 24 25 protected HashMap registry = new HashMap (); 26 27 private String endpointName; 28 29 protected ErrorManager errorManager = null; 30 31 public void registerException(Class exceptionClass) 32 { 33 34 registry.put(exceptionClass, exceptionClass); 35 36 } 37 38 public Iterator getRegisteredClasses() 39 { 40 return registry.keySet().iterator(); 41 } 42 43 public void unRegisterException(Class exceptionClass) 44 { 45 registry.remove(exceptionClass); 46 47 } 48 49 public boolean isRegisteredFor(Class exceptionClass) 50 { 51 Class aClass = null; 52 for (Iterator i = getRegisteredClasses(); i.hasNext();) 53 { 54 aClass = (Class )i.next(); 55 if (aClass.isAssignableFrom(exceptionClass)) 56 { 57 return true; 58 } 59 } 60 return false; 61 } 62 63 public void onException(ErrorMessage message) throws HandlerException 64 { 65 Throwable t = null; 66 67 try 68 { 69 t = message.getException().toException(); 70 } 71 catch (Exception e) 72 { 73 throw new HandlerException("Failed to retrieve exception from exception message: " + e, e); 74 } 75 76 if (!isRegisteredFor(t.getClass())) 77 { 78 throw new HandlerException( 79 "Exception: " + t.getClass().getName() + " was received by Exception behaviour: " 80 + getClass().getName() 81 + ", but the exception is not registered to be handled by this behaviour"); 82 } 83 processException(message, t); 84 } 85 86 protected abstract void processException(ErrorMessage message, Throwable t) throws HandlerException; 87 88 91 public ErrorManager getErrorManager() 92 { 93 return errorManager; 94 } 95 96 99 public void setErrorManager(ErrorManager errorManager) 100 { 101 this.errorManager = errorManager; 102 } 103 104 107 public String getendpointName() 108 { 109 return endpointName; 110 } 111 112 115 public void setEndpointName(String endpointName) 116 { 117 this.endpointName = endpointName; 118 } 119 120 } 121 | Popular Tags |