1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.util.servlet;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.ListUtil;
20  
21  import java.util.Collections;
22  import java.util.Enumeration;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.concurrent.ConcurrentHashMap;
26  
27  import javax.servlet.ServletContext;
28  import javax.servlet.http.HttpSession;
29  
30  /**
31   * <a href="SharedSessionWrapper.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   */
35  public class SharedSessionWrapper implements HttpSession {
36  
37      public SharedSessionWrapper(HttpSession session) {
38          this(session, new ConcurrentHashMap<String, Object>());
39      }
40  
41      public SharedSessionWrapper(
42          HttpSession session, Map<String, Object> sharedAttributes) {
43  
44          if (session == null) {
45              _session = new NullSession();
46  
47              if (_log.isWarnEnabled()) {
48                  _log.warn("Wrapped session is null");
49              }
50          }
51          else {
52              _session = session;
53          }
54  
55          _sharedAttributes = sharedAttributes;
56      }
57  
58      public Object getAttribute(String name) {
59          Object value = _session.getAttribute(name);
60  
61          if (value == null) {
62              value = _sharedAttributes.get(name);
63          }
64  
65          return value;
66      }
67  
68      public Enumeration<String> getAttributeNames() {
69          if (_sharedAttributes.size() > 0) {
70  
71              Enumeration<String> sessionAttributeNames =
72                  _session.getAttributeNames();
73  
74              List<String> names = null;
75  
76              synchronized (sessionAttributeNames) {
77                  names = ListUtil.fromEnumeration(sessionAttributeNames);
78              }
79  
80              for (String name : _sharedAttributes.keySet()) {
81                  names.add(name);
82              }
83  
84              return Collections.enumeration(names);
85          }
86          else {
87              return _session.getAttributeNames();
88          }
89      }
90  
91      public long getCreationTime() {
92          return _session.getCreationTime();
93      }
94  
95      public String getId() {
96          return _session.getId();
97      }
98  
99      public long getLastAccessedTime() {
100         return _session.getLastAccessedTime();
101     }
102 
103     public int getMaxInactiveInterval() {
104         return _session.getMaxInactiveInterval();
105     }
106 
107     public ServletContext getServletContext() {
108         return _session.getServletContext();
109     }
110 
111     /**
112      * @deprecated
113      */
114     public javax.servlet.http.HttpSessionContext getSessionContext() {
115         return _session.getSessionContext();
116     }
117 
118     public Object getValue(String name) {
119         return getAttribute(name);
120     }
121 
122     public String[] getValueNames() {
123         List<String> names = ListUtil.fromEnumeration(getAttributeNames());
124 
125         return names.toArray(new String[names.size()]);
126     }
127 
128     public void invalidate() {
129         _session.invalidate();
130     }
131 
132     public boolean isNew() {
133         return _session.isNew();
134     }
135 
136     public void putValue(String name, Object value) {
137         setAttribute(name, value);
138     }
139 
140     public void removeAttribute(String name) {
141         _session.removeAttribute(name);
142     }
143 
144     public void removeValue(String name) {
145         removeAttribute(name);
146     }
147 
148     public void setAttribute(String name, Object value) {
149         _session.setAttribute(name, value);
150     }
151 
152     public void setMaxInactiveInterval(int maxInactiveInterval) {
153         _session.setMaxInactiveInterval(maxInactiveInterval);
154     }
155 
156     private static Log _log = LogFactoryUtil.getLog(SharedSessionWrapper.class);
157 
158     private HttpSession _session;
159     private Map<String, Object> _sharedAttributes;
160 
161 }