From 900a251d255c387e7bba3d18c98050a37b852bae Mon Sep 17 00:00:00 2001 From: hns Date: Wed, 9 Sep 2009 23:27:04 +0000 Subject: [PATCH] Redesign session management to only register sessions with the session manager that have been changed. --- .../framework/core/RequestEvaluator.java | 2 +- src/helma/framework/core/Session.java | 60 +++++++++++++++---- src/helma/framework/core/SessionBean.java | 4 +- src/helma/framework/core/SessionManager.java | 27 ++------- 4 files changed, 58 insertions(+), 35 deletions(-) diff --git a/src/helma/framework/core/RequestEvaluator.java b/src/helma/framework/core/RequestEvaluator.java index 5e725a8e..e0cc8ad1 100644 --- a/src/helma/framework/core/RequestEvaluator.java +++ b/src/helma/framework/core/RequestEvaluator.java @@ -784,7 +784,7 @@ public final class RequestEvaluator implements Runnable { res.reportError("Request timed out"); } - session.commit(this); + session.commit(this, app.sessionMgr); return res; } diff --git a/src/helma/framework/core/Session.java b/src/helma/framework/core/Session.java index 8bb84de4..c3399d7f 100644 --- a/src/helma/framework/core/Session.java +++ b/src/helma/framework/core/Session.java @@ -46,9 +46,12 @@ public class Session implements Serializable { // the transient cache node that is exposed to javascript // this stays the same across logins and logouts. protected INode cacheNode; + + // timestamps for creation, last request, last modification protected long onSince; protected long lastTouched; protected long lastModified; + protected long cacheLastModified; // used to remember messages to the user between requests, mainly between redirects. protected String message; @@ -56,6 +59,9 @@ public class Session implements Serializable { protected HashMap uploads = null; + protected transient boolean modifiedInRequest = false; + protected transient boolean registered = false; + /** * Creates a new Session object. * @@ -68,7 +74,10 @@ public class Session implements Serializable { this.uid = null; this.userHandle = null; 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; } @@ -85,17 +94,22 @@ public class Session implements Serializable { } lastModified = System.currentTimeMillis(); + modifiedInRequest = true; } /** * Try logging in this session given the userName and password. * - * @param userName - * @param password + * @param userName the user name + * @param password the password * @return true if session was logged in. */ 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; uid = null; lastModified = System.currentTimeMillis(); + modifiedInRequest = true; + } } } @@ -132,7 +148,7 @@ public class Session implements Serializable { * @return ... */ 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. */ public void setCacheNode(INode node) { + if (node == null) { + throw new NullPointerException("cache node is null"); + } 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 */ - public void commit(RequestEvaluator reval) { - // nothing to do + public void commit(RequestEvaluator reval, SessionManager smgr) { + 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. * - * @param date ... + * @param l the timestamp */ - public void setLastModified(Date date) { - if (date != null) { - lastModified = date.getTime(); - } + public void setLastModified(long l) { + lastModified = l; } /** @@ -278,6 +303,13 @@ public class Session implements Serializable { 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. @@ -290,6 +322,7 @@ public class Session implements Serializable { res.setDebugBuffer(debugBuffer); message = null; debugBuffer = null; + modifiedInRequest = true; } } @@ -301,6 +334,9 @@ public class Session implements Serializable { public synchronized void storeResponseMessages(ResponseTrans res) { message = res.getMessage(); debugBuffer = res.getDebugBuffer(); + if (message != null || debugBuffer != null) { + modifiedInRequest = true; + } } /** diff --git a/src/helma/framework/core/SessionBean.java b/src/helma/framework/core/SessionBean.java index 387388b6..ba073b69 100644 --- a/src/helma/framework/core/SessionBean.java +++ b/src/helma/framework/core/SessionBean.java @@ -181,7 +181,9 @@ public class SessionBean implements Serializable { * @param date ... */ public void setLastModified(Date date) { - session.setLastModified(date); + if (date != null) { + session.setLastModified(date.getTime()); + } } /** diff --git a/src/helma/framework/core/SessionManager.java b/src/helma/framework/core/SessionManager.java index f0b281fb..3f7a2893 100644 --- a/src/helma/framework/core/SessionManager.java +++ b/src/helma/framework/core/SessionManager.java @@ -43,22 +43,23 @@ public class SessionManager { public Session createSession(String sessionId) { Session session = getSession(sessionId); - if (session == null) { session = new Session(sessionId, app); - sessions.put(sessionId, session); } - return session; } public Session getSession(String sessionId) { - if (sessionId == null) + if (sessionId == null) { return null; - + } 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 * actual changes from the table itself, which is managed by the application. @@ -83,22 +84,6 @@ public class SessionManager { 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 SessionBean objects currently associated with a given