001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.servlet;
016    
017    import java.io.Serializable;
018    
019    import java.util.HashMap;
020    import java.util.HashSet;
021    import java.util.Iterator;
022    import java.util.Map;
023    import java.util.Set;
024    
025    import javax.servlet.http.HttpSession;
026    import javax.servlet.http.HttpSessionBindingEvent;
027    import javax.servlet.http.HttpSessionBindingListener;
028    import javax.servlet.http.HttpSessionEvent;
029    import javax.servlet.http.HttpSessionListener;
030    
031    /**
032     * <p>
033     * See http://issues.liferay.com/browse/LEP-1466.
034     * </p>
035     *
036     * @author Rudy Hilado
037     */
038    public class PortletSessionTracker
039            implements HttpSessionListener, HttpSessionBindingListener, Serializable {
040    
041            public static void add(HttpSession session) {
042                    _instance._add(session);
043            }
044    
045            public static void invalidate(HttpSession session) {
046                    _instance._invalidate(session.getId());
047            }
048    
049            public static HttpSessionBindingListener getInstance() {
050                    return _instance;
051            }
052    
053            public static void invalidate(String sessionId) {
054                    _instance._invalidate(sessionId);
055            }
056    
057            public void sessionCreated(HttpSessionEvent httpSessionEvent) {
058            }
059    
060            public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
061                    _invalidate(httpSessionEvent.getSession().getId());
062            }
063    
064            public void valueBound(HttpSessionBindingEvent event) {
065            }
066    
067            public void valueUnbound(HttpSessionBindingEvent event) {
068                    invalidate(event.getSession().getId());
069            }
070    
071            private PortletSessionTracker() {
072                    _sessions = new HashMap<String, Set<HttpSession>>();
073    
074                    PortletSessionListenerManager.addListener(this);
075            }
076    
077            private void _add(HttpSession session) {
078                    String sessionId = session.getId();
079    
080                    synchronized (_sessions) {
081                            Set<HttpSession> portletSessions = _sessions.get(sessionId);
082    
083                            if (portletSessions == null) {
084                                    portletSessions = new HashSet<HttpSession>();
085    
086                                    _sessions.put(sessionId, portletSessions);
087                            }
088    
089                            portletSessions.add(session);
090                    }
091            }
092    
093            private void _invalidate(String sessionId) {
094                    Set<HttpSession> sessionsToInvalidate = null;
095    
096                    synchronized (_sessions) {
097                            Set<HttpSession> portletSessions = _sessions.get(sessionId);
098    
099                            if (portletSessions != null) {
100                                    sessionsToInvalidate = new HashSet<HttpSession>(
101                                            portletSessions);
102                            }
103    
104                            _sessions.remove(sessionId);
105                    }
106    
107                    if (sessionsToInvalidate != null) {
108                            Iterator<HttpSession> itr = sessionsToInvalidate.iterator();
109    
110                            while (itr.hasNext()) {
111                                    HttpSession session = itr.next();
112    
113                                    try {
114                                            session.invalidate();
115                                    }
116                                    catch (Exception e) {
117                                    }
118                            }
119                    }
120            }
121    
122            private static PortletSessionTracker _instance =
123                    new PortletSessionTracker();
124    
125            private transient Map<String, Set<HttpSession>> _sessions;
126    
127    }