1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.webapp; |
20 | |
|
21 | |
import java.util.Enumeration; |
22 | |
import javax.faces.context.ExceptionHandler; |
23 | |
import javax.faces.context.ExternalContext; |
24 | |
import javax.faces.context.FacesContext; |
25 | |
|
26 | |
import javax.servlet.ServletContext; |
27 | |
import javax.servlet.ServletContextAttributeEvent; |
28 | |
import javax.servlet.ServletContextAttributeListener; |
29 | |
import javax.servlet.ServletContextEvent; |
30 | |
import javax.servlet.ServletContextListener; |
31 | |
import javax.servlet.ServletRequest; |
32 | |
import javax.servlet.ServletRequestAttributeEvent; |
33 | |
import javax.servlet.ServletRequestAttributeListener; |
34 | |
import javax.servlet.ServletRequestEvent; |
35 | |
import javax.servlet.ServletRequestListener; |
36 | |
import javax.servlet.http.HttpSessionAttributeListener; |
37 | |
import javax.servlet.http.HttpSessionBindingEvent; |
38 | |
import javax.servlet.http.HttpSessionEvent; |
39 | |
import javax.servlet.http.HttpSessionListener; |
40 | |
|
41 | |
import org.apache.myfaces.config.ManagedBeanDestroyer; |
42 | |
import org.apache.myfaces.context.ReleaseableExternalContext; |
43 | |
import org.apache.myfaces.context.servlet.StartupFacesContextImpl; |
44 | |
import org.apache.myfaces.context.servlet.StartupServletExternalContextImpl; |
45 | |
import org.apache.myfaces.shared.context.ExceptionHandlerImpl; |
46 | |
import org.apache.myfaces.spi.ViewScopeProvider; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | 0 | public class ManagedBeanDestroyerListener implements |
61 | |
HttpSessionAttributeListener, HttpSessionListener, |
62 | |
ServletContextListener, ServletContextAttributeListener, |
63 | |
ServletRequestListener, ServletRequestAttributeListener |
64 | |
{ |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
public static final String APPLICATION_MAP_KEY = "org.apache.myfaces.ManagedBeanDestroyerListener"; |
72 | |
|
73 | 0 | private ManagedBeanDestroyer _destroyer = null; |
74 | |
|
75 | 0 | private ViewScopeProvider _viewScopeHandler = null; |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public void setManagedBeanDestroyer(ManagedBeanDestroyer destroyer) |
83 | |
{ |
84 | 0 | _destroyer = destroyer; |
85 | 0 | } |
86 | |
|
87 | |
public void setViewScopeHandler(ViewScopeProvider listener) |
88 | |
{ |
89 | 0 | _viewScopeHandler = listener; |
90 | 0 | } |
91 | |
|
92 | |
|
93 | |
|
94 | |
public void attributeAdded(HttpSessionBindingEvent event) |
95 | |
{ |
96 | |
|
97 | 0 | } |
98 | |
|
99 | |
public void attributeRemoved(HttpSessionBindingEvent event) |
100 | |
{ |
101 | 0 | if (_destroyer != null) |
102 | |
{ |
103 | 0 | _destroyer.destroy(event.getName(), event.getValue()); |
104 | |
} |
105 | 0 | } |
106 | |
|
107 | |
public void attributeReplaced(HttpSessionBindingEvent event) |
108 | |
{ |
109 | 0 | if (_destroyer != null) |
110 | |
{ |
111 | 0 | _destroyer.destroy(event.getName(), event.getValue()); |
112 | |
} |
113 | 0 | } |
114 | |
|
115 | |
public void sessionCreated(HttpSessionEvent event) |
116 | |
{ |
117 | |
|
118 | 0 | } |
119 | |
|
120 | |
@SuppressWarnings("unchecked") |
121 | |
public void sessionDestroyed(HttpSessionEvent event) |
122 | |
{ |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | 0 | if (_viewScopeHandler != null) |
154 | |
{ |
155 | 0 | FacesContext facesContext = FacesContext.getCurrentInstance(); |
156 | 0 | if (facesContext != null) |
157 | |
{ |
158 | 0 | _viewScopeHandler.onSessionDestroyed(); |
159 | |
} |
160 | |
else |
161 | |
{ |
162 | |
|
163 | |
|
164 | |
|
165 | |
try |
166 | |
{ |
167 | 0 | ServletContext servletContext = event.getSession().getServletContext(); |
168 | 0 | ExternalContext externalContext = new StartupServletExternalContextImpl(servletContext, false); |
169 | 0 | ExceptionHandler exceptionHandler = new ExceptionHandlerImpl(); |
170 | 0 | facesContext = new StartupFacesContextImpl(externalContext, |
171 | |
(ReleaseableExternalContext) externalContext, exceptionHandler, false); |
172 | 0 | _viewScopeHandler.onSessionDestroyed(); |
173 | |
} |
174 | |
finally |
175 | |
{ |
176 | 0 | facesContext.release(); |
177 | 0 | } |
178 | |
} |
179 | |
} |
180 | 0 | } |
181 | |
|
182 | |
|
183 | |
|
184 | |
public void attributeAdded(ServletContextAttributeEvent event) |
185 | |
{ |
186 | |
|
187 | 0 | } |
188 | |
|
189 | |
public void attributeRemoved(ServletContextAttributeEvent event) |
190 | |
{ |
191 | 0 | if (_destroyer != null) |
192 | |
{ |
193 | 0 | _destroyer.destroy(event.getName(), event.getValue()); |
194 | |
} |
195 | 0 | } |
196 | |
|
197 | |
public void attributeReplaced(ServletContextAttributeEvent event) |
198 | |
{ |
199 | 0 | if (_destroyer != null) |
200 | |
{ |
201 | 0 | _destroyer.destroy(event.getName(), event.getValue()); |
202 | |
} |
203 | 0 | } |
204 | |
|
205 | |
public void contextInitialized(ServletContextEvent event) |
206 | |
{ |
207 | |
|
208 | 0 | } |
209 | |
|
210 | |
@SuppressWarnings("unchecked") |
211 | |
public void contextDestroyed(ServletContextEvent event) |
212 | |
{ |
213 | 0 | if (_destroyer != null) |
214 | |
{ |
215 | 0 | ServletContext ctx = event.getServletContext(); |
216 | 0 | Enumeration<String> attributes = ctx.getAttributeNames(); |
217 | 0 | if (!attributes.hasMoreElements()) |
218 | |
{ |
219 | |
|
220 | 0 | return; |
221 | |
} |
222 | |
|
223 | 0 | while (attributes.hasMoreElements()) |
224 | |
{ |
225 | 0 | String name = attributes.nextElement(); |
226 | 0 | Object value = ctx.getAttribute(name); |
227 | 0 | _destroyer.destroy(name, value); |
228 | 0 | } |
229 | |
} |
230 | 0 | } |
231 | |
|
232 | |
|
233 | |
|
234 | |
public void attributeAdded(ServletRequestAttributeEvent event) |
235 | |
{ |
236 | |
|
237 | 0 | } |
238 | |
|
239 | |
public void attributeRemoved(ServletRequestAttributeEvent event) |
240 | |
{ |
241 | 0 | if (_destroyer != null) |
242 | |
{ |
243 | 0 | _destroyer.destroy(event.getName(), event.getValue()); |
244 | |
} |
245 | 0 | } |
246 | |
|
247 | |
public void attributeReplaced(ServletRequestAttributeEvent event) |
248 | |
{ |
249 | 0 | if (_destroyer != null) |
250 | |
{ |
251 | 0 | _destroyer.destroy(event.getName(), event.getValue()); |
252 | |
} |
253 | 0 | } |
254 | |
|
255 | |
public void requestInitialized(ServletRequestEvent event) |
256 | |
{ |
257 | |
|
258 | 0 | } |
259 | |
|
260 | |
@SuppressWarnings("unchecked") |
261 | |
public void requestDestroyed(ServletRequestEvent event) |
262 | |
{ |
263 | 0 | if (_destroyer != null) |
264 | |
{ |
265 | 0 | ServletRequest request = event.getServletRequest(); |
266 | 0 | Enumeration<String> attributes = request.getAttributeNames(); |
267 | 0 | if (!attributes.hasMoreElements()) |
268 | |
{ |
269 | |
|
270 | 0 | return; |
271 | |
} |
272 | |
|
273 | 0 | while (attributes.hasMoreElements()) |
274 | |
{ |
275 | 0 | String name = attributes.nextElement(); |
276 | 0 | Object value = request.getAttribute(name); |
277 | 0 | _destroyer.destroy(name, value); |
278 | 0 | } |
279 | |
} |
280 | 0 | } |
281 | |
|
282 | |
} |