1
2
3
4
5
6
7 package org.mule.example.errorhandler;
8
9 import java.util.HashMap;
10 import java.util.Iterator;
11
12
13
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
81
82 public ErrorManager getErrorManager()
83 {
84 return errorManager;
85 }
86
87
88
89
90 public void setErrorManager(ErrorManager errorManager)
91 {
92 this.errorManager = errorManager;
93 }
94
95
96
97
98 public String getendpointName()
99 {
100 return endpointName;
101 }
102
103
104
105
106 public void setEndpointName(String endpointName)
107 {
108 this.endpointName = endpointName;
109 }
110
111 }