1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class NullSession implements HttpSession {
44  
45      public NullSession() {
46          _attributes = new HashMap<String, Object>();
47          _creationTime = System.currentTimeMillis();
48          _id =
49              NullSession.class.getName() + StringPool.POUND +
50                  PwdGenerator.getPinNumber();
51          _lastAccessedTime = _creationTime;
52          _maxInactiveInterval = 0;
53          _servletContext = null;
54          _new = true;
55      }
56  
57      public Object getAttribute(String name) {
58          return _attributes.get(name);
59      }
60  
61      public Enumeration<String> getAttributeNames() {
62          return Collections.enumeration(_attributes.keySet());
63      }
64  
65      public long getCreationTime() {
66          return _creationTime;
67      }
68  
69      public String getId() {
70          return _id;
71      }
72  
73      public long getLastAccessedTime() {
74          return _lastAccessedTime;
75      }
76  
77      public int getMaxInactiveInterval() {
78          return _maxInactiveInterval;
79      }
80  
81      public ServletContext getServletContext() {
82          return _servletContext;
83      }
84  
85      /**
86       * @deprecated
87       */
88      public javax.servlet.http.HttpSessionContext getSessionContext() {
89          return null;
90      }
91  
92      public Object getValue(String name) {
93          return getAttribute(name);
94      }
95  
96      public String[] getValueNames() {
97          List<String> names = ListUtil.fromEnumeration(getAttributeNames());
98  
99          return names.toArray(new String[0]);
100     }
101 
102     public void invalidate() {
103         _attributes.clear();
104     }
105 
106     public boolean isNew() {
107         return _new;
108     }
109 
110     public void putValue(String name, Object value) {
111         setAttribute(name, value);
112     }
113 
114     public void removeAttribute(String name) {
115         _attributes.remove(name);
116     }
117 
118     public void removeValue(String name) {
119         removeAttribute(name);
120     }
121 
122     public void setAttribute(String name, Object value) {
123         _attributes.put(name, value);
124     }
125 
126     public void setMaxInactiveInterval(int maxInactiveInterval) {
127         _maxInactiveInterval = maxInactiveInterval;
128     }
129 
130     private Map<String, Object> _attributes;
131     private long _creationTime;
132     private String _id;
133     private long _lastAccessedTime;
134     private int _maxInactiveInterval;
135     private ServletContext _servletContext;
136     private boolean _new;
137 
138 }