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
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 public class ConversationManagerSessionListener
64 implements HttpSessionAttributeListener, HttpSessionActivationListener, ServletContextListener
65 {
66 private final Log log = LogFactory.getLog(ConversationManagerSessionListener.class);
67 private final static long DEFAULT_CHECK_TIME = 5 * 60 * 1000;
68
69 private final static String CHECK_TIME = "org.apache.myfaces.orchestra.WIPER_THREAD_CHECK_TIME";
70
71 private ConversationWiperThread conversationWiperThread;
72
73 public void contextInitialized(ServletContextEvent event)
74 {
75 log.debug("contextInitialized");
76 long checkTime = DEFAULT_CHECK_TIME;
77 String checkTimeString = event.getServletContext().getInitParameter(CHECK_TIME);
78 if (checkTimeString != null)
79 {
80 checkTime = Long.parseLong(checkTimeString);
81 }
82
83 if (conversationWiperThread == null)
84 {
85 conversationWiperThread = new ConversationWiperThread(checkTime);
86 conversationWiperThread.start();
87 }
88 else
89 {
90 log.error("context initialised more than once");
91 }
92 log.debug("initialised");
93 }
94
95 public void contextDestroyed(ServletContextEvent event)
96 {
97 log.debug("Context destroyed");
98 if (conversationWiperThread != null)
99 {
100 conversationWiperThread.interrupt();
101 conversationWiperThread = null;
102 }
103 else
104 {
105 log.error("Context destroyed more than once");
106 }
107
108 }
109
110 public void attributeAdded(HttpSessionBindingEvent event)
111 {
112
113 if (event.getValue() instanceof ConversationManager)
114 {
115 ConversationManager cm = (ConversationManager) event.getValue();
116 conversationWiperThread.addConversationManager(cm);
117 }
118 }
119
120 public void attributeRemoved(HttpSessionBindingEvent event)
121 {
122
123
124
125
126
127 if (event.getValue() instanceof ConversationManager)
128 {
129 ConversationManager cm = (ConversationManager) event.getValue();
130 conversationWiperThread.removeConversationManager(cm);
131 }
132 }
133
134 public void attributeReplaced(HttpSessionBindingEvent event)
135 {
136
137
138 if (event.getValue() instanceof ConversationManager)
139 {
140 ConversationManager oldConversationManager = (ConversationManager) event.getValue();
141 conversationWiperThread.removeConversationManager(oldConversationManager);
142 }
143
144
145 HttpSession session = event.getSession();
146 String attrName = event.getName();
147 Object newObj = session.getAttribute(attrName);
148 if (newObj instanceof ConversationManager)
149 {
150 ConversationManager newConversationManager = (ConversationManager) newObj;
151 conversationWiperThread.addConversationManager(newConversationManager);
152 }
153 }
154
155
156
157
158
159
160
161
162
163 public void sessionDidActivate(HttpSessionEvent se)
164 {
165
166 HttpSession session = se.getSession();
167 Enumeration e = session.getAttributeNames();
168 while (e.hasMoreElements())
169 {
170 String attrName = (String) e.nextElement();
171 Object val = session.getAttribute(attrName);
172 if (val instanceof ConversationManager)
173 {
174
175
176
177
178
179
180
181
182
183 ConversationManager cm = (ConversationManager) val;
184 conversationWiperThread.addConversationManager(cm);
185 }
186 }
187 }
188
189
190
191
192
193
194
195
196
197 public void sessionWillPassivate(HttpSessionEvent se)
198 {
199
200
201
202
203 HttpSession session = se.getSession();
204 Enumeration e = session.getAttributeNames();
205 while (e.hasMoreElements())
206 {
207 String attrName = (String) e.nextElement();
208 Object val = session.getAttribute(attrName);
209 if (val instanceof ConversationManager)
210 {
211 ConversationManager cm = (ConversationManager) val;
212 conversationWiperThread.removeConversationManager(cm);
213 }
214 }
215 }
216 }