1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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   */
44  public class SharedSessionWrapper implements HttpSession {
45  
46      public SharedSessionWrapper(HttpSession session) {
47          this(session, new ConcurrentHashMap<String, Object>());
48      }
49  
50      public SharedSessionWrapper(
51          HttpSession session, Map<String, Object> sharedAttributes) {
52  
53          if (session == null) {
54              _session = new NullSession();
55  
56              if (_log.isWarnEnabled()) {
57                  _log.warn("Wrapped session is null");
58              }
59          }
60          else {
61              _session = session;
62          }
63  
64          _sharedAttributes = sharedAttributes;
65      }
66  
67      public Object getAttribute(String name) {
68          Object value = _session.getAttribute(name);
69  
70          if (value == null) {
71              value = _sharedAttributes.get(name);
72          }
73  
74          return value;
75      }
76  
77      public Enumeration<String> getAttributeNames() {
78          if (_sharedAttributes.size() > 0) {
79  
80              Enumeration<String> sessionAttributeNames =
81                  _session.getAttributeNames();
82  
83              List<String> names = null;
84  
85              synchronized (sessionAttributeNames) {
86                  names = ListUtil.fromEnumeration(sessionAttributeNames);
87              }
88  
89              for (String name : _sharedAttributes.keySet()) {
90                  names.add(name);
91              }
92  
93              return Collections.enumeration(names);
94          }
95          else {
96              return _session.getAttributeNames();
97          }
98      }
99  
100     public long getCreationTime() {
101         return _session.getCreationTime();
102     }
103 
104     public String getId() {
105         return _session.getId();
106     }
107 
108     public long getLastAccessedTime() {
109         return _session.getLastAccessedTime();
110     }
111 
112     public int getMaxInactiveInterval() {
113         return _session.getMaxInactiveInterval();
114     }
115 
116     public ServletContext getServletContext() {
117         return _session.getServletContext();
118     }
119 
120     /**
121      * @deprecated
122      */
123     public javax.servlet.http.HttpSessionContext getSessionContext() {
124         return _session.getSessionContext();
125     }
126 
127     public Object getValue(String name) {
128         return getAttribute(name);
129     }
130 
131     public String[] getValueNames() {
132         List<String> names = ListUtil.fromEnumeration(getAttributeNames());
133 
134         return names.toArray(new String[names.size()]);
135     }
136 
137     public void invalidate() {
138         _session.invalidate();
139     }
140 
141     public boolean isNew() {
142         return _session.isNew();
143     }
144 
145     public void putValue(String name, Object value) {
146         setAttribute(name, value);
147     }
148 
149     public void removeAttribute(String name) {
150         _session.removeAttribute(name);
151     }
152 
153     public void removeValue(String name) {
154         removeAttribute(name);
155     }
156 
157     public void setAttribute(String name, Object value) {
158         _session.setAttribute(name, value);
159     }
160 
161     public void setMaxInactiveInterval(int maxInactiveInterval) {
162         _session.setMaxInactiveInterval(maxInactiveInterval);
163     }
164 
165     private static Log _log = LogFactoryUtil.getLog(SharedSessionWrapper.class);
166 
167     private HttpSession _session;
168     private Map<String, Object> _sharedAttributes;
169 
170 }