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.Collections;
20 import java.util.HashMap;
21 import java.util.Iterator;
22 import java.util.Map;
23
24 import javax.servlet.http.HttpSession;
25
26 /***
27 * PortalSessionsManagerImpl
28 *
29 * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
30 * @version $Id: $
31 */
32 public class PortalSessionsManagerImpl implements PortalSessionsManager
33 {
34 private static final class PortalSessionRegistry
35 {
36 long portalSessionKey;
37 PortalSessionMonitor psm;
38 Map sessionMonitors;
39
40 PortalSessionRegistry()
41 {
42 sessionMonitors = Collections.synchronizedMap(new HashMap());
43 }
44 }
45
46 private long portalSessionKeySequence;
47 private Map portalSessionsRegistry;
48 private boolean forceInvalidate;
49
50 public PortalSessionsManagerImpl()
51 {
52 this(true);
53 }
54
55 public PortalSessionsManagerImpl(boolean forceInvalidate)
56 {
57 portalSessionKeySequence = System.currentTimeMillis();
58 portalSessionsRegistry = Collections.synchronizedMap(new HashMap());
59 this.forceInvalidate = forceInvalidate;
60 }
61
62
63
64
65 public synchronized void portalSessionCreated(HttpSession portalSession)
66 {
67 PortalSessionMonitor psm = new PortalSessionMonitorImpl(++portalSessionKeySequence, forceInvalidate);
68 portalSession.setAttribute(PortalSessionMonitor.SESSION_KEY, psm);
69
70 portalSessionDidActivate(psm);
71 }
72
73
74
75
76 public synchronized void portalSessionWillPassivate(PortalSessionMonitor psm)
77 {
78 portalSessionsRegistry.remove(psm.getSessionId());
79 }
80
81
82
83
84 public synchronized void portalSessionDidActivate(PortalSessionMonitor restoredPsm)
85 {
86 PortalSessionRegistry psr = (PortalSessionRegistry)portalSessionsRegistry.get(restoredPsm.getSessionId());
87 if ( psr != null && psr.portalSessionKey != -1 && psr.portalSessionKey != restoredPsm.getSessionKey() )
88 {
89
90
91 portalSessionDestroyed(psr.psm);
92 psr = null;
93 }
94 if ( psr == null )
95 {
96 psr = new PortalSessionRegistry();
97 portalSessionsRegistry.put(restoredPsm.getSessionId(), psr);
98 }
99
100 psr.psm = restoredPsm;
101 psr.portalSessionKey = restoredPsm.getSessionKey();
102
103 Iterator iter = psr.sessionMonitors.values().iterator();
104 PortletApplicationSessionMonitor pasm;
105 while (iter.hasNext())
106 {
107 pasm = (PortletApplicationSessionMonitor)iter.next();
108 if ( pasm.getPortalSessionKey() != psr.portalSessionKey )
109 {
110 pasm.invalidateSession();
111 iter.remove();
112 }
113 }
114 }
115
116
117
118
119 public synchronized void portalSessionDestroyed(PortalSessionMonitor psm)
120 {
121 PortalSessionRegistry psr = (PortalSessionRegistry)portalSessionsRegistry.remove(psm.getSessionId());
122 if ( psr != null )
123 {
124 Iterator iter = psr.sessionMonitors.values().iterator();
125 while (iter.hasNext())
126 {
127 ((PortletApplicationSessionMonitor)iter.next()).invalidateSession();
128 }
129
130
131 psm.invalidateSession();
132 }
133 }
134
135
136
137
138 public synchronized void checkMonitorSession(String contextPath, HttpSession portalSession, HttpSession paSession)
139 {
140 if ( portalSession != null && paSession != null )
141 {
142 PortalSessionRegistry psr = (PortalSessionRegistry)portalSessionsRegistry.get(portalSession.getId());
143 if (psr == null)
144 {
145
146
147
148 PortalSessionMonitor psm = (PortalSessionMonitor)portalSession.getAttribute(PortalSessionMonitor.SESSION_KEY);
149
150 if ( psm == null )
151 {
152 portalSessionCreated(portalSession);
153 }
154 else
155 {
156
157
158
159
160 portalSessionDidActivate(psm);
161 }
162
163 psr = (PortalSessionRegistry)portalSessionsRegistry.get(portalSession.getId());
164 }
165 PortletApplicationSessionMonitor pasm = (PortletApplicationSessionMonitor)psr.sessionMonitors.get(contextPath);
166 if ( pasm != null )
167 {
168 try
169 {
170 if ( paSession.getAttribute(PortletApplicationSessionMonitor.SESSION_KEY) == null )
171 {
172
173
174 pasm.invalidateSession();
175 pasm = null;
176
177 }
178 }
179 catch (IllegalStateException ise)
180 {
181
182 }
183 }
184 if ( pasm == null )
185 {
186 pasm = new PortletApplicationSessionMonitorImpl(contextPath,portalSession.getId(),psr.portalSessionKey, forceInvalidate);
187 try
188 {
189 paSession.setAttribute(PortletApplicationSessionMonitor.SESSION_KEY, pasm);
190 psr.sessionMonitors.put(contextPath, pasm);
191 }
192 catch (IllegalStateException ise)
193 {
194
195 }
196 }
197 }
198 }
199
200
201
202
203 public void sessionWillPassivate(PortletApplicationSessionMonitor pasm)
204 {
205 PortalSessionRegistry psr = (PortalSessionRegistry)portalSessionsRegistry.get(pasm.getPortalSessionId());
206 if (psr != null )
207 {
208 psr.sessionMonitors.remove(pasm.getContextPath());
209 }
210 }
211
212
213
214
215 public synchronized void sessionDidActivate(PortletApplicationSessionMonitor restoredPasm)
216 {
217 PortalSessionRegistry psr = (PortalSessionRegistry)portalSessionsRegistry.get(restoredPasm.getPortalSessionId());
218 if ( psr == null )
219 {
220
221
222
223
224 psr = new PortalSessionRegistry();
225 psr.psm = new PortalSessionMonitorImpl(-1);
226 portalSessionsRegistry.put(restoredPasm.getPortalSessionId(), psr);
227 }
228
229 psr.sessionMonitors.put(restoredPasm.getContextPath(), restoredPasm);
230 }
231
232
233
234
235 public synchronized void sessionDestroyed(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 pasm.invalidateSession();
244 }
245 }
246
247
248
249
250 public int sessionCount() {
251
252 return portalSessionsRegistry.size();
253 }
254 }