1   /**
2    * Copyright (c) 2000-2008 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.util.ListUtil;
26  
27  import java.util.Collections;
28  import java.util.Enumeration;
29  import java.util.List;
30  import java.util.Map;
31  import java.util.concurrent.ConcurrentHashMap;
32  
33  import javax.servlet.ServletContext;
34  import javax.servlet.http.HttpSession;
35  
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  
39  /**
40   * <a href="SharedSessionWrapper.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class SharedSessionWrapper implements HttpSession {
46  
47      public SharedSessionWrapper(HttpSession session) {
48          this(session, new ConcurrentHashMap<String, Object>());
49      }
50  
51      public SharedSessionWrapper(
52          HttpSession session, Map<String, Object> sharedAttributes) {
53  
54          if (session == null) {
55              _session = new NullSession();
56  
57              if (_log.isWarnEnabled()) {
58                  _log.warn("Wrapped session is null");
59              }
60          }
61          else {
62              _session = session;
63          }
64  
65          _sharedAttributes = sharedAttributes;
66      }
67  
68      public Object getAttribute(String name) {
69          Object value = _session.getAttribute(name);
70  
71          if (value == null) {
72              value = _sharedAttributes.get(name);
73          }
74  
75          return value;
76      }
77  
78      public Enumeration<String> getAttributeNames() {
79          if (_sharedAttributes.size() > 0) {
80              List<String> names = ListUtil.fromEnumeration(
81                  _session.getAttributeNames());
82  
83              for (String name : _sharedAttributes.keySet()) {
84                  names.add(name);
85              }
86  
87              return Collections.enumeration(names);
88          }
89          else {
90              return _session.getAttributeNames();
91          }
92      }
93  
94      public long getCreationTime() {
95          return _session.getCreationTime();
96      }
97  
98      public String getId() {
99          return _session.getId();
100     }
101 
102     public long getLastAccessedTime() {
103         return _session.getLastAccessedTime();
104     }
105 
106     public int getMaxInactiveInterval() {
107         return _session.getMaxInactiveInterval();
108     }
109 
110     public ServletContext getServletContext() {
111         return _session.getServletContext();
112     }
113 
114     /**
115      * @deprecated
116      */
117     public javax.servlet.http.HttpSessionContext getSessionContext() {
118         return _session.getSessionContext();
119     }
120 
121     public Object getValue(String name) {
122         return getAttribute(name);
123     }
124 
125     public String[] getValueNames() {
126         List<String> names = ListUtil.fromEnumeration(getAttributeNames());
127 
128         return names.toArray(new String[names.size()]);
129     }
130 
131     public void invalidate() {
132         _session.invalidate();
133     }
134 
135     public boolean isNew() {
136         return _session.isNew();
137     }
138 
139     public void putValue(String name, Object value) {
140         setAttribute(name, value);
141     }
142 
143     public void removeAttribute(String name) {
144         _session.removeAttribute(name);
145     }
146 
147     public void removeValue(String name) {
148         removeAttribute(name);
149     }
150 
151     public void setAttribute(String name, Object value) {
152         _session.setAttribute(name, value);
153     }
154 
155     public void setMaxInactiveInterval(int maxInactiveInterval) {
156         _session.setMaxInactiveInterval(maxInactiveInterval);
157     }
158 
159     private static Log _log = LogFactory.getLog(SharedSessionWrapper.class);
160 
161     private HttpSession _session;
162     private Map<String, Object> _sharedAttributes;
163 
164 }