1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.orchestra.conversation.servlet;
20
21 import java.util.Enumeration;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.myfaces.orchestra.conversation.ConversationManager;
26 import org.apache.myfaces.orchestra.conversation.ConversationWiperThread;
27 import org.apache.myfaces.orchestra.conversation.ConversationMessager;
28 import org.apache.myfaces.orchestra.conversation.basic.LogConversationMessager;
29 import org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapter;
30 import org.apache.myfaces.orchestra.frameworkAdapter.local.LocalFrameworkAdapter;
31
32 import javax.servlet.ServletContextEvent;
33 import javax.servlet.ServletContextListener;
34 import javax.servlet.http.HttpSession;
35 import javax.servlet.http.HttpSessionActivationListener;
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 public class ConversationManagerSessionListener
72 implements
73 ServletContextListener,
74 HttpSessionListener,
75 HttpSessionAttributeListener,
76 HttpSessionActivationListener
77 {
78 private final Log log = LogFactory.getLog(ConversationManagerSessionListener.class);
79 private final static long DEFAULT_CHECK_TIME = 5 * 60 * 1000;
80
81 private final static String CHECK_TIME = "org.apache.myfaces.orchestra.WIPER_THREAD_CHECK_TIME";
82
83 private ConversationWiperThread conversationWiperThread;
84
85 public void contextInitialized(ServletContextEvent event)
86 {
87 if (log.isDebugEnabled())
88 {
89 log.debug("contextInitialized");
90 }
91 long checkTime = DEFAULT_CHECK_TIME;
92 String checkTimeString = event.getServletContext().getInitParameter(CHECK_TIME);
93 if (checkTimeString != null)
94 {
95 checkTime = Long.parseLong(checkTimeString);
96 }
97
98 if (conversationWiperThread == null)
99 {
100 conversationWiperThread = new ConversationWiperThread(checkTime);
101 conversationWiperThread.setName("Orchestra:ConversationWiperThread");
102 conversationWiperThread.start();
103 }
104 else
105 {
106 log.error("context initialised more than once");
107 }
108 if (log.isDebugEnabled())
109 {
110 log.debug("initialised");
111 }
112 }
113
114 public void contextDestroyed(ServletContextEvent event)
115 {
116 if (log.isDebugEnabled())
117 {
118 log.debug("Context destroyed");
119 }
120 if (conversationWiperThread != null)
121 {
122 conversationWiperThread.interrupt();
123 conversationWiperThread = null;
124 }
125 else
126 {
127 log.error("Context destroyed more than once");
128 }
129
130 }
131
132 public void sessionCreated(HttpSessionEvent event)
133 {
134
135 }
136
137 public void sessionDestroyed(HttpSessionEvent event)
138 {
139
140
141
142
143
144
145
146
147
148
149 HttpSession session = event.getSession();
150 Enumeration e = session.getAttributeNames();
151 while (e.hasMoreElements())
152 {
153 String attrName = (String) e.nextElement();
154 Object o = session.getAttribute(attrName);
155 if (o instanceof ConversationManager)
156 {
157
158
159
160 if (log.isDebugEnabled())
161 {
162 log.debug("Session containing a ConversationManager has been destroyed (eg timed out)");
163 }
164 session.removeAttribute(attrName);
165 }
166 }
167 }
168
169 public void attributeAdded(HttpSessionBindingEvent event)
170 {
171
172 if (event.getValue() instanceof ConversationManager)
173 {
174 ConversationManager cm = (ConversationManager) event.getValue();
175 conversationWiperThread.addConversationManager(cm);
176 }
177 }
178
179 public void attributeRemoved(HttpSessionBindingEvent event)
180 {
181
182
183
184
185
186 if (event.getValue() instanceof ConversationManager)
187 {
188 if (log.isDebugEnabled())
189 {
190 log.debug("A ConversationManager instance has been removed from a session");
191 }
192 ConversationManager cm = (ConversationManager) event.getValue();
193 removeAndInvalidateConversationManager(cm);
194 }
195 }
196
197 public void attributeReplaced(HttpSessionBindingEvent event)
198 {
199
200
201 if (event.getValue() instanceof ConversationManager)
202 {
203 ConversationManager oldConversationManager = (ConversationManager) event.getValue();
204 removeAndInvalidateConversationManager(oldConversationManager);
205 }
206
207
208 HttpSession session = event.getSession();
209 String attrName = event.getName();
210 Object newObj = session.getAttribute(attrName);
211 if (newObj instanceof ConversationManager)
212 {
213 ConversationManager newConversationManager = (ConversationManager) newObj;
214 conversationWiperThread.addConversationManager(newConversationManager);
215 }
216 }
217
218
219
220
221
222
223
224
225
226 public void sessionDidActivate(HttpSessionEvent se)
227 {
228
229 HttpSession session = se.getSession();
230 Enumeration e = session.getAttributeNames();
231 while (e.hasMoreElements())
232 {
233 String attrName = (String) e.nextElement();
234 Object val = session.getAttribute(attrName);
235 if (val instanceof ConversationManager)
236 {
237
238
239
240
241
242
243
244
245
246 ConversationManager cm = (ConversationManager) val;
247 conversationWiperThread.addConversationManager(cm);
248 }
249 }
250 }
251
252
253
254
255
256
257
258
259
260 public void sessionWillPassivate(HttpSessionEvent se)
261 {
262
263
264
265
266 HttpSession session = se.getSession();
267 Enumeration e = session.getAttributeNames();
268 while (e.hasMoreElements())
269 {
270 String attrName = (String) e.nextElement();
271 Object val = session.getAttribute(attrName);
272 if (val instanceof ConversationManager)
273 {
274 ConversationManager cm = (ConversationManager) val;
275 conversationWiperThread.removeConversationManager(cm);
276 }
277 }
278 }
279
280 private void removeAndInvalidateConversationManager(ConversationManager cm)
281 {
282
283
284
285
286
287 FrameworkAdapter currentFrameworkAdapter = FrameworkAdapter.getCurrentInstance();
288 try
289 {
290
291
292
293 FrameworkAdapter fa = new LocalFrameworkAdapter();
294 ConversationMessager conversationMessager = new LogConversationMessager();
295 fa.setConversationMessager(conversationMessager);
296 FrameworkAdapter.setCurrentInstance(fa);
297
298 conversationWiperThread.removeConversationManager(cm);
299 cm.removeAndInvalidateAllConversationContexts();
300 }
301 finally
302 {
303
304 FrameworkAdapter.setCurrentInstance(currentFrameworkAdapter);
305
306 if (currentFrameworkAdapter != null)
307 {
308 log.warn("removeAndInvalidateConversationManager: currentFrameworkAdapter is not null..");
309 }
310 }
311 }
312 }