001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.kernel.servlet;
022:
023: import java.io.Serializable;
024:
025: import java.util.HashMap;
026: import java.util.HashSet;
027: import java.util.Iterator;
028: import java.util.Map;
029: import java.util.Set;
030:
031: import javax.servlet.http.HttpSession;
032: import javax.servlet.http.HttpSessionBindingEvent;
033: import javax.servlet.http.HttpSessionBindingListener;
034:
035: /**
036: * <a href="PortletSessionTracker.java.html"><b><i>View Source</i></b></a>
037: *
038: * <p>
039: * See http://support.liferay.com/browse/LEP-1466.
040: * </p>
041: *
042: * @author Rudy Hilado
043: *
044: */
045: public class PortletSessionTracker implements
046: HttpSessionBindingListener, Serializable {
047:
048: public static void add(HttpSession session) {
049: _instance._add(session);
050: }
051:
052: public static HttpSessionBindingListener getInstance() {
053: return _instance;
054: }
055:
056: public static void invalidate(String sessionId) {
057: _instance._invalidate(sessionId);
058: }
059:
060: public void valueBound(HttpSessionBindingEvent event) {
061: }
062:
063: public void valueUnbound(HttpSessionBindingEvent event) {
064: invalidate(event.getSession().getId());
065: }
066:
067: private PortletSessionTracker() {
068: _sessions = new HashMap();
069: }
070:
071: private void _add(HttpSession session) {
072: String sessionId = session.getId();
073:
074: synchronized (_sessions) {
075: Set portletSessions = (Set) _sessions.get(sessionId);
076:
077: if (portletSessions == null) {
078: portletSessions = new HashSet();
079:
080: _sessions.put(sessionId, portletSessions);
081: }
082:
083: portletSessions.add(session);
084: }
085: }
086:
087: private void _invalidate(String sessionId) {
088: synchronized (_sessions) {
089: Set portletSessions = (Set) _sessions.get(sessionId);
090:
091: if (portletSessions != null) {
092: Iterator itr = portletSessions.iterator();
093:
094: while (itr.hasNext()) {
095: HttpSession session = (HttpSession) itr.next();
096:
097: try {
098: session.invalidate();
099: } catch (Exception e) {
100: }
101: }
102: }
103:
104: _sessions.remove(sessionId);
105: }
106: }
107:
108: private static PortletSessionTracker _instance = new PortletSessionTracker();
109:
110: private transient Map _sessions;
111:
112: }
|