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.servlet;
016    
017    import java.io.Serializable;
018    
019    import java.util.Map;
020    import java.util.concurrent.ConcurrentHashMap;
021    
022    import javax.servlet.http.HttpSession;
023    
024    /**
025     * @author Michael C. Han
026     */
027    public class SharedSessionAttributeCache implements Serializable {
028    
029            public static SharedSessionAttributeCache getInstance(HttpSession session) {
030                    synchronized (session) {
031                            SharedSessionAttributeCache cache =
032                                    (SharedSessionAttributeCache)session.getAttribute(_SESSION_KEY);
033    
034                            if (cache == null) {
035                                    cache = new SharedSessionAttributeCache();
036    
037                                    session.setAttribute(_SESSION_KEY, cache);
038                            }
039    
040                            return cache;
041                    }
042            }
043    
044            public boolean contains(String name) {
045                    return _attributes.containsKey(name);
046            }
047    
048            public Map<String, Object> getValues() {
049                    return _attributes;
050            }
051    
052            public void removeAttribute(String key) {
053                    _attributes.remove(key);
054            }
055    
056            public void setAttribute(String key, Object value) {
057                    _attributes.put(key, value);
058            }
059    
060            private SharedSessionAttributeCache() {
061                    _attributes = new ConcurrentHashMap<String, Object>();
062            }
063    
064            private static final String _SESSION_KEY =
065                    SharedSessionAttributeCache.class.getName();
066    
067            private Map<String, Object> _attributes;
068    
069    }