diff --git a/jetty-nosql/src/main/java/org/eclipse/jetty/nosql/NoSqlSession.java b/jetty-nosql/src/main/java/org/eclipse/jetty/nosql/NoSqlSession.java index 1a1459a45a0..8b732d4d2b3 100644 --- a/jetty-nosql/src/main/java/org/eclipse/jetty/nosql/NoSqlSession.java +++ b/jetty-nosql/src/main/java/org/eclipse/jetty/nosql/NoSqlSession.java @@ -24,13 +24,13 @@ import java.util.concurrent.atomic.AtomicInteger; import javax.servlet.http.HttpServletRequest; -import org.eclipse.jetty.server.session.AbstractSession; +import org.eclipse.jetty.server.session.MemSession; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; /* ------------------------------------------------------------ */ -public class NoSqlSession extends AbstractSession +public class NoSqlSession extends MemSession { private final static Logger __log = Log.getLogger("org.eclipse.jetty.server.session"); diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/session/AbstractSession.java b/jetty-server/src/main/java/org/eclipse/jetty/server/session/AbstractSession.java index cb1479f54a0..8f9f99f3d50 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/session/AbstractSession.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/session/AbstractSession.java @@ -18,13 +18,7 @@ package org.eclipse.jetty.server.session; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Enumeration; -import java.util.HashMap; -import java.util.HashSet; import java.util.Iterator; -import java.util.List; import java.util.Map; import java.util.Set; @@ -54,7 +48,6 @@ public abstract class AbstractSession implements AbstractSessionManager.SessionI private String _clusterId; // ID without any node (ie "worker") id appended private String _nodeId; // ID of session with node(ie "worker") id appended private final AbstractSessionManager _manager; - private final Map _attributes=new HashMap(); private boolean _idChanged; private final long _created; private long _cookieSet; @@ -139,53 +132,21 @@ public abstract class AbstractSession implements AbstractSessionManager.SessionI } /* ------------------------------------------------------------- */ - public Map getAttributeMap() - { - return _attributes; - } + public abstract Map getAttributeMap(); + + + + /* ------------------------------------------------------------ */ - @Override - public Object getAttribute(String name) - { - synchronized (this) - { - checkValid(); - return _attributes.get(name); - } - } + public abstract int getAttributes(); + + + /* ------------------------------------------------------------ */ - public int getAttributes() - { - synchronized (this) - { - checkValid(); - return _attributes.size(); - } - } - - /* ------------------------------------------------------------ */ - @SuppressWarnings({ "unchecked" }) - @Override - public Enumeration getAttributeNames() - { - synchronized (this) - { - checkValid(); - List names=_attributes==null?Collections.EMPTY_LIST:new ArrayList(_attributes.keySet()); - return Collections.enumeration(names); - } - } - - /* ------------------------------------------------------------ */ - public Set getNames() - { - synchronized (this) - { - return new HashSet(_attributes.keySet()); - } - } + public abstract Set getNames(); + /* ------------------------------------------------------------- */ public long getCookieSetTime() @@ -272,25 +233,7 @@ public abstract class AbstractSession implements AbstractSessionManager.SessionI return getAttribute(name); } - /* ------------------------------------------------------------- */ - /** - * @deprecated As of Version 2.2, this method is replaced by - * {@link #getAttributeNames} - */ - @Deprecated - @Override - public String[] getValueNames() throws IllegalStateException - { - synchronized(this) - { - checkValid(); - if (_attributes==null) - return new String[0]; - String[] a=new String[_attributes.size()]; - return (String[])_attributes.keySet().toArray(a); - } - } - + /* ------------------------------------------------------------ */ public void renewId(HttpServletRequest request) @@ -403,34 +346,8 @@ public abstract class AbstractSession implements AbstractSessionManager.SessionI } /* ------------------------------------------------------------- */ - public void clearAttributes() - { - while (_attributes!=null && _attributes.size()>0) - { - ArrayList keys; - synchronized(this) - { - keys=new ArrayList(_attributes.keySet()); - } - - Iterator iter=keys.iterator(); - while (iter.hasNext()) - { - String key=(String)iter.next(); - - Object value; - synchronized(this) - { - value=doPutOrRemove(key,null); - } - unbindValue(key,value); - - _manager.doSessionAttributeListeners(this,key,value,null); - } - } - if (_attributes!=null) - _attributes.clear(); - } + public abstract void clearAttributes(); + /* ------------------------------------------------------------- */ public boolean isIdChanged() @@ -478,16 +395,12 @@ public abstract class AbstractSession implements AbstractSessionManager.SessionI } /* ------------------------------------------------------------ */ - protected Object doPutOrRemove(String name, Object value) - { - return value==null?_attributes.remove(name):_attributes.put(name,value); - } + public abstract Object doPutOrRemove(String name, Object value); + /* ------------------------------------------------------------ */ - protected Object doGet(String name) - { - return _attributes.get(name); - } + public abstract Object doGet(String name); + /* ------------------------------------------------------------ */ @Override @@ -571,12 +484,7 @@ public abstract class AbstractSession implements AbstractSessionManager.SessionI _manager.doSessionAttributeListeners(this,name,oldValue,newValue); } } - - /* ------------------------------------------------------------ */ - protected void addAttributes(Map map) - { - _attributes.putAll(map); - } + /* ------------------------------------------------------------- */ public void setIdChanged(boolean changed) @@ -653,7 +561,7 @@ public abstract class AbstractSession implements AbstractSessionManager.SessionI synchronized(this) { HttpSessionEvent event = new HttpSessionEvent(this); - for (Iterator iter = _attributes.values().iterator(); iter.hasNext();) + for (Iterator iter = getAttributeMap().values().iterator(); iter.hasNext();) { Object value = iter.next(); if (value instanceof HttpSessionActivationListener) @@ -671,7 +579,7 @@ public abstract class AbstractSession implements AbstractSessionManager.SessionI synchronized(this) { HttpSessionEvent event = new HttpSessionEvent(this); - for (Iterator iter = _attributes.values().iterator(); iter.hasNext();) + for (Iterator iter = getAttributeMap().values().iterator(); iter.hasNext();) { Object value = iter.next(); if (value instanceof HttpSessionActivationListener) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/session/HashedSession.java b/jetty-server/src/main/java/org/eclipse/jetty/server/session/HashedSession.java index dc599cc5dc2..d36e4277e49 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/session/HashedSession.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/session/HashedSession.java @@ -34,7 +34,7 @@ import org.eclipse.jetty.util.IO; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; -public class HashedSession extends AbstractSession +public class HashedSession extends MemSession { private static final Logger LOG = Log.getLogger(HashedSession.class); diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/session/JDBCSessionManager.java b/jetty-server/src/main/java/org/eclipse/jetty/server/session/JDBCSessionManager.java index 4b6804669b8..dbe95335dae 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/session/JDBCSessionManager.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/session/JDBCSessionManager.java @@ -88,7 +88,7 @@ public class JDBCSessionManager extends AbstractSessionManager * * Session instance. */ - public class Session extends AbstractSession + public class Session extends MemSession { private static final long serialVersionUID = 5208464051134226143L; diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/session/MemSession.java b/jetty-server/src/main/java/org/eclipse/jetty/server/session/MemSession.java new file mode 100644 index 00000000000..32608ddc56f --- /dev/null +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/session/MemSession.java @@ -0,0 +1,171 @@ +// +// ======================================================================== +// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.server.session; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.servlet.http.HttpServletRequest; + + +/** + * MemSession + * + * A session whose data is kept in memory + */ +public class MemSession extends AbstractSession +{ + + private final Map _attributes=new HashMap(); + + protected MemSession(AbstractSessionManager abstractSessionManager, HttpServletRequest request) + { + super(abstractSessionManager, request); + } + + public MemSession(AbstractSessionManager abstractSessionManager, long created, long accessed, String clusterId) + { + super(abstractSessionManager, created, accessed, clusterId); + } + + + /* ------------------------------------------------------------- */ + public Map getAttributeMap() + { + return _attributes; + } + + /* ------------------------------------------------------------ */ + @Override + public Object getAttribute(String name) + { + synchronized (this) + { + checkValid(); + return _attributes.get(name); + } + } + + /* ------------------------------------------------------------ */ + public int getAttributes() + { + synchronized (this) + { + checkValid(); + return _attributes.size(); + } + } + + + /* ------------------------------------------------------------ */ + @SuppressWarnings({ "unchecked" }) + @Override + public Enumeration getAttributeNames() + { + synchronized (this) + { + checkValid(); + List names=_attributes==null?Collections.EMPTY_LIST:new ArrayList(_attributes.keySet()); + return Collections.enumeration(names); + } + } + + /* ------------------------------------------------------------ */ + public Set getNames() + { + synchronized (this) + { + return new HashSet(_attributes.keySet()); + } + } + /* ------------------------------------------------------------- */ + /** + * @deprecated As of Version 2.2, this method is replaced by + * {@link #getAttributeNames} + */ + @Deprecated + @Override + public String[] getValueNames() throws IllegalStateException + { + synchronized(this) + { + checkValid(); + if (_attributes==null) + return new String[0]; + String[] a=new String[_attributes.size()]; + return (String[])_attributes.keySet().toArray(a); + } + } + + /* ------------------------------------------------------------- */ + public void clearAttributes() + { + while (_attributes!=null && _attributes.size()>0) + { + ArrayList keys; + synchronized(this) + { + keys=new ArrayList(_attributes.keySet()); + } + + Iterator iter=keys.iterator(); + while (iter.hasNext()) + { + String key=(String)iter.next(); + + Object value; + synchronized(this) + { + value=doPutOrRemove(key,null); + } + unbindValue(key,value); + + ((AbstractSessionManager)getSessionManager()).doSessionAttributeListeners(this,key,value,null); + } + } + if (_attributes!=null) + _attributes.clear(); + } + + /* ------------------------------------------------------------ */ + public void addAttributes(Map map) + { + _attributes.putAll(map); + } + + /* ------------------------------------------------------------ */ + public Object doPutOrRemove(String name, Object value) + { + return value==null?_attributes.remove(name):_attributes.put(name,value); + } + + /* ------------------------------------------------------------ */ + public Object doGet(String name) + { + return _attributes.get(name); + } + +} diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/session/SessionCookieTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/session/SessionCookieTest.java index 94e92ead7ee..ff8a0ed43ff 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/session/SessionCookieTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/session/SessionCookieTest.java @@ -21,6 +21,10 @@ package org.eclipse.jetty.server.session; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import java.util.Enumeration; +import java.util.Map; +import java.util.Set; + import javax.servlet.SessionCookieConfig; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; @@ -50,6 +54,87 @@ public class SessionCookieTest super(abstractSessionManager, created, accessed, clusterId); } + /** + * @see javax.servlet.http.HttpSession#getAttribute(java.lang.String) + */ + @Override + public Object getAttribute(String name) + { + return null; + } + + /** + * @see javax.servlet.http.HttpSession#getAttributeNames() + */ + @Override + public Enumeration getAttributeNames() + { + return null; + } + + /** + * @see javax.servlet.http.HttpSession#getValueNames() + */ + @Override + public String[] getValueNames() + { + return null; + } + + /** + * @see org.eclipse.jetty.server.session.AbstractSession#getAttributeMap() + */ + @Override + public Map getAttributeMap() + { + return null; + } + + /** + * @see org.eclipse.jetty.server.session.AbstractSession#getAttributes() + */ + @Override + public int getAttributes() + { + return 0; + } + + /** + * @see org.eclipse.jetty.server.session.AbstractSession#getNames() + */ + @Override + public Set getNames() + { + return null; + } + + /** + * @see org.eclipse.jetty.server.session.AbstractSession#clearAttributes() + */ + @Override + public void clearAttributes() + { + + } + + /** + * @see org.eclipse.jetty.server.session.AbstractSession#doPutOrRemove(java.lang.String, java.lang.Object) + */ + @Override + public Object doPutOrRemove(String name, Object value) + { + return null; + } + + /** + * @see org.eclipse.jetty.server.session.AbstractSession#doGet(java.lang.String) + */ + @Override + public Object doGet(String name) + { + return null; + } + } public class MockSessionIdManager extends AbstractSessionIdManager