001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util.servlet;
016    
017    import com.liferay.portal.kernel.util.ListUtil;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.StringUtil;
020    
021    import java.util.Collections;
022    import java.util.Enumeration;
023    import java.util.HashMap;
024    import java.util.List;
025    import java.util.Map;
026    
027    import javax.servlet.ServletContext;
028    import javax.servlet.http.HttpSession;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class NullSession implements HttpSession {
034    
035            public NullSession() {
036                    _attributes = new HashMap<String, Object>();
037                    _creationTime = System.currentTimeMillis();
038                    _id =
039                            NullSession.class.getName() + StringPool.POUND +
040                                    StringUtil.randomId();
041                    _lastAccessedTime = _creationTime;
042                    _maxInactiveInterval = 0;
043                    _servletContext = null;
044                    _new = true;
045            }
046    
047            @Override
048            public Object getAttribute(String name) {
049                    return _attributes.get(name);
050            }
051    
052            @Override
053            public Enumeration<String> getAttributeNames() {
054                    return Collections.enumeration(_attributes.keySet());
055            }
056    
057            @Override
058            public long getCreationTime() {
059                    return _creationTime;
060            }
061    
062            @Override
063            public String getId() {
064                    return _id;
065            }
066    
067            @Override
068            public long getLastAccessedTime() {
069                    return _lastAccessedTime;
070            }
071    
072            @Override
073            public int getMaxInactiveInterval() {
074                    return _maxInactiveInterval;
075            }
076    
077            @Override
078            public ServletContext getServletContext() {
079                    return _servletContext;
080            }
081    
082            /**
083             * @deprecated As of 6.1.0
084             */
085            @Deprecated
086            @Override
087            public javax.servlet.http.HttpSessionContext getSessionContext() {
088                    return null;
089            }
090    
091            @Override
092            public Object getValue(String name) {
093                    return getAttribute(name);
094            }
095    
096            @Override
097            public String[] getValueNames() {
098                    List<String> names = ListUtil.fromEnumeration(getAttributeNames());
099    
100                    return names.toArray(new String[0]);
101            }
102    
103            @Override
104            public void invalidate() {
105                    _attributes.clear();
106            }
107    
108            @Override
109            public boolean isNew() {
110                    return _new;
111            }
112    
113            @Override
114            public void putValue(String name, Object value) {
115                    setAttribute(name, value);
116            }
117    
118            @Override
119            public void removeAttribute(String name) {
120                    _attributes.remove(name);
121            }
122    
123            @Override
124            public void removeValue(String name) {
125                    removeAttribute(name);
126            }
127    
128            @Override
129            public void setAttribute(String name, Object value) {
130                    _attributes.put(name, value);
131            }
132    
133            @Override
134            public void setMaxInactiveInterval(int maxInactiveInterval) {
135                    _maxInactiveInterval = maxInactiveInterval;
136            }
137    
138            private final Map<String, Object> _attributes;
139            private final long _creationTime;
140            private final String _id;
141            private final long _lastAccessedTime;
142            private int _maxInactiveInterval;
143            private final boolean _new;
144            private final ServletContext _servletContext;
145    
146    }