1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.util.servlet;
21  
22  import com.liferay.portal.kernel.util.ListUtil;
23  import com.liferay.portal.kernel.util.StringPool;
24  import com.liferay.util.PwdGenerator;
25  
26  import java.util.Collections;
27  import java.util.Enumeration;
28  import java.util.HashMap;
29  import java.util.List;
30  import java.util.Map;
31  
32  import javax.servlet.ServletContext;
33  import javax.servlet.http.HttpSession;
34  
35  /**
36   * <a href="NullSession.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   *
40   */
41  public class NullSession implements HttpSession {
42  
43      public NullSession() {
44          _attributes = new HashMap<String, Object>();
45          _creationTime = System.currentTimeMillis();
46          _id =
47              NullSession.class.getName() + StringPool.POUND +
48                  PwdGenerator.getPinNumber();
49          _lastAccessedTime = _creationTime;
50          _maxInactiveInterval = 0;
51          _servletContext = null;
52          _new = true;
53      }
54  
55      public Object getAttribute(String name) {
56          return _attributes.get(name);
57      }
58  
59      public Enumeration<String> getAttributeNames() {
60          return Collections.enumeration(_attributes.keySet());
61      }
62  
63      public long getCreationTime() {
64          return _creationTime;
65      }
66  
67      public String getId() {
68          return _id;
69      }
70  
71      public long getLastAccessedTime() {
72          return _lastAccessedTime;
73      }
74  
75      public int getMaxInactiveInterval() {
76          return _maxInactiveInterval;
77      }
78  
79      public ServletContext getServletContext() {
80          return _servletContext;
81      }
82  
83      /**
84       * @deprecated
85       */
86      public javax.servlet.http.HttpSessionContext getSessionContext() {
87          return null;
88      }
89  
90      public Object getValue(String name) {
91          return getAttribute(name);
92      }
93  
94      public String[] getValueNames() {
95          List<String> names = ListUtil.fromEnumeration(getAttributeNames());
96  
97          return names.toArray(new String[0]);
98      }
99  
100     public void invalidate() {
101         _attributes.clear();
102     }
103 
104     public boolean isNew() {
105         return _new;
106     }
107 
108     public void putValue(String name, Object value) {
109         setAttribute(name, value);
110     }
111 
112     public void removeAttribute(String name) {
113         _attributes.remove(name);
114     }
115 
116     public void removeValue(String name) {
117         removeAttribute(name);
118     }
119 
120     public void setAttribute(String name, Object value) {
121         _attributes.put(name, value);
122     }
123 
124     public void setMaxInactiveInterval(int maxInactiveInterval) {
125         _maxInactiveInterval = maxInactiveInterval;
126     }
127 
128     private Map<String, Object> _attributes;
129     private long _creationTime;
130     private String _id;
131     private long _lastAccessedTime;
132     private int _maxInactiveInterval;
133     private ServletContext _servletContext;
134     private boolean _new;
135 
136 }