001: /*
002: * $Id: AbstractExceptionHandler.java 10789 2008-02-12 20:04:43Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.example.errorhandler;
012:
013: import java.util.HashMap;
014: import java.util.Iterator;
015:
016: /**
017: * <code>AbstractExceptionListener</code> TODO (document class)
018: *
019: */
020: public abstract class AbstractExceptionHandler implements
021: ExceptionHandler {
022:
023: protected HashMap registry = new HashMap();
024:
025: private String endpointName;
026:
027: protected ErrorManager errorManager = null;
028:
029: public void registerException(Class exceptionClass) {
030:
031: registry.put(exceptionClass, exceptionClass);
032:
033: }
034:
035: public Iterator getRegisteredClasses() {
036: return registry.keySet().iterator();
037: }
038:
039: public void unRegisterException(Class exceptionClass) {
040: registry.remove(exceptionClass);
041:
042: }
043:
044: public boolean isRegisteredFor(Class exceptionClass) {
045: Class aClass = null;
046: for (Iterator i = getRegisteredClasses(); i.hasNext();) {
047: aClass = (Class) i.next();
048: if (aClass.isAssignableFrom(exceptionClass)) {
049: return true;
050: }
051: }
052: return false;
053: }
054:
055: public void onException(ErrorMessage message)
056: throws HandlerException {
057: Throwable t = null;
058:
059: try {
060: t = message.getException().toException();
061: } catch (Exception e) {
062: throw new HandlerException(LocaleMessage
063: .unretrievedException(e), e);
064: }
065:
066: if (!isRegisteredFor(t.getClass())) {
067: throw new HandlerException(LocaleMessage
068: .unhandledException(t.getClass(), this .getClass()));
069: }
070: processException(message, t);
071: }
072:
073: protected abstract void processException(ErrorMessage message,
074: Throwable t) throws HandlerException;
075:
076: /**
077: * @return Returns the errorManager.
078: */
079: public ErrorManager getErrorManager() {
080: return errorManager;
081: }
082:
083: /**
084: * @param errorManager The errorManager to set.
085: */
086: public void setErrorManager(ErrorManager errorManager) {
087: this .errorManager = errorManager;
088: }
089:
090: /**
091: * @return Returns the endpointName.
092: */
093: public String getendpointName() {
094: return endpointName;
095: }
096:
097: /**
098: * @param endpointName The endpointName to set.
099: */
100: public void setEndpointName(String endpointName) {
101: this.endpointName = endpointName;
102: }
103:
104: }
|