KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > samples > errorhandler > AbstractExceptionHandler


1 /*
2  * $Id: AbstractExceptionHandler.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
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 JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 /**
17  * <code>AbstractExceptionListener</code> TODO (document class)
18  *
19  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
20  * @version $Revision: 3798 $
21  */

22 public abstract class AbstractExceptionHandler implements ExceptionHandler
23 {
24
25     protected HashMap JavaDoc registry = new HashMap JavaDoc();
26
27     private String JavaDoc endpointName;
28
29     protected ErrorManager errorManager = null;
30
31     public void registerException(Class JavaDoc exceptionClass)
32     {
33
34         registry.put(exceptionClass, exceptionClass);
35
36     }
37
38     public Iterator JavaDoc getRegisteredClasses()
39     {
40         return registry.keySet().iterator();
41     }
42
43     public void unRegisterException(Class JavaDoc exceptionClass)
44     {
45         registry.remove(exceptionClass);
46
47     }
48
49     public boolean isRegisteredFor(Class JavaDoc exceptionClass)
50     {
51         Class JavaDoc aClass = null;
52         for (Iterator JavaDoc i = getRegisteredClasses(); i.hasNext();)
53         {
54             aClass = (Class JavaDoc)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 JavaDoc t = null;
66
67         try
68         {
69             t = message.getException().toException();
70         }
71         catch (Exception JavaDoc 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 JavaDoc t) throws HandlerException;
87
88     /**
89      * @return Returns the errorManager.
90      */

91     public ErrorManager getErrorManager()
92     {
93         return errorManager;
94     }
95
96     /**
97      * @param errorManager The errorManager to set.
98      */

99     public void setErrorManager(ErrorManager errorManager)
100     {
101         this.errorManager = errorManager;
102     }
103
104     /**
105      * @return Returns the endpointName.
106      */

107     public String JavaDoc getendpointName()
108     {
109         return endpointName;
110     }
111
112     /**
113      * @param endpointName The endpointName to set.
114      */

115     public void setEndpointName(String JavaDoc endpointName)
116     {
117         this.endpointName = endpointName;
118     }
119
120 }
121
Popular Tags