View Javadoc

1   /*
2    * $Id: AbstractExceptionHandler.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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 java.util.HashMap;
14  import java.util.Iterator;
15  
16  /**
17   * <code>AbstractExceptionListener</code> TODO (document class)
18   * 
19   */
20  public abstract class AbstractExceptionHandler implements ExceptionHandler
21  {
22  
23      protected HashMap registry = new HashMap();
24  
25      private String endpointName;
26  
27      protected ErrorManager errorManager = null;
28  
29      public void registerException(Class exceptionClass)
30      {
31  
32          registry.put(exceptionClass, exceptionClass);
33  
34      }
35  
36      public Iterator getRegisteredClasses()
37      {
38          return registry.keySet().iterator();
39      }
40  
41      public void unRegisterException(Class exceptionClass)
42      {
43          registry.remove(exceptionClass);
44  
45      }
46  
47      public boolean isRegisteredFor(Class exceptionClass)
48      {
49          Class aClass = null;
50          for (Iterator i = getRegisteredClasses(); i.hasNext();)
51          {
52              aClass = (Class)i.next();
53              if (aClass.isAssignableFrom(exceptionClass))
54              {
55                  return true;
56              }
57          }
58          return false;
59      }
60  
61      public void onException(ErrorMessage message) throws HandlerException
62      {
63          Throwable t = null;
64  
65          try
66          {
67              t = message.getException().toException();
68          }
69          catch (Exception e)
70          {
71              throw new HandlerException(LocaleMessage.unretrievedException(e), e);
72          }
73  
74          if (!isRegisteredFor(t.getClass()))
75          {
76              throw new HandlerException(LocaleMessage.unhandledException(t.getClass(), this.getClass()));
77          }
78          processException(message, t);
79      }
80  
81      protected abstract void processException(ErrorMessage message, Throwable t) throws HandlerException;
82  
83      /**
84       * @return Returns the errorManager.
85       */
86      public ErrorManager getErrorManager()
87      {
88          return errorManager;
89      }
90  
91      /**
92       * @param errorManager The errorManager to set.
93       */
94      public void setErrorManager(ErrorManager errorManager)
95      {
96          this.errorManager = errorManager;
97      }
98  
99      /**
100      * @return Returns the endpointName.
101      */
102     public String getendpointName()
103     {
104         return endpointName;
105     }
106 
107     /**
108      * @param endpointName The endpointName to set.
109      */
110     public void setEndpointName(String endpointName)
111     {
112         this.endpointName = endpointName;
113     }
114 
115 }