View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.example.errorhandler;
8   
9   import java.util.HashMap;
10  import java.util.Iterator;
11  
12  /**
13   * <code>AbstractExceptionListener</code> TODO (document class)
14   * 
15   */
16  public abstract class AbstractExceptionHandler implements ExceptionHandler
17  {
18  
19      protected HashMap registry = new HashMap();
20  
21      private String endpointName;
22  
23      protected ErrorManager errorManager = null;
24  
25      public void registerException(Class exceptionClass)
26      {
27  
28          registry.put(exceptionClass, exceptionClass);
29  
30      }
31  
32      public Iterator getRegisteredClasses()
33      {
34          return registry.keySet().iterator();
35      }
36  
37      public void unRegisterException(Class exceptionClass)
38      {
39          registry.remove(exceptionClass);
40  
41      }
42  
43      public boolean isRegisteredFor(Class exceptionClass)
44      {
45          Class aClass = null;
46          for (Iterator i = getRegisteredClasses(); i.hasNext();)
47          {
48              aClass = (Class)i.next();
49              if (aClass.isAssignableFrom(exceptionClass))
50              {
51                  return true;
52              }
53          }
54          return false;
55      }
56  
57      public void onException(ErrorMessage message) throws HandlerException
58      {
59          Throwable t = null;
60  
61          try
62          {
63              t = message.getException().toException();
64          }
65          catch (Exception e)
66          {
67              throw new HandlerException(LocaleMessage.unretrievedException(e), e);
68          }
69  
70          if (!isRegisteredFor(t.getClass()))
71          {
72              throw new HandlerException(LocaleMessage.unhandledException(t.getClass(), this.getClass()));
73          }
74          processException(message, t);
75      }
76  
77      protected abstract void processException(ErrorMessage message, Throwable t) throws HandlerException;
78  
79      /**
80       * @return Returns the errorManager.
81       */
82      public ErrorManager getErrorManager()
83      {
84          return errorManager;
85      }
86  
87      /**
88       * @param errorManager The errorManager to set.
89       */
90      public void setErrorManager(ErrorManager errorManager)
91      {
92          this.errorManager = errorManager;
93      }
94  
95      /**
96       * @return Returns the endpointName.
97       */
98      public String getendpointName()
99      {
100         return endpointName;
101     }
102 
103     /**
104      * @param endpointName The endpointName to set.
105      */
106     public void setEndpointName(String endpointName)
107     {
108         this.endpointName = endpointName;
109     }
110 
111 }