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 | 0 | public abstract class AbstractExceptionHandler implements ExceptionHandler |
17 | |
{ |
18 | |
|
19 | 0 | protected HashMap registry = new HashMap(); |
20 | |
|
21 | |
private String endpointName; |
22 | |
|
23 | 0 | protected ErrorManager errorManager = null; |
24 | |
|
25 | |
public void registerException(Class exceptionClass) |
26 | |
{ |
27 | |
|
28 | 0 | registry.put(exceptionClass, exceptionClass); |
29 | |
|
30 | 0 | } |
31 | |
|
32 | |
public Iterator getRegisteredClasses() |
33 | |
{ |
34 | 0 | return registry.keySet().iterator(); |
35 | |
} |
36 | |
|
37 | |
public void unRegisterException(Class exceptionClass) |
38 | |
{ |
39 | 0 | registry.remove(exceptionClass); |
40 | |
|
41 | 0 | } |
42 | |
|
43 | |
public boolean isRegisteredFor(Class exceptionClass) |
44 | |
{ |
45 | 0 | Class aClass = null; |
46 | 0 | for (Iterator i = getRegisteredClasses(); i.hasNext();) |
47 | |
{ |
48 | 0 | aClass = (Class)i.next(); |
49 | 0 | if (aClass.isAssignableFrom(exceptionClass)) |
50 | |
{ |
51 | 0 | return true; |
52 | |
} |
53 | |
} |
54 | 0 | return false; |
55 | |
} |
56 | |
|
57 | |
public void onException(ErrorMessage message) throws HandlerException |
58 | |
{ |
59 | 0 | Throwable t = null; |
60 | |
|
61 | |
try |
62 | |
{ |
63 | 0 | t = message.getException().toException(); |
64 | |
} |
65 | 0 | catch (Exception e) |
66 | |
{ |
67 | 0 | throw new HandlerException(LocaleMessage.unretrievedException(e), e); |
68 | 0 | } |
69 | |
|
70 | 0 | if (!isRegisteredFor(t.getClass())) |
71 | |
{ |
72 | 0 | throw new HandlerException(LocaleMessage.unhandledException(t.getClass(), this.getClass())); |
73 | |
} |
74 | 0 | processException(message, t); |
75 | 0 | } |
76 | |
|
77 | |
protected abstract void processException(ErrorMessage message, Throwable t) throws HandlerException; |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public ErrorManager getErrorManager() |
83 | |
{ |
84 | 0 | return errorManager; |
85 | |
} |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
public void setErrorManager(ErrorManager errorManager) |
91 | |
{ |
92 | 0 | this.errorManager = errorManager; |
93 | 0 | } |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
public String getendpointName() |
99 | |
{ |
100 | 0 | return endpointName; |
101 | |
} |
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
public void setEndpointName(String endpointName) |
107 | |
{ |
108 | 0 | this.endpointName = endpointName; |
109 | 0 | } |
110 | |
|
111 | |
} |