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