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