View Javadoc

1   /*
2    * $Id: AbstractExceptionHandler.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.samples.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          }
79          processException(message, t);
80      }
81  
82      protected abstract void processException(ErrorMessage message, Throwable t) throws HandlerException;
83  
84      /**
85       * @return Returns the errorManager.
86       */
87      public ErrorManager getErrorManager()
88      {
89          return errorManager;
90      }
91  
92      /**
93       * @param errorManager The errorManager to set.
94       */
95      public void setErrorManager(ErrorManager errorManager)
96      {
97          this.errorManager = errorManager;
98      }
99  
100     /**
101      * @return Returns the endpointName.
102      */
103     public String getendpointName()
104     {
105         return endpointName;
106     }
107 
108     /**
109      * @param endpointName The endpointName to set.
110      */
111     public void setEndpointName(String endpointName)
112     {
113         this.endpointName = endpointName;
114     }
115 
116 }