436345 Refactor AbstractSession to ease customization

This commit is contained in:
Jan Bartel 2014-06-02 11:48:01 +02:00
parent c6128db48b
commit 1b00888739
6 changed files with 281 additions and 117 deletions

View File

@ -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");

View File

@ -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<String,Object> _attributes=new HashMap<String, Object>();
private boolean _idChanged;
private final long _created;
private long _cookieSet;
@ -139,53 +132,21 @@ public abstract class AbstractSession implements AbstractSessionManager.SessionI
}
/* ------------------------------------------------------------- */
public Map<String,Object> getAttributeMap()
{
return _attributes;
}
public abstract Map<String,Object> 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<String> getAttributeNames()
{
synchronized (this)
{
checkValid();
List<String> names=_attributes==null?Collections.EMPTY_LIST:new ArrayList<String>(_attributes.keySet());
return Collections.enumeration(names);
}
}
/* ------------------------------------------------------------ */
public Set<String> getNames()
{
synchronized (this)
{
return new HashSet<String>(_attributes.keySet());
}
}
public abstract Set<String> 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<String> keys;
synchronized(this)
{
keys=new ArrayList<String>(_attributes.keySet());
}
Iterator<String> 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<String,Object> 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<Object> iter = _attributes.values().iterator(); iter.hasNext();)
for (Iterator<Object> 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<Object> iter = _attributes.values().iterator(); iter.hasNext();)
for (Iterator<Object> iter = getAttributeMap().values().iterator(); iter.hasNext();)
{
Object value = iter.next();
if (value instanceof HttpSessionActivationListener)

View File

@ -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);

View File

@ -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;

View File

@ -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<String,Object> _attributes=new HashMap<String, Object>();
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<String,Object> 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<String> getAttributeNames()
{
synchronized (this)
{
checkValid();
List<String> names=_attributes==null?Collections.EMPTY_LIST:new ArrayList<String>(_attributes.keySet());
return Collections.enumeration(names);
}
}
/* ------------------------------------------------------------ */
public Set<String> getNames()
{
synchronized (this)
{
return new HashSet<String>(_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<String> keys;
synchronized(this)
{
keys=new ArrayList<String>(_attributes.keySet());
}
Iterator<String> 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<String,Object> 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);
}
}

View File

@ -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<String> 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<String, Object> 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<String> 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