001    /**
002     * Copyright (c) 2000-2013 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 com.liferay.portal.kernel.servlet.filters.compoundsessionid.CompoundSessionIdSplitterUtil;
018    
019    import java.util.Map;
020    import java.util.concurrent.ConcurrentHashMap;
021    import java.util.concurrent.ConcurrentMap;
022    
023    import javax.servlet.ServletContext;
024    import javax.servlet.http.HttpSession;
025    
026    /**
027     * <p>
028     * See http://issues.liferay.com/browse/LEP-1466.
029     * </p>
030     *
031     * @author Rudy Hilado
032     * @author Shuyang Zhou
033     */
034    public class PortletSessionTracker {
035    
036            public static void add(HttpSession session) {
037                    String sessionId = session.getId();
038    
039                    if (CompoundSessionIdSplitterUtil.hasSessionDelimiter()) {
040                            sessionId = CompoundSessionIdSplitterUtil.parseSessionId(sessionId);
041                    }
042    
043                    Map<String, HttpSession> sessions = _sessions.get(sessionId);
044    
045                    if (sessions == null) {
046                            sessions = new ConcurrentHashMap<String, HttpSession>();
047    
048                            Map<String, HttpSession> previousSessions = _sessions.putIfAbsent(
049                                    sessionId, sessions);
050    
051                            if (previousSessions != null) {
052                                    sessions = previousSessions;
053                            }
054                    }
055    
056                    ServletContext servletContext = session.getServletContext();
057    
058                    String contextPath = servletContext.getContextPath();
059    
060                    // ConcurrentHashMap's read is faster than its write. This check is
061                    // logically unnecessary, but is a performance improvement.
062    
063                    if (!sessions.containsKey(contextPath)) {
064                            sessions.put(contextPath, session);
065                    }
066            }
067    
068            public static void invalidate(String sessionId) {
069                    if (CompoundSessionIdSplitterUtil.hasSessionDelimiter()) {
070                            sessionId = CompoundSessionIdSplitterUtil.parseSessionId(sessionId);
071                    }
072    
073                    Map<String, HttpSession> sessions = _sessions.remove(sessionId);
074    
075                    if (sessions == null) {
076                            return;
077                    }
078    
079                    for (HttpSession session : sessions.values()) {
080                            try {
081                                    session.invalidate();
082                            }
083                            catch (Exception e) {
084                            }
085                    }
086            }
087    
088            private static ConcurrentMap<String, Map<String, HttpSession>> _sessions =
089                    new ConcurrentHashMap<String, Map<String, HttpSession>>();
090    
091    }