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