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
28 import javax.servlet.ServletContextEvent;
29 import javax.servlet.ServletContextListener;
30 import javax.servlet.http.HttpSession;
31 import javax.servlet.http.HttpSessionActivationListener;
32 import javax.servlet.http.HttpSessionAttributeListener;
33 import javax.servlet.http.HttpSessionBindingEvent;
34 import javax.servlet.http.HttpSessionEvent;
35 import javax.servlet.http.HttpSessionListener;
36
37
38
39
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 public class ConversationManagerSessionListener
68 implements
69 ServletContextListener,
70 HttpSessionListener,
71 HttpSessionAttributeListener,
72 HttpSessionActivationListener
73 {
74 private final Log log = LogFactory.getLog(ConversationManagerSessionListener.class);
75 private final static long DEFAULT_CHECK_TIME = 5 * 60 * 1000;
76
77 private final static String CHECK_TIME = "org.apache.myfaces.orchestra.WIPER_THREAD_CHECK_TIME";
78
79 private ConversationWiperThread conversationWiperThread;
80
81 public void contextInitialized(ServletContextEvent event)
82 {
83 log.debug("contextInitialized");
84 long checkTime = DEFAULT_CHECK_TIME;
85 String checkTimeString = event.getServletContext().getInitParameter(CHECK_TIME);
86 if (checkTimeString != null)
87 {
88 checkTime = Long.parseLong(checkTimeString);
89 }
90
91 if (conversationWiperThread == null)
92 {
93 conversationWiperThread = new ConversationWiperThread(checkTime);
94 conversationWiperThread.setName("Orchestra:ConversationWiperThread");
95 conversationWiperThread.start();
96 }
97 else
98 {
99 log.error("context initialised more than once");
100 }
101 log.debug("initialised");
102 }
103
104 public void contextDestroyed(ServletContextEvent event)
105 {
106 log.debug("Context destroyed");
107 if (conversationWiperThread != null)
108 {
109 conversationWiperThread.interrupt();
110 conversationWiperThread = null;
111 }
112 else
113 {
114 log.error("Context destroyed more than once");
115 }
116
117 }
118
119 public void sessionCreated(HttpSessionEvent event)
120 {
121
122 }
123
124 public void sessionDestroyed(HttpSessionEvent event)
125 {
126
127
128
129
130
131
132
133
134
135
136 HttpSession session = event.getSession();
137 Enumeration e = session.getAttributeNames();
138 while (e.hasMoreElements())
139 {
140 String attrName = (String) e.nextElement();
141 Object o = session.getAttribute(attrName);
142 if (o instanceof ConversationManager)
143 {
144
145
146
147 log.debug("Session containing a ConversationManager has been destroyed (eg timed out)");
148 session.removeAttribute(attrName);
149 }
150 }
151 }
152
153 public void attributeAdded(HttpSessionBindingEvent event)
154 {
155
156 if (event.getValue() instanceof ConversationManager)
157 {
158 ConversationManager cm = (ConversationManager) event.getValue();
159 conversationWiperThread.addConversationManager(cm);
160 }
161 }
162
163 public void attributeRemoved(HttpSessionBindingEvent event)
164 {
165
166
167
168
169
170 if (event.getValue() instanceof ConversationManager)
171 {
172 log.debug("A ConversationManager instance has been removed from a session");
173 ConversationManager cm = (ConversationManager) event.getValue();
174 conversationWiperThread.removeConversationManager(cm);
175 }
176 }
177
178 public void attributeReplaced(HttpSessionBindingEvent event)
179 {
180
181
182 if (event.getValue() instanceof ConversationManager)
183 {
184 ConversationManager oldConversationManager = (ConversationManager) event.getValue();
185 conversationWiperThread.removeConversationManager(oldConversationManager);
186 }
187
188
189 HttpSession session = event.getSession();
190 String attrName = event.getName();
191 Object newObj = session.getAttribute(attrName);
192 if (newObj instanceof ConversationManager)
193 {
194 ConversationManager newConversationManager = (ConversationManager) newObj;
195 conversationWiperThread.addConversationManager(newConversationManager);
196 }
197 }
198
199
200
201
202
203
204
205
206
207 public void sessionDidActivate(HttpSessionEvent se)
208 {
209
210 HttpSession session = se.getSession();
211 Enumeration e = session.getAttributeNames();
212 while (e.hasMoreElements())
213 {
214 String attrName = (String) e.nextElement();
215 Object val = session.getAttribute(attrName);
216 if (val instanceof ConversationManager)
217 {
218
219
220
221
222
223
224
225
226
227 ConversationManager cm = (ConversationManager) val;
228 conversationWiperThread.addConversationManager(cm);
229 }
230 }
231 }
232
233
234
235
236
237
238
239
240
241 public void sessionWillPassivate(HttpSessionEvent se)
242 {
243
244
245
246
247 HttpSession session = se.getSession();
248 Enumeration e = session.getAttributeNames();
249 while (e.hasMoreElements())
250 {
251 String attrName = (String) e.nextElement();
252 Object val = session.getAttribute(attrName);
253 if (val instanceof ConversationManager)
254 {
255 ConversationManager cm = (ConversationManager) val;
256 conversationWiperThread.removeConversationManager(cm);
257 }
258 }
259 }
260 }