1   /**
2    * Copyright (c) 2000-2009 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  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.util.PwdGenerator;
28  
29  import java.util.Collections;
30  import java.util.Enumeration;
31  import java.util.HashMap;
32  import java.util.List;
33  import java.util.Map;
34  
35  import javax.servlet.ServletContext;
36  import javax.servlet.http.HttpSession;
37  
38  /**
39   * <a href="NullSession.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class NullSession implements HttpSession {
45  
46      public NullSession() {
47          _attributes = new HashMap<String, Object>();
48          _creationTime = System.currentTimeMillis();
49          _id =
50              NullSession.class.getName() + StringPool.POUND +
51                  PwdGenerator.getPinNumber();
52          _lastAccessedTime = _creationTime;
53          _maxInactiveInterval = 0;
54          _servletContext = null;
55          _new = true;
56      }
57  
58      public Object getAttribute(String name) {
59          return _attributes.get(name);
60      }
61  
62      public Enumeration<String> getAttributeNames() {
63          return Collections.enumeration(_attributes.keySet());
64      }
65  
66      public long getCreationTime() {
67          return _creationTime;
68      }
69  
70      public String getId() {
71          return _id;
72      }
73  
74      public long getLastAccessedTime() {
75          return _lastAccessedTime;
76      }
77  
78      public int getMaxInactiveInterval() {
79          return _maxInactiveInterval;
80      }
81  
82      public ServletContext getServletContext() {
83          return _servletContext;
84      }
85  
86      /**
87       * @deprecated
88       */
89      public javax.servlet.http.HttpSessionContext getSessionContext() {
90          return null;
91      }
92  
93      public Object getValue(String name) {
94          return getAttribute(name);
95      }
96  
97      public String[] getValueNames() {
98          List<String> names = ListUtil.fromEnumeration(getAttributeNames());
99  
100         return names.toArray(new String[0]);
101     }
102 
103     public void invalidate() {
104         _attributes.clear();
105     }
106 
107     public boolean isNew() {
108         return _new;
109     }
110 
111     public void putValue(String name, Object value) {
112         setAttribute(name, value);
113     }
114 
115     public void removeAttribute(String name) {
116         _attributes.remove(name);
117     }
118 
119     public void removeValue(String name) {
120         removeAttribute(name);
121     }
122 
123     public void setAttribute(String name, Object value) {
124         _attributes.put(name, value);
125     }
126 
127     public void setMaxInactiveInterval(int maxInactiveInterval) {
128         _maxInactiveInterval = maxInactiveInterval;
129     }
130 
131     private Map<String, Object> _attributes;
132     private long _creationTime;
133     private String _id;
134     private long _lastAccessedTime;
135     private int _maxInactiveInterval;
136     private ServletContext _servletContext;
137     private boolean _new;
138 
139 }