Skip to content

Commit

Permalink
Make jdbc session tests work
Browse files Browse the repository at this point in the history
  • Loading branch information
janbartel committed Nov 11, 2015
1 parent 84239bc commit b0748c5
Show file tree
Hide file tree
Showing 31 changed files with 606 additions and 305 deletions.
Expand Up @@ -33,8 +33,8 @@

public abstract class AbstractSessionIdManager extends AbstractLifeCycle implements SessionIdManager
{
private static final Logger LOG = Log.getLogger(AbstractSessionIdManager.class);

private final static Logger LOG = Log.getLogger("org.eclipse.jetty.server.session");
private final static String __NEW_SESSION_ID="org.eclipse.jetty.server.newSessionId";

protected Random _random;
Expand Down Expand Up @@ -243,7 +243,6 @@ protected void doStart() throws Exception
}

_scavenger.start();
System.err.println("Started scavenger "+_scavenger);
}

/* ------------------------------------------------------------ */
Expand Down Expand Up @@ -345,6 +344,7 @@ public void invalidateAll (String id)
{
//take the id out of the list of known sessionids for this node
removeId(id);

//tell all contexts that may have a session object with this id to
//get rid of them
Handler[] contexts = _server.getChildHandlersByClass(ContextHandler.class);
Expand Down
Expand Up @@ -20,10 +20,14 @@
package org.eclipse.jetty.server.session;

import java.util.Collections;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.eclipse.jetty.util.component.AbstractLifeCycle;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.statistic.CounterStatistic;

/**
* AbstractSessionStore
Expand All @@ -32,26 +36,75 @@
*/
public abstract class AbstractSessionStore extends AbstractLifeCycle implements SessionStore
{
final static Logger LOG = Log.getLogger("org.eclipse.jetty.server.session");

protected SessionDataStore _sessionDataStore;
protected StalenessStrategy _staleStrategy;
protected SessionManager _manager;

protected final CounterStatistic _sessionStats = new CounterStatistic();



/**
* Create a new Session object from session data
* @param data
* @return
*/
public abstract Session newSession (SessionData data);



/**
* Get the session matching the key
* @param key
* @return
*/
public abstract Session doGet(SessionKey key);

public abstract void doPut (SessionKey key, Session session);


/**
* Put the session into the map if it wasn't already there
*
* @param key the identity of the session
* @param session the session object
* @return null if the session wasn't already in the map, or the existing entry otherwise
*/
public abstract Session doPutIfAbsent (SessionKey key, Session session);



/**
* Check to see if the session exists in the store
* @param key
* @return
*/
public abstract boolean doExists (SessionKey key);

public abstract void doDelete (SessionKey key);


/**
* Remove the session with this identity from the store
* @param key
* @return the removed Session or null if no such key
*/
public abstract Session doDelete (SessionKey key);




/**
* Get a list of keys for sessions that the store thinks has expired
* @return
*/
public abstract Set<SessionKey> doGetExpiredCandidates();




/**
*
*/
public AbstractSessionStore ()
{
}
Expand Down Expand Up @@ -126,11 +179,21 @@ public void setStaleStrategy(StalenessStrategy staleStrategy)
* @see org.eclipse.jetty.server.session.SessionStore#get(java.lang.String)
*/
@Override
public Session get(SessionKey key) throws Exception
public Session get(SessionKey key, boolean staleCheck) throws Exception
{
//look locally
Session session = doGet(key);


if (staleCheck && isStale(session))
{
//delete from memory so should reload
doDelete(key);
session = null;
_sessionStats.decrement();
}


//not in session store, load the data for the session if possible
if (session == null && _sessionDataStore != null)
{
Expand All @@ -139,7 +202,15 @@ public Session get(SessionKey key) throws Exception
{
session = newSession(data);
session.setSessionManager(_manager);
doPut(key, session);
Session existing = doPutIfAbsent(key, session);
if (existing != null)
{
//some other thread has got in first and added the session
//so use it
session = existing;
}
else
_sessionStats.increment();
}
}
return session;
Expand All @@ -159,30 +230,29 @@ public void put(SessionKey key, Session session) throws Exception
throw new IllegalArgumentException ("Put key="+key+" session="+(session==null?"null":session.getId()));

session.setSessionManager(_manager);
//if the session is already in our cache, then we want to write through any changes
if (doExists(key))

Session existing = doPutIfAbsent(key,session);
if (existing == null)
{
//if the session data has changed, or the cache is considered stale, write it to any backing store
if ((session.getSessionData().isDirty() || isStale(session)) && _sessionDataStore != null)
//session not already in cache write through
if (_sessionDataStore != null)
{
session.willPassivate();
_sessionDataStore.store(key, session.getSessionData());
_sessionDataStore.store(SessionKey.getKey(session.getSessionData()), session.getSessionData());
session.didActivate();
}
_sessionStats.increment();
}
else
{
//session not already in cache, add it and write through
if (_sessionDataStore != null)
//if the session data has changed, or the cache is considered stale, write it to any backing store
if ((session.getSessionData().isDirty() || isStale(session)) && _sessionDataStore != null)
{
session.willPassivate();
_sessionDataStore.store(SessionKey.getKey(session.getSessionData()), session.getSessionData());
_sessionDataStore.store(key, session.getSessionData());
session.didActivate();
}
doPut(key,session);
}

}

/**
Expand All @@ -207,11 +277,17 @@ public boolean exists(SessionKey key)
@Override
public boolean delete(SessionKey key) throws Exception
{
boolean deleted = true;
if (_sessionDataStore != null)
deleted = _sessionDataStore.delete(key);
doDelete(key);
return deleted;
{
boolean dsdel = _sessionDataStore.delete(key);
if (LOG.isDebugEnabled()) LOG.debug("Session {} deleted in db {}",key, dsdel);
}
if (doDelete(key) != null)
{
_sessionStats.decrement();
return true;
}
return false;
}

public boolean isStale (Session session)
Expand All @@ -235,4 +311,46 @@ public Set<SessionKey> getExpired()
return _sessionDataStore.getExpired(candidates);
}





@Override
public Session newSession(HttpServletRequest request, SessionKey key, long time, long maxInactiveMs)
{
return null;
}



@Override
public int getSessions()
{
return (int)_sessionStats.getCurrent();
}



@Override
public int getSessionsMax()
{
return (int)_sessionStats.getMax();
}



@Override
public int getSessionsTotal()
{
return (int)_sessionStats.getTotal();
}



@Override
public void resetStats()
{
_sessionStats.reset();
}

}
Expand Up @@ -41,7 +41,7 @@
/**
* FileSessionDataStore
*
*
* A file-based store of session data.
*/
public class FileSessionDataStore extends AbstractSessionDataStore
{
Expand Down
Expand Up @@ -50,7 +50,6 @@ public boolean isIdInUse(String id)
public String newSessionId(long seedTerm)
{
String id = super.newSessionId(seedTerm);
useId(id);
return id;
}

Expand Down

0 comments on commit b0748c5

Please sign in to comment.