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.util.servlet;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.ListUtil;
020    
021    import java.util.Collections;
022    import java.util.Enumeration;
023    import java.util.List;
024    import java.util.Map;
025    import java.util.concurrent.ConcurrentHashMap;
026    
027    import javax.servlet.ServletContext;
028    import javax.servlet.http.HttpSession;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class SharedSessionWrapper implements HttpSession {
034    
035            public SharedSessionWrapper(HttpSession session) {
036                    this(session, new ConcurrentHashMap<String, Object>());
037            }
038    
039            public SharedSessionWrapper(
040                    HttpSession session, Map<String, Object> sharedAttributes) {
041    
042                    if (session == null) {
043                            _session = new NullSession();
044    
045                            if (_log.isWarnEnabled()) {
046                                    _log.warn("Wrapped session is null");
047                            }
048                    }
049                    else {
050                            _session = session;
051                    }
052    
053                    _sharedAttributes = sharedAttributes;
054            }
055    
056            public Object getAttribute(String name) {
057                    Object value = _session.getAttribute(name);
058    
059                    if (value == null) {
060                            value = _sharedAttributes.get(name);
061                    }
062    
063                    return value;
064            }
065    
066            public Enumeration<String> getAttributeNames() {
067                    if (_sharedAttributes.size() > 0) {
068    
069                            Enumeration<String> sessionAttributeNames =
070                                    _session.getAttributeNames();
071    
072                            List<String> names = null;
073    
074                            synchronized (sessionAttributeNames) {
075                                    names = ListUtil.fromEnumeration(sessionAttributeNames);
076                            }
077    
078                            names.addAll(_sharedAttributes.keySet());
079    
080                            return Collections.enumeration(names);
081                    }
082                    else {
083                            return _session.getAttributeNames();
084                    }
085            }
086    
087            public long getCreationTime() {
088                    return _session.getCreationTime();
089            }
090    
091            public String getId() {
092                    return _session.getId();
093            }
094    
095            public long getLastAccessedTime() {
096                    return _session.getLastAccessedTime();
097            }
098    
099            public int getMaxInactiveInterval() {
100                    return _session.getMaxInactiveInterval();
101            }
102    
103            public ServletContext getServletContext() {
104                    return _session.getServletContext();
105            }
106    
107            /**
108             * @deprecated
109             */
110            public javax.servlet.http.HttpSessionContext getSessionContext() {
111                    return _session.getSessionContext();
112            }
113    
114            public Object getValue(String name) {
115                    return getAttribute(name);
116            }
117    
118            public String[] getValueNames() {
119                    List<String> names = ListUtil.fromEnumeration(getAttributeNames());
120    
121                    return names.toArray(new String[names.size()]);
122            }
123    
124            public void invalidate() {
125                    _session.invalidate();
126            }
127    
128            public boolean isNew() {
129                    return _session.isNew();
130            }
131    
132            public void putValue(String name, Object value) {
133                    setAttribute(name, value);
134            }
135    
136            public void removeAttribute(String name) {
137                    _session.removeAttribute(name);
138            }
139    
140            public void removeValue(String name) {
141                    removeAttribute(name);
142            }
143    
144            public void setAttribute(String name, Object value) {
145                    _session.setAttribute(name, value);
146            }
147    
148            public void setMaxInactiveInterval(int maxInactiveInterval) {
149                    _session.setMaxInactiveInterval(maxInactiveInterval);
150            }
151    
152            private static Log _log = LogFactoryUtil.getLog(SharedSessionWrapper.class);
153    
154            private HttpSession _session;
155            private Map<String, Object> _sharedAttributes;
156    
157    }