001    /**
002     * Copyright (c) 2000-2013 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.util.PwdGenerator;
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                                    PwdGenerator.getPinNumber();
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
084             */
085            @Override
086            public javax.servlet.http.HttpSessionContext getSessionContext() {
087                    return null;
088            }
089    
090            @Override
091            public Object getValue(String name) {
092                    return getAttribute(name);
093            }
094    
095            @Override
096            public String[] getValueNames() {
097                    List<String> names = ListUtil.fromEnumeration(getAttributeNames());
098    
099                    return names.toArray(new String[0]);
100            }
101    
102            @Override
103            public void invalidate() {
104                    _attributes.clear();
105            }
106    
107            @Override
108            public boolean isNew() {
109                    return _new;
110            }
111    
112            @Override
113            public void putValue(String name, Object value) {
114                    setAttribute(name, value);
115            }
116    
117            @Override
118            public void removeAttribute(String name) {
119                    _attributes.remove(name);
120            }
121    
122            @Override
123            public void removeValue(String name) {
124                    removeAttribute(name);
125            }
126    
127            @Override
128            public void setAttribute(String name, Object value) {
129                    _attributes.put(name, value);
130            }
131    
132            @Override
133            public void setMaxInactiveInterval(int maxInactiveInterval) {
134                    _maxInactiveInterval = maxInactiveInterval;
135            }
136    
137            private Map<String, Object> _attributes;
138            private long _creationTime;
139            private String _id;
140            private long _lastAccessedTime;
141            private int _maxInactiveInterval;
142            private boolean _new;
143            private ServletContext _servletContext;
144    
145    }