1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.util.servlet;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.ListUtil;
28  
29  import java.util.Collections;
30  import java.util.Enumeration;
31  import java.util.List;
32  import java.util.Map;
33  import java.util.concurrent.ConcurrentHashMap;
34  
35  import javax.servlet.ServletContext;
36  import javax.servlet.http.HttpSession;
37  
38  /**
39   * <a href="SharedSessionWrapper.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   */
43  public class SharedSessionWrapper implements HttpSession {
44  
45      public SharedSessionWrapper(HttpSession session) {
46          this(session, new ConcurrentHashMap<String, Object>());
47      }
48  
49      public SharedSessionWrapper(
50          HttpSession session, Map<String, Object> sharedAttributes) {
51  
52          if (session == null) {
53              _session = new NullSession();
54  
55              if (_log.isWarnEnabled()) {
56                  _log.warn("Wrapped session is null");
57              }
58          }
59          else {
60              _session = session;
61          }
62  
63          _sharedAttributes = sharedAttributes;
64      }
65  
66      public Object getAttribute(String name) {
67          Object value = _session.getAttribute(name);
68  
69          if (value == null) {
70              value = _sharedAttributes.get(name);
71          }
72  
73          return value;
74      }
75  
76      public Enumeration<String> getAttributeNames() {
77          if (_sharedAttributes.size() > 0) {
78  
79              Enumeration<String> sessionAttributeNames =
80                  _session.getAttributeNames();
81  
82              List<String> names = null;
83  
84              synchronized (sessionAttributeNames) {
85                  names = ListUtil.fromEnumeration(sessionAttributeNames);
86              }
87  
88              for (String name : _sharedAttributes.keySet()) {
89                  names.add(name);
90              }
91  
92              return Collections.enumeration(names);
93          }
94          else {
95              return _session.getAttributeNames();
96          }
97      }
98  
99      public long getCreationTime() {
100         return _session.getCreationTime();
101     }
102 
103     public String getId() {
104         return _session.getId();
105     }
106 
107     public long getLastAccessedTime() {
108         return _session.getLastAccessedTime();
109     }
110 
111     public int getMaxInactiveInterval() {
112         return _session.getMaxInactiveInterval();
113     }
114 
115     public ServletContext getServletContext() {
116         return _session.getServletContext();
117     }
118 
119     /**
120      * @deprecated
121      */
122     public javax.servlet.http.HttpSessionContext getSessionContext() {
123         return _session.getSessionContext();
124     }
125 
126     public Object getValue(String name) {
127         return getAttribute(name);
128     }
129 
130     public String[] getValueNames() {
131         List<String> names = ListUtil.fromEnumeration(getAttributeNames());
132 
133         return names.toArray(new String[names.size()]);
134     }
135 
136     public void invalidate() {
137         _session.invalidate();
138     }
139 
140     public boolean isNew() {
141         return _session.isNew();
142     }
143 
144     public void putValue(String name, Object value) {
145         setAttribute(name, value);
146     }
147 
148     public void removeAttribute(String name) {
149         _session.removeAttribute(name);
150     }
151 
152     public void removeValue(String name) {
153         removeAttribute(name);
154     }
155 
156     public void setAttribute(String name, Object value) {
157         _session.setAttribute(name, value);
158     }
159 
160     public void setMaxInactiveInterval(int maxInactiveInterval) {
161         _session.setMaxInactiveInterval(maxInactiveInterval);
162     }
163 
164     private static Log _log = LogFactoryUtil.getLog(SharedSessionWrapper.class);
165 
166     private HttpSession _session;
167     private Map<String, Object> _sharedAttributes;
168 
169 }