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                            for (String name : _sharedAttributes.keySet()) {
079                                    names.add(name);
080                            }
081    
082                            return Collections.enumeration(names);
083                    }
084                    else {
085                            return _session.getAttributeNames();
086                    }
087            }
088    
089            public long getCreationTime() {
090                    return _session.getCreationTime();
091            }
092    
093            public String getId() {
094                    return _session.getId();
095            }
096    
097            public long getLastAccessedTime() {
098                    return _session.getLastAccessedTime();
099            }
100    
101            public int getMaxInactiveInterval() {
102                    return _session.getMaxInactiveInterval();
103            }
104    
105            public ServletContext getServletContext() {
106                    return _session.getServletContext();
107            }
108    
109            /**
110             * @deprecated
111             */
112            public javax.servlet.http.HttpSessionContext getSessionContext() {
113                    return _session.getSessionContext();
114            }
115    
116            public Object getValue(String name) {
117                    return getAttribute(name);
118            }
119    
120            public String[] getValueNames() {
121                    List<String> names = ListUtil.fromEnumeration(getAttributeNames());
122    
123                    return names.toArray(new String[names.size()]);
124            }
125    
126            public void invalidate() {
127                    _session.invalidate();
128            }
129    
130            public boolean isNew() {
131                    return _session.isNew();
132            }
133    
134            public void putValue(String name, Object value) {
135                    setAttribute(name, value);
136            }
137    
138            public void removeAttribute(String name) {
139                    _session.removeAttribute(name);
140            }
141    
142            public void removeValue(String name) {
143                    removeAttribute(name);
144            }
145    
146            public void setAttribute(String name, Object value) {
147                    _session.setAttribute(name, value);
148            }
149    
150            public void setMaxInactiveInterval(int maxInactiveInterval) {
151                    _session.setMaxInactiveInterval(maxInactiveInterval);
152            }
153    
154            private static Log _log = LogFactoryUtil.getLog(SharedSessionWrapper.class);
155    
156            private HttpSession _session;
157            private Map<String, Object> _sharedAttributes;
158    
159    }