| PortletSessionTracker.java |
1 /**
2 * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 package com.liferay.portal.kernel.servlet;
24
25 import java.io.Serializable;
26
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.Iterator;
30 import java.util.Map;
31 import java.util.Set;
32
33 import javax.servlet.http.HttpSession;
34 import javax.servlet.http.HttpSessionBindingEvent;
35 import javax.servlet.http.HttpSessionBindingListener;
36
37 /**
38 * <a href="PortletSessionTracker.java.html"><b><i>View Source</i></b></a>
39 *
40 * <p>
41 * See http://support.liferay.com/browse/LEP-1466.
42 * </p>
43 *
44 * @author Rudy Hilado
45 *
46 */
47 public class PortletSessionTracker
48 implements HttpSessionBindingListener, Serializable {
49
50 public static void add(HttpSession session) {
51 _instance._add(session);
52 }
53
54 public static HttpSessionBindingListener getInstance() {
55 return _instance;
56 }
57
58 public static void invalidate(String sessionId) {
59 _instance._invalidate(sessionId);
60 }
61
62 public void valueBound(HttpSessionBindingEvent event) {
63 }
64
65 public void valueUnbound(HttpSessionBindingEvent event) {
66 invalidate(event.getSession().getId());
67 }
68
69 private PortletSessionTracker() {
70 _sessions = new HashMap<String, Set<HttpSession>>();
71 }
72
73 private void _add(HttpSession session) {
74 String sessionId = session.getId();
75
76 synchronized (_sessions) {
77 Set<HttpSession> portletSessions = _sessions.get(sessionId);
78
79 if (portletSessions == null) {
80 portletSessions = new HashSet<HttpSession>();
81
82 _sessions.put(sessionId, portletSessions);
83 }
84
85 portletSessions.add(session);
86 }
87 }
88
89 private void _invalidate(String sessionId) {
90 synchronized (_sessions) {
91 Set<HttpSession> portletSessions = _sessions.get(sessionId);
92
93 if (portletSessions != null) {
94 Iterator<HttpSession> itr = portletSessions.iterator();
95
96 while (itr.hasNext()) {
97 HttpSession session = itr.next();
98
99 try {
100 session.invalidate();
101 }
102 catch (Exception e) {
103 }
104 }
105 }
106
107 _sessions.remove(sessionId);
108 }
109 }
110
111 private static PortletSessionTracker _instance =
112 new PortletSessionTracker();
113
114 private transient Map<String, Set<HttpSession>> _sessions;
115
116 }