1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
package pt.digitalis.dif.controller; |
7 | |
|
8 | |
import java.util.HashMap; |
9 | |
import java.util.Map; |
10 | |
|
11 | |
import javax.servlet.ServletException; |
12 | |
|
13 | |
import pt.digitalis.dif.controller.interfaces.IDIFContext; |
14 | |
import pt.digitalis.dif.controller.interfaces.IDispatcherErrorHandler; |
15 | |
import pt.digitalis.dif.controller.objects.ExceptionHandlerData; |
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | 0 | public class ExceptionHandlers { |
24 | |
|
25 | |
|
26 | 0 | static private Map<String, ExceptionHandlerData> handlers = new HashMap<String, ExceptionHandlerData>(); |
27 | |
|
28 | |
|
29 | |
static public final String RAISED_EXCEPTION_ATTRIBUTE = "RaisedExceptionCaghtInHandler"; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
static public void addExceptionHandler(Class<Throwable> exceptionClass, String stageID) |
40 | |
{ |
41 | 0 | handlers.put(exceptionClass.getCanonicalName(), new ExceptionHandlerData(exceptionClass, stageID)); |
42 | 0 | } |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
@SuppressWarnings("unchecked") |
53 | |
static public void addExceptionHandler(String exceptionClassName, String stageID) |
54 | |
{ |
55 | |
try |
56 | |
{ |
57 | 0 | Class<Throwable> clazz = (Class<Throwable>) Class.forName(exceptionClassName); |
58 | 0 | addExceptionHandler(clazz, stageID); |
59 | |
} |
60 | 0 | catch (ClassNotFoundException e) |
61 | |
{ |
62 | 0 | e.printStackTrace(); |
63 | |
} |
64 | 0 | } |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
static public String getExceptionHandler(Class<? extends Throwable> exceptionClass) |
74 | |
{ |
75 | 0 | ExceptionHandlerData handler = handlers.get(exceptionClass.getCanonicalName()); |
76 | |
|
77 | |
|
78 | 0 | if (handler == null) |
79 | |
{ |
80 | 0 | for (ExceptionHandlerData currentHandler: handlers.values()) |
81 | 0 | if (currentHandler.getExceptionClass().isAssignableFrom(exceptionClass)) |
82 | |
{ |
83 | 0 | handler = currentHandler; |
84 | 0 | break; |
85 | |
} |
86 | |
} |
87 | |
|
88 | 0 | if (handler == null) |
89 | 0 | return null; |
90 | |
else |
91 | 0 | return handler.getHandlerStageID(); |
92 | |
} |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
static public String getExceptionHandler(String exceptionClassName) |
102 | |
{ |
103 | 0 | ExceptionHandlerData handler = handlers.get(exceptionClassName); |
104 | |
|
105 | |
|
106 | 0 | if (handler == null) |
107 | |
{ |
108 | |
try |
109 | |
{ |
110 | |
@SuppressWarnings("unchecked") |
111 | 0 | Class<Throwable> clazz = (Class<Throwable>) Class.forName(exceptionClassName); |
112 | |
|
113 | 0 | return getExceptionHandler(clazz); |
114 | |
} |
115 | 0 | catch (ClassNotFoundException e) |
116 | |
{ |
117 | 0 | e.printStackTrace(); |
118 | 0 | return null; |
119 | |
} |
120 | |
} |
121 | |
else |
122 | 0 | return handler.getHandlerStageID(); |
123 | |
} |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
static public String getExceptionHandler(Throwable exception) |
133 | |
{ |
134 | 0 | String result = null; |
135 | 0 | result = getExceptionHandler(exception.getClass()); |
136 | |
|
137 | 0 | while (result == null && exception != null) |
138 | |
{ |
139 | 0 | if (exception.getCause() instanceof Exception) |
140 | 0 | exception = exception.getCause(); |
141 | 0 | else if (exception instanceof ServletException) |
142 | 0 | exception = ((ServletException) exception).getRootCause(); |
143 | |
else |
144 | |
break; |
145 | |
|
146 | 0 | if (exception != null) |
147 | 0 | result = getExceptionHandler(exception.getClass()); |
148 | |
} |
149 | |
|
150 | 0 | return result; |
151 | |
} |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
static public void handleException(IDIFContext context, Exception exception) |
163 | |
{ |
164 | 0 | String stageToRedirectTo = getExceptionHandler(exception); |
165 | |
|
166 | 0 | if (stageToRedirectTo != null) |
167 | |
{ |
168 | 0 | context.addStageResult(IDispatcherErrorHandler.ORIGINAL_REQUEST, context.getRequest()); |
169 | 0 | context.addStageResult(IDispatcherErrorHandler.EXCEPTION, exception); |
170 | 0 | context.getRequest().addAttribute(RAISED_EXCEPTION_ATTRIBUTE, exception); |
171 | 0 | context.redirectTo(stageToRedirectTo, true); |
172 | |
} |
173 | 0 | } |
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
static public boolean hasHandler(Class<? extends Throwable> exceptionClass) |
183 | |
{ |
184 | 0 | return (getExceptionHandler(exceptionClass) != null); |
185 | |
} |
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
static public boolean hasHandler(Exception exception) |
195 | |
{ |
196 | 0 | return (getExceptionHandler(exception) != null); |
197 | |
} |
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
static public boolean hasHandler(String exceptionClassName) |
207 | |
{ |
208 | 0 | return (getExceptionHandler(exceptionClassName) != null); |
209 | |
} |
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
public static void removeExceptionHandler(String key) |
217 | |
{ |
218 | 0 | handlers.remove(key); |
219 | 0 | } |
220 | |
} |