1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.container.session;
18
19 import java.util.Arrays;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.Iterator;
24 import java.util.Map;
25
26 import javax.servlet.http.HttpSession;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31 /***
32 * PortalSessionsManagerImpl
33 *
34 * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
35 * @version $Id: $
36 */
37 public class PortalSessionsManagerImpl implements PortalSessionsManager
38 {
39
40 private static Log log = LogFactory.getLog(PortalSessionsManagerImpl.class);
41
42 private static final class PortalSessionRegistry
43 {
44 long portalSessionKey;
45 PortalSessionMonitor psm;
46 Map sessionMonitors;
47
48 PortalSessionRegistry()
49 {
50 sessionMonitors = Collections.synchronizedMap(new HashMap());
51 }
52 }
53
54 private long portalSessionKeySequence;
55 private Map portalSessionsRegistry;
56 private boolean forceInvalidate;
57
58 public PortalSessionsManagerImpl()
59 {
60 this(true);
61 }
62
63 public PortalSessionsManagerImpl(boolean forceInvalidate)
64 {
65 portalSessionKeySequence = System.currentTimeMillis();
66 portalSessionsRegistry = Collections.synchronizedMap(new HashMap());
67 this.forceInvalidate = forceInvalidate;
68 }
69
70
71
72
73 public void portalSessionCreated(HttpSession portalSession)
74 {
75 PortalSessionMonitor psm = null;
76
77 synchronized (this)
78 {
79 psm = new PortalSessionMonitorImpl(++portalSessionKeySequence, forceInvalidate);
80 }
81
82 portalSession.setAttribute(PortalSessionMonitor.SESSION_KEY, psm);
83
84 portalSessionDidActivate(psm);
85 }
86
87
88
89
90 public void portalSessionWillPassivate(PortalSessionMonitor psm)
91 {
92 portalSessionsRegistry.remove(psm.getSessionId());
93 }
94
95
96
97
98 public void portalSessionDidActivate(PortalSessionMonitor restoredPsm)
99 {
100 PortalSessionRegistry psr = (PortalSessionRegistry)portalSessionsRegistry.get(restoredPsm.getSessionId());
101 if ( psr != null && psr.portalSessionKey != -1 && psr.portalSessionKey != restoredPsm.getSessionKey() )
102 {
103
104
105 portalSessionDestroyed(psr.psm);
106 psr = null;
107 }
108 if ( psr == null )
109 {
110 psr = new PortalSessionRegistry();
111 portalSessionsRegistry.put(restoredPsm.getSessionId(), psr);
112 }
113
114 psr.psm = restoredPsm;
115 psr.portalSessionKey = restoredPsm.getSessionKey();
116
117
118 Iterator iter = valuesShallowCopy(psr.sessionMonitors.values()).iterator();
119 PortletApplicationSessionMonitor pasm;
120 while (iter.hasNext())
121 {
122 pasm = (PortletApplicationSessionMonitor)iter.next();
123 if ( pasm.getPortalSessionKey() != psr.portalSessionKey )
124 {
125 pasm.invalidateSession();
126
127 psr.sessionMonitors.remove(pasm.getContextPath());
128 }
129 }
130 }
131
132
133
134
135 public void portalSessionDestroyed(PortalSessionMonitor psm)
136 {
137 PortalSessionRegistry psr = (PortalSessionRegistry)portalSessionsRegistry.remove(psm.getSessionId());
138 if ( psr != null )
139 {
140
141 Iterator iter = valuesShallowCopy(psr.sessionMonitors.values()).iterator();
142 while (iter.hasNext())
143 {
144 ((PortletApplicationSessionMonitor) iter.next()).invalidateSession();
145 }
146
147 try
148 {
149
150
151 psm.invalidateSession();
152 }
153 catch (IllegalStateException ise)
154 {
155
156 }
157 }
158 }
159
160
161
162
163 public void checkMonitorSession(String contextPath, HttpSession portalSession, HttpSession paSession)
164 {
165 if ( portalSession != null && paSession != null )
166 {
167 if (portalSession == paSession)
168 {
169
170 log.warn("servlet context name of paSession(" + paSession.getId() + "): " + paSession.getServletContext().getServletContextName());
171 return;
172 }
173
174 PortalSessionRegistry psr = (PortalSessionRegistry)portalSessionsRegistry.get(portalSession.getId());
175 if (psr == null)
176 {
177
178
179
180 PortalSessionMonitor psm = (PortalSessionMonitor)portalSession.getAttribute(PortalSessionMonitor.SESSION_KEY);
181
182 if ( psm == null )
183 {
184 portalSessionCreated(portalSession);
185 }
186 else
187 {
188
189
190
191
192 portalSessionDidActivate(psm);
193 }
194
195 psr = (PortalSessionRegistry)portalSessionsRegistry.get(portalSession.getId());
196 }
197 PortletApplicationSessionMonitor pasm = (PortletApplicationSessionMonitor)psr.sessionMonitors.get(contextPath);
198 if ( pasm != null )
199 {
200 try
201 {
202 if ( paSession.getAttribute(PortletApplicationSessionMonitor.SESSION_KEY) == null )
203 {
204
205
206 pasm.invalidateSession();
207 pasm = null;
208
209 }
210 }
211 catch (IllegalStateException ise)
212 {
213
214 }
215 }
216 if ( pasm == null )
217 {
218 pasm = new PortletApplicationSessionMonitorImpl(contextPath,portalSession.getId(),psr.portalSessionKey, forceInvalidate);
219 try
220 {
221 paSession.setAttribute(PortletApplicationSessionMonitor.SESSION_KEY, pasm);
222 psr.sessionMonitors.put(contextPath, pasm);
223 }
224 catch (IllegalStateException ise)
225 {
226
227 }
228 }
229 }
230 }
231
232
233
234
235 public void sessionWillPassivate(PortletApplicationSessionMonitor pasm)
236 {
237 PortalSessionRegistry psr = (PortalSessionRegistry)portalSessionsRegistry.get(pasm.getPortalSessionId());
238 if (psr != null )
239 {
240 psr.sessionMonitors.remove(pasm.getContextPath());
241 }
242 }
243
244
245
246
247 public void sessionDidActivate(PortletApplicationSessionMonitor restoredPasm)
248 {
249 PortalSessionRegistry psr = (PortalSessionRegistry)portalSessionsRegistry.get(restoredPasm.getPortalSessionId());
250 if ( psr == null )
251 {
252
253
254
255
256 psr = new PortalSessionRegistry();
257 psr.psm = new PortalSessionMonitorImpl(-1);
258 portalSessionsRegistry.put(restoredPasm.getPortalSessionId(), psr);
259 }
260
261
262 restoredPasm.getSession().setAttribute(PortletApplicationSessionMonitor.SESSION_KEY, restoredPasm);
263 psr.sessionMonitors.put(restoredPasm.getContextPath(), restoredPasm);
264 }
265
266
267
268
269 public void sessionDestroyed(PortletApplicationSessionMonitor pasm)
270 {
271 PortalSessionRegistry psr = (PortalSessionRegistry)portalSessionsRegistry.get(pasm.getPortalSessionId());
272 if ( psr != null )
273 {
274 psr.sessionMonitors.remove(pasm.getContextPath());
275
276 try
277 {
278
279
280 pasm.invalidateSession();
281 }
282 catch (IllegalStateException ise)
283 {
284
285 }
286 }
287 }
288
289
290
291
292 public int sessionCount() {
293
294 return portalSessionsRegistry.size();
295 }
296
297 /***
298 * Returns a shallow copy of the given Collection.
299 * @param inValues
300 * @return shallow copy
301 */
302 private Collection valuesShallowCopy(Collection inValues) {
303 return Arrays.asList(inValues.toArray());
304 }
305 }