001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }