1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.shared.context;
20
21 import java.util.Collections;
22 import java.util.LinkedList;
23 import java.util.Queue;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26
27 import javax.el.ELException;
28 import javax.faces.FacesException;
29 import javax.faces.context.ExceptionHandler;
30 import javax.faces.event.AbortProcessingException;
31 import javax.faces.event.ExceptionQueuedEvent;
32 import javax.faces.event.ExceptionQueuedEventContext;
33 import javax.faces.event.SystemEvent;
34
35
36
37
38
39
40
41
42
43 public class ExceptionHandlerImpl extends ExceptionHandler
44 {
45
46
47
48
49
50
51
52
53 private static final Logger log = Logger.getLogger(ExceptionHandlerImpl.class.getName());
54
55 private Queue<ExceptionQueuedEvent> handled;
56 private Queue<ExceptionQueuedEvent> unhandled;
57 private ExceptionQueuedEvent handledAndThrown;
58
59 public ExceptionHandlerImpl()
60 {
61
62 }
63
64
65
66
67 @Override
68 public ExceptionQueuedEvent getHandledExceptionQueuedEvent()
69 {
70 return handledAndThrown;
71 }
72
73
74
75
76 @Override
77 public Iterable<ExceptionQueuedEvent> getHandledExceptionQueuedEvents()
78 {
79 return handled == null ? Collections.<ExceptionQueuedEvent>emptyList() : handled;
80 }
81
82
83
84
85 @Override
86 public Throwable getRootCause(Throwable t)
87 {
88 if (t == null)
89 {
90 throw new NullPointerException("t");
91 }
92
93 while (t != null)
94 {
95 Class<?> clazz = t.getClass();
96 if (!clazz.equals(FacesException.class) && !clazz.equals(ELException.class))
97 {
98 return t;
99 }
100
101 t = t.getCause();
102 }
103
104 return null;
105 }
106
107
108
109
110 @Override
111 public Iterable<ExceptionQueuedEvent> getUnhandledExceptionQueuedEvents()
112 {
113 return unhandled == null ? Collections.<ExceptionQueuedEvent>emptyList() : unhandled;
114 }
115
116
117
118
119 @Override
120 public void handle() throws FacesException
121 {
122 if (unhandled != null && !unhandled.isEmpty())
123 {
124 if (handled == null)
125 {
126 handled = new LinkedList<ExceptionQueuedEvent>();
127 }
128
129 FacesException toThrow = null;
130
131 do
132 {
133
134
135
136 ExceptionQueuedEvent event = unhandled.peek();
137 try
138 {
139
140 ExceptionQueuedEventContext context = event.getContext();
141
142
143 Throwable exception = context.getException();
144
145
146
147 if (!shouldSkip(exception))
148 {
149
150 handledAndThrown = event;
151
152
153
154
155
156 toThrow = wrap(getRethrownException(exception));
157 break;
158 }
159 else
160 {
161
162
163 log.log(Level.SEVERE, exception.getClass().getName() + " occured while processing " +
164 (context.inBeforePhase() ? "beforePhase() of " :
165 (context.inAfterPhase() ? "afterPhase() of " : "")) +
166 "phase " + context.getPhaseId() + ": " +
167 "UIComponent-ClientId=" +
168 (context.getComponent() != null ?
169 context.getComponent().getClientId(context.getContext()) : "") + ", " +
170 "Message=" + exception.getMessage());
171
172 log.log(Level.SEVERE, exception.getMessage(), exception);
173
174 }
175 }
176 catch (Throwable t)
177 {
178
179
180 throw new FacesException("Could not perform the algorithm to handle the Exception", t);
181 }
182 finally
183 {
184
185
186 handled.add(event);
187 unhandled.remove(event);
188 }
189 } while (!unhandled.isEmpty());
190
191
192 if (toThrow != null)
193 {
194 throw toThrow;
195 }
196 }
197 }
198
199
200
201
202 @Override
203 public boolean isListenerForSource(Object source)
204 {
205 return source instanceof ExceptionQueuedEventContext;
206 }
207
208
209
210
211 @Override
212 public void processEvent(SystemEvent exceptionQueuedEvent) throws AbortProcessingException
213 {
214 if (unhandled == null)
215 {
216 unhandled = new LinkedList<ExceptionQueuedEvent>();
217 }
218
219 unhandled.add((ExceptionQueuedEvent)exceptionQueuedEvent);
220 }
221
222 protected Throwable getRethrownException(Throwable exception)
223 {
224
225
226 Throwable toRethrow = getRootCause(exception);
227 if (toRethrow == null)
228 {
229 toRethrow = exception;
230 }
231
232 return toRethrow;
233 }
234
235 protected FacesException wrap(Throwable exception)
236 {
237 if (exception instanceof FacesException)
238 {
239 return (FacesException) exception;
240 }
241 return new FacesException(exception);
242 }
243
244 protected boolean shouldSkip(Throwable exception)
245 {
246 return exception instanceof AbortProcessingException;
247 }
248 }