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 log.debug("contextInitialized");
88 long checkTime = DEFAULT_CHECK_TIME;
89 String checkTimeString = event.getServletContext().getInitParameter(CHECK_TIME);
90 if (checkTimeString != null)
91 {
92 checkTime = Long.parseLong(checkTimeString);
93 }
94
95 if (conversationWiperThread == null)
96 {
97 conversationWiperThread = new ConversationWiperThread(checkTime);
98 conversationWiperThread.setName("Orchestra:ConversationWiperThread");
99 conversationWiperThread.start();
100 }
101 else
102 {
103 log.error("context initialised more than once");
104 }
105 log.debug("initialised");
106 }
107
108 public void contextDestroyed(ServletContextEvent event)
109 {
110 log.debug("Context destroyed");
111 if (conversationWiperThread != null)
112 {
113 conversationWiperThread.interrupt();
114 conversationWiperThread = null;
115 }
116 else
117 {
118 log.error("Context destroyed more than once");
119 }
120
121 }
122
123 public void sessionCreated(HttpSessionEvent event)
124 {
125
126 }
127
128 public void sessionDestroyed(HttpSessionEvent event)
129 {
130
131
132
133
134
135
136
137
138
139
140 HttpSession session = event.getSession();
141 Enumeration e = session.getAttributeNames();
142 while (e.hasMoreElements())
143 {
144 String attrName = (String) e.nextElement();
145 Object o = session.getAttribute(attrName);
146 if (o instanceof ConversationManager)
147 {
148
149
150
151 log.debug("Session containing a ConversationManager has been destroyed (eg timed out)");
152 session.removeAttribute(attrName);
153 }
154 }
155 }
156
157 public void attributeAdded(HttpSessionBindingEvent event)
158 {
159
160 if (event.getValue() instanceof ConversationManager)
161 {
162 ConversationManager cm = (ConversationManager) event.getValue();
163 conversationWiperThread.addConversationManager(cm);
164 }
165 }
166
167 public void attributeRemoved(HttpSessionBindingEvent event)
168 {
169
170
171
172
173
174 if (event.getValue() instanceof ConversationManager)
175 {
176 log.debug("A ConversationManager instance has been removed from a session");
177 ConversationManager cm = (ConversationManager) event.getValue();
178 removeAndInvalidateConversationManager(cm);
179 }
180 }
181
182 public void attributeReplaced(HttpSessionBindingEvent event)
183 {
184
185
186 if (event.getValue() instanceof ConversationManager)
187 {
188 ConversationManager oldConversationManager = (ConversationManager) event.getValue();
189 removeAndInvalidateConversationManager(oldConversationManager);
190 }
191
192
193 HttpSession session = event.getSession();
194 String attrName = event.getName();
195 Object newObj = session.getAttribute(attrName);
196 if (newObj instanceof ConversationManager)
197 {
198 ConversationManager newConversationManager = (ConversationManager) newObj;
199 conversationWiperThread.addConversationManager(newConversationManager);
200 }
201 }
202
203
204
205
206
207
208
209
210
211 public void sessionDidActivate(HttpSessionEvent se)
212 {
213
214 HttpSession session = se.getSession();
215 Enumeration e = session.getAttributeNames();
216 while (e.hasMoreElements())
217 {
218 String attrName = (String) e.nextElement();
219 Object val = session.getAttribute(attrName);
220 if (val instanceof ConversationManager)
221 {
222
223
224
225
226
227
228
229
230
231 ConversationManager cm = (ConversationManager) val;
232 conversationWiperThread.addConversationManager(cm);
233 }
234 }
235 }
236
237
238
239
240
241
242
243
244
245 public void sessionWillPassivate(HttpSessionEvent se)
246 {
247
248
249
250
251 HttpSession session = se.getSession();
252 Enumeration e = session.getAttributeNames();
253 while (e.hasMoreElements())
254 {
255 String attrName = (String) e.nextElement();
256 Object val = session.getAttribute(attrName);
257 if (val instanceof ConversationManager)
258 {
259 ConversationManager cm = (ConversationManager) val;
260 conversationWiperThread.removeConversationManager(cm);
261 }
262 }
263 }
264
265 private void removeAndInvalidateConversationManager(ConversationManager cm)
266 {
267
268
269
270
271
272 FrameworkAdapter currentFrameworkAdapter = FrameworkAdapter.getCurrentInstance();
273 try
274 {
275
276
277
278 FrameworkAdapter fa = new LocalFrameworkAdapter();
279 ConversationMessager conversationMessager = new LogConversationMessager();
280 fa.setConversationMessager(conversationMessager);
281 FrameworkAdapter.setCurrentInstance(fa);
282
283 conversationWiperThread.removeConversationManager(cm);
284 cm.removeAndInvalidateAllConversationContexts();
285 }
286 finally
287 {
288
289 FrameworkAdapter.setCurrentInstance(currentFrameworkAdapter);
290
291 if (currentFrameworkAdapter != null)
292 {
293 log.warn("removeAndInvalidateConversationManager: currentFrameworkAdapter is not null..");
294 }
295 }
296 }
297 }