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