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.util.Validator;
018    
019    import java.util.Collection;
020    import java.util.Map;
021    import java.util.concurrent.ConcurrentHashMap;
022    
023    import javax.servlet.http.HttpSession;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class PortalSessionContext {
029    
030            public static int count() {
031                    return _instance._count();
032            }
033    
034            public static HttpSession get(String sessionId) {
035                    return _instance._get(sessionId);
036            }
037    
038            public static void put(String sessionId, HttpSession session) {
039                    _instance._put(sessionId, session);
040            }
041    
042            public static HttpSession remove(String sessionId) {
043                    return _instance._remove(sessionId);
044            }
045    
046            public static Collection<HttpSession> values() {
047                    return _instance._values();
048            }
049    
050            protected PortalSessionContext() {
051                    _sessionPool = new ConcurrentHashMap<String, HttpSession>();
052            }
053    
054            private int _count() {
055                    return _sessionPool.size();
056            }
057    
058            private HttpSession _get(String sessionId) {
059                    if (Validator.isNull(sessionId)) {
060                            return null;
061                    }
062    
063                    return _sessionPool.get(sessionId);
064            }
065    
066            private void _put(String sessionId, HttpSession session) {
067                    _sessionPool.put(sessionId, session);
068            }
069    
070            private HttpSession _remove(String sessionId) {
071                    return _sessionPool.remove(sessionId);
072            }
073    
074            private Collection<HttpSession> _values() {
075                    return _sessionPool.values();
076            }
077    
078            private static PortalSessionContext _instance = new PortalSessionContext();
079    
080            private Map<String, HttpSession> _sessionPool;
081    
082    }