Redesign session management to only register sessions with the session manager that have been changed.

This commit is contained in:
hns 2009-09-09 23:27:04 +00:00
parent 244a7529cb
commit 900a251d25
4 changed files with 58 additions and 35 deletions

View file

@ -784,7 +784,7 @@ public final class RequestEvaluator implements Runnable {
res.reportError("Request timed out"); res.reportError("Request timed out");
} }
session.commit(this); session.commit(this, app.sessionMgr);
return res; return res;
} }

View file

@ -46,9 +46,12 @@ public class Session implements Serializable {
// the transient cache node that is exposed to javascript // the transient cache node that is exposed to javascript
// this stays the same across logins and logouts. // this stays the same across logins and logouts.
protected INode cacheNode; protected INode cacheNode;
// timestamps for creation, last request, last modification
protected long onSince; protected long onSince;
protected long lastTouched; protected long lastTouched;
protected long lastModified; protected long lastModified;
protected long cacheLastModified;
// used to remember messages to the user between requests, mainly between redirects. // used to remember messages to the user between requests, mainly between redirects.
protected String message; protected String message;
@ -56,6 +59,9 @@ public class Session implements Serializable {
protected HashMap uploads = null; protected HashMap uploads = null;
protected transient boolean modifiedInRequest = false;
protected transient boolean registered = false;
/** /**
* Creates a new Session object. * Creates a new Session object.
* *
@ -68,7 +74,10 @@ public class Session implements Serializable {
this.uid = null; this.uid = null;
this.userHandle = null; this.userHandle = null;
cacheNode = new TransientNode("session"); cacheNode = new TransientNode("session");
onSince = System.currentTimeMillis(); cacheLastModified = cacheNode.lastModified();
// HACK - decrease timestamp by 1 to notice modifications
// taking place immediately after object creation
onSince = System.currentTimeMillis() - 1;
lastTouched = lastModified = onSince; lastTouched = lastModified = onSince;
} }
@ -85,17 +94,22 @@ public class Session implements Serializable {
} }
lastModified = System.currentTimeMillis(); lastModified = System.currentTimeMillis();
modifiedInRequest = true;
} }
/** /**
* Try logging in this session given the userName and password. * Try logging in this session given the userName and password.
* *
* @param userName * @param userName the user name
* @param password * @param password the password
* @return true if session was logged in. * @return true if session was logged in.
*/ */
public boolean login(String userName, String password) { public boolean login(String userName, String password) {
return app.loginSession(userName, password, this); if (app.loginSession(userName, password, this)) {
lastModified = System.currentTimeMillis();
modifiedInRequest = true;
}
return false;
} }
/** /**
@ -122,6 +136,8 @@ public class Session implements Serializable {
userHandle = null; userHandle = null;
uid = null; uid = null;
lastModified = System.currentTimeMillis(); lastModified = System.currentTimeMillis();
modifiedInRequest = true;
} }
} }
} }
@ -132,7 +148,7 @@ public class Session implements Serializable {
* @return ... * @return ...
*/ */
public boolean isLoggedIn() { public boolean isLoggedIn() {
return (userHandle != null) && (uid != null); return userHandle != null;
} }
/** /**
@ -164,7 +180,11 @@ public class Session implements Serializable {
* Set the cache node for this session. * Set the cache node for this session.
*/ */
public void setCacheNode(INode node) { public void setCacheNode(INode node) {
if (node == null) {
throw new NullPointerException("cache node is null");
}
this.cacheNode = node; this.cacheNode = node;
this.cacheLastModified = cacheNode.lastModified();
} }
/** /**
@ -214,8 +234,15 @@ public class Session implements Serializable {
* *
* @param reval the request evaluator that handled the request * @param reval the request evaluator that handled the request
*/ */
public void commit(RequestEvaluator reval) { public void commit(RequestEvaluator reval, SessionManager smgr) {
// nothing to do if (modifiedInRequest || cacheLastModified != cacheNode.lastModified()) {
if (!registered) {
smgr.registerSession(this);
registered = true;
}
modifiedInRequest = false;
cacheLastModified = cacheNode.lastModified();
}
} }
/** /**
@ -240,12 +267,10 @@ public class Session implements Serializable {
/** /**
* Set the last modified time on this session. * Set the last modified time on this session.
* *
* @param date ... * @param l the timestamp
*/ */
public void setLastModified(Date date) { public void setLastModified(long l) {
if (date != null) { lastModified = l;
lastModified = date.getTime();
}
} }
/** /**
@ -278,6 +303,13 @@ public class Session implements Serializable {
return uid; return uid;
} }
/**
* Set the persistent user id of a registered user.
* @param uid the user name, or null if the user is not logged in.
*/
public void setUID(String uid) {
this.uid = uid;
}
/** /**
* Set the user and debug messages over from a previous response. * Set the user and debug messages over from a previous response.
@ -290,6 +322,7 @@ public class Session implements Serializable {
res.setDebugBuffer(debugBuffer); res.setDebugBuffer(debugBuffer);
message = null; message = null;
debugBuffer = null; debugBuffer = null;
modifiedInRequest = true;
} }
} }
@ -301,6 +334,9 @@ public class Session implements Serializable {
public synchronized void storeResponseMessages(ResponseTrans res) { public synchronized void storeResponseMessages(ResponseTrans res) {
message = res.getMessage(); message = res.getMessage();
debugBuffer = res.getDebugBuffer(); debugBuffer = res.getDebugBuffer();
if (message != null || debugBuffer != null) {
modifiedInRequest = true;
}
} }
/** /**

View file

@ -181,7 +181,9 @@ public class SessionBean implements Serializable {
* @param date ... * @param date ...
*/ */
public void setLastModified(Date date) { public void setLastModified(Date date) {
session.setLastModified(date); if (date != null) {
session.setLastModified(date.getTime());
}
} }
/** /**

View file

@ -43,22 +43,23 @@ public class SessionManager {
public Session createSession(String sessionId) { public Session createSession(String sessionId) {
Session session = getSession(sessionId); Session session = getSession(sessionId);
if (session == null) { if (session == null) {
session = new Session(sessionId, app); session = new Session(sessionId, app);
sessions.put(sessionId, session);
} }
return session; return session;
} }
public Session getSession(String sessionId) { public Session getSession(String sessionId) {
if (sessionId == null) if (sessionId == null) {
return null; return null;
}
return (Session) sessions.get(sessionId); return (Session) sessions.get(sessionId);
} }
public void registerSession(Session session) {
sessions.put(session.getSessionId(), session);
}
/** /**
* Return the whole session map. We return a clone of the table to prevent * Return the whole session map. We return a clone of the table to prevent
* actual changes from the table itself, which is managed by the application. * actual changes from the table itself, which is managed by the application.
@ -83,22 +84,6 @@ public class SessionManager {
sessions.remove(session.getSessionId()); sessions.remove(session.getSessionId());
} }
/**
* Log in a user given his or her user name and password.
* @deprecated
*/
public boolean loginSession(String uname, String password, Session session) {
return app.loginSession(uname, password, session);
}
/**
* Log out a session from this application.
* @deprecated
*/
public void logoutSession(Session session) {
app.logoutSession(session);
}
/** /**
* Return an array of <code>SessionBean</code> objects currently associated with a given * Return an array of <code>SessionBean</code> objects currently associated with a given