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.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 ses) {
48          this(ses, new ConcurrentHashMap<String, Object>());
49      }
50  
51      public SharedSessionWrapper(
52          HttpSession ses, Map<String, Object> sharedAttributes) {
53  
54          if (ses == null) {
55              _ses = new NullSession();
56  
57              if (_log.isWarnEnabled()) {
58                  _log.warn("Wrapped session is null");
59              }
60          }
61          else {
62              _ses = ses;
63          }
64  
65          _sharedAttributes = sharedAttributes;
66      }
67  
68      public Object getAttribute(String name) {
69          Object value = _ses.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(_ses.getAttributeNames());
81  
82              for (String name : _sharedAttributes.keySet()) {
83                  names.add(name);
84              }
85  
86              return Collections.enumeration(names);
87          }
88          else {
89              return _ses.getAttributeNames();
90          }
91      }
92  
93      public long getCreationTime() {
94          return _ses.getCreationTime();
95      }
96  
97      public String getId() {
98          return _ses.getId();
99      }
100 
101     public long getLastAccessedTime() {
102         return _ses.getLastAccessedTime();
103     }
104 
105     public int getMaxInactiveInterval() {
106         return _ses.getMaxInactiveInterval();
107     }
108 
109     public ServletContext getServletContext() {
110         return _ses.getServletContext();
111     }
112 
113     /**
114      * @deprecated
115      */
116     public javax.servlet.http.HttpSessionContext getSessionContext() {
117         return _ses.getSessionContext();
118     }
119 
120     public Object getValue(String name) {
121         return getAttribute(name);
122     }
123 
124     public String[] getValueNames() {
125         List<String> names = ListUtil.fromEnumeration(getAttributeNames());
126 
127         return names.toArray(new String[names.size()]);
128     }
129 
130     public void invalidate() {
131         _ses.invalidate();
132     }
133 
134     public boolean isNew() {
135         return _ses.isNew();
136     }
137 
138     public void putValue(String name, Object value) {
139         setAttribute(name, value);
140     }
141 
142     public void removeAttribute(String name) {
143         _ses.removeAttribute(name);
144     }
145 
146     public void removeValue(String name) {
147         removeAttribute(name);
148     }
149 
150     public void setAttribute(String name, Object value) {
151         _ses.setAttribute(name, value);
152     }
153 
154     public void setMaxInactiveInterval(int maxInactiveInterval) {
155         _ses.setMaxInactiveInterval(maxInactiveInterval);
156     }
157 
158     private static Log _log = LogFactory.getLog(SharedSessionWrapper.class);
159 
160     private HttpSession _ses;
161     private Map<String, Object> _sharedAttributes;
162 
163 }