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
23 import javax.faces.context.ExternalContext;
24 import javax.faces.context.FacesContext;
25 import javax.faces.event.PreDestroyCustomScopeEvent;
26 import javax.faces.event.PreDestroyViewMapEvent;
27 import javax.servlet.ServletContext;
28 import javax.servlet.ServletContextAttributeEvent;
29 import javax.servlet.ServletContextAttributeListener;
30 import javax.servlet.ServletContextEvent;
31 import javax.servlet.ServletContextListener;
32 import javax.servlet.ServletRequest;
33 import javax.servlet.ServletRequestAttributeEvent;
34 import javax.servlet.ServletRequestAttributeListener;
35 import javax.servlet.ServletRequestEvent;
36 import javax.servlet.ServletRequestListener;
37 import javax.servlet.http.HttpSession;
38 import javax.servlet.http.HttpSessionAttributeListener;
39 import javax.servlet.http.HttpSessionBindingEvent;
40 import javax.servlet.http.HttpSessionEvent;
41 import javax.servlet.http.HttpSessionListener;
42
43 import org.apache.myfaces.config.ManagedBeanDestroyer;
44 import org.apache.myfaces.config.RuntimeConfig;
45 import org.apache.myfaces.config.annotation.LifecycleProvider;
46
47
48
49
50
51
52
53
54
55
56
57
58
59 public class ManagedBeanDestroyerListener implements
60 HttpSessionAttributeListener, HttpSessionListener,
61 ServletContextListener, ServletContextAttributeListener,
62 ServletRequestListener, ServletRequestAttributeListener
63 {
64
65
66
67
68
69
70 public static final String APPLICATION_MAP_KEY = "org.apache.myfaces.ManagedBeanDestroyerListener";
71
72 private ManagedBeanDestroyer _destroyer = null;
73
74
75
76
77
78
79 public void setManagedBeanDestroyer(ManagedBeanDestroyer destroyer)
80 {
81 _destroyer = destroyer;
82 }
83
84
85
86 public void attributeAdded(HttpSessionBindingEvent event)
87 {
88
89 }
90
91 public void attributeRemoved(HttpSessionBindingEvent event)
92 {
93 if (_destroyer != null)
94 {
95 _destroyer.destroy(event.getName(), event.getValue());
96 }
97 }
98
99 public void attributeReplaced(HttpSessionBindingEvent event)
100 {
101 if (_destroyer != null)
102 {
103 _destroyer.destroy(event.getName(), event.getValue());
104 }
105 }
106
107 public void sessionCreated(HttpSessionEvent event)
108 {
109
110 }
111
112 @SuppressWarnings("unchecked")
113 public void sessionDestroyed(HttpSessionEvent event)
114 {
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138 }
139
140
141
142 public void attributeAdded(ServletContextAttributeEvent event)
143 {
144
145 }
146
147 public void attributeRemoved(ServletContextAttributeEvent event)
148 {
149 if (_destroyer != null)
150 {
151 _destroyer.destroy(event.getName(), event.getValue());
152 }
153 }
154
155 public void attributeReplaced(ServletContextAttributeEvent event)
156 {
157 if (_destroyer != null)
158 {
159 _destroyer.destroy(event.getName(), event.getValue());
160 }
161 }
162
163 public void contextInitialized(ServletContextEvent event)
164 {
165
166 }
167
168 @SuppressWarnings("unchecked")
169 public void contextDestroyed(ServletContextEvent event)
170 {
171 if (_destroyer != null)
172 {
173 ServletContext ctx = event.getServletContext();
174 Enumeration<String> attributes = ctx.getAttributeNames();
175 if (!attributes.hasMoreElements())
176 {
177
178 return;
179 }
180
181 while (attributes.hasMoreElements())
182 {
183 String name = attributes.nextElement();
184 Object value = ctx.getAttribute(name);
185 _destroyer.destroy(name, value);
186 }
187 }
188 }
189
190
191
192 public void attributeAdded(ServletRequestAttributeEvent event)
193 {
194
195 }
196
197 public void attributeRemoved(ServletRequestAttributeEvent event)
198 {
199 if (_destroyer != null)
200 {
201 _destroyer.destroy(event.getName(), event.getValue());
202 }
203 }
204
205 public void attributeReplaced(ServletRequestAttributeEvent event)
206 {
207 if (_destroyer != null)
208 {
209 _destroyer.destroy(event.getName(), event.getValue());
210 }
211 }
212
213 public void requestInitialized(ServletRequestEvent event)
214 {
215
216 }
217
218 @SuppressWarnings("unchecked")
219 public void requestDestroyed(ServletRequestEvent event)
220 {
221 if (_destroyer != null)
222 {
223 ServletRequest request = event.getServletRequest();
224 Enumeration<String> attributes = request.getAttributeNames();
225 if (!attributes.hasMoreElements())
226 {
227
228 return;
229 }
230
231 while (attributes.hasMoreElements())
232 {
233 String name = attributes.nextElement();
234 Object value = request.getAttribute(name);
235 _destroyer.destroy(name, value);
236 }
237 }
238 }
239
240 }