From 732fab12b8833ee1fe4da4229d965d5d1a785188 Mon Sep 17 00:00:00 2001 From: hns Date: Thu, 17 Mar 2005 08:32:47 +0000 Subject: [PATCH] Overworked and cleaned up session code a bit: * Made all fields in Session and SessionManager protected to allow subclassing outside the package * Renamed SessionManager.setApplication() to init() and introduce SessionManager.shutdown() * Introduce Session.commit(RequestEvaluator) which is called after a HTTP request has finished executing --- src/helma/framework/core/Application.java | 6 +- .../framework/core/RequestEvaluator.java | 1 + src/helma/framework/core/Session.java | 76 ++++++++++++------- src/helma/framework/core/SessionBean.java | 6 +- src/helma/framework/core/SessionManager.java | 10 ++- 5 files changed, 61 insertions(+), 38 deletions(-) diff --git a/src/helma/framework/core/Application.java b/src/helma/framework/core/Application.java index 6852f728..d2e194ab 100644 --- a/src/helma/framework/core/Application.java +++ b/src/helma/framework/core/Application.java @@ -276,7 +276,7 @@ public final class Application implements IPathElement, Runnable { String sessionMgrImpl = props.getProperty("sessionManagerImpl", "helma.framework.core.SessionManager"); sessionMgr = (SessionManager) Class.forName(sessionMgrImpl).newInstance(); - sessionMgr.setApplication(this); + sessionMgr.init(this); // read the sessions if wanted if ("true".equalsIgnoreCase(getProperty("persistentSessions"))) { @@ -428,7 +428,7 @@ public final class Application implements IPathElement, Runnable { if ("true".equalsIgnoreCase(getProperty("persistentSessions"))) { sessionMgr.storeSessionData(null); } - + sessionMgr.shutdown(); } public synchronized boolean isRunning() { @@ -1392,7 +1392,7 @@ public final class Application implements IPathElement, Runnable { if (userhandle != null) { try { - Object[] param = { session.getSessionID() }; + Object[] param = { session.getSessionId() }; thisEvaluator.invokeInternal(userhandle, "onLogout", param); } catch (Exception ignore) { diff --git a/src/helma/framework/core/RequestEvaluator.java b/src/helma/framework/core/RequestEvaluator.java index 8f4944db..b3de55fe 100644 --- a/src/helma/framework/core/RequestEvaluator.java +++ b/src/helma/framework/core/RequestEvaluator.java @@ -684,6 +684,7 @@ public final class RequestEvaluator implements Runnable { res.writeErrorReport(app.getName(), "Request timed out"); } + session.commit(this); return res; } diff --git a/src/helma/framework/core/Session.java b/src/helma/framework/core/Session.java index 9f005a4d..ad75e154 100644 --- a/src/helma/framework/core/Session.java +++ b/src/helma/framework/core/Session.java @@ -23,39 +23,39 @@ import java.util.*; /** * This represents a session currently using the Hop application. - * This comprends anybody who happens to surf the site. - * Depending on whether the user is logged in or not, the user object holds a + * This includes anybody who happens to request a page from this application. + * Depending on whether the user is logged in or not, the session holds a * persistent user node. */ public class Session implements Serializable { - transient Application app; - String sessionID; + + transient protected Application app; + protected String sessionId; // the unique id (login name) for the user, if logged in - String uid; + protected String uid; // the handle to this user's persistent db node, if logged in - NodeHandle userHandle; + protected NodeHandle userHandle; // the transient cache node that is exposed to javascript // this stays the same across logins and logouts. - public TransientNode cacheNode; - long onSince; - long lastTouched; - long lastModified; + protected TransientNode cacheNode; + protected long onSince; + protected long lastTouched; + protected long lastModified; - // used to remember messages to the user between requests - - // used for redirects. - String message; + // used to remember messages to the user between requests, mainly between redirects. + protected String message; /** * Creates a new Session object. * - * @param sessionID ... + * @param sessionId ... * @param app ... */ - public Session(String sessionID, Application app) { - this.sessionID = sessionID; + public Session(String sessionId, Application app) { + this.sessionId = sessionId; this.app = app; this.uid = null; this.userHandle = null; @@ -65,7 +65,7 @@ public class Session implements Serializable { } /** - * attach the given user node to this session. + * Attach the given user node to this session. */ public void login(INode usernode) { if (usernode == null) { @@ -91,7 +91,7 @@ public class Session implements Serializable { } /** - * remove this sessions's user node. + * Remove this sessions's user node. */ public void logout() { userHandle = null; @@ -100,7 +100,7 @@ public class Session implements Serializable { } /** - * + * Returns true if this session is currently associated with a user object. * * @return ... */ @@ -112,6 +112,13 @@ public class Session implements Serializable { } } + /** + * Get the Node handle for the current user, if logged in. + */ + public NodeHandle getUserHandle() { + return userHandle; + } + /** * Gets the user Node from this Application's NodeManager. */ @@ -131,7 +138,7 @@ public class Session implements Serializable { } /** - * + * Get this session's application * * @return ... */ @@ -140,7 +147,7 @@ public class Session implements Serializable { } /** - * + * Set this session's application * * @param app ... */ @@ -149,23 +156,33 @@ public class Session implements Serializable { } /** - * + * Return this session's id. * * @return ... */ - public String getSessionID() { - return sessionID; + public String getSessionId() { + return sessionId; } /** - * + * Called at the beginning of a request to let the session know it's + * being used. */ public void touch() { lastTouched = System.currentTimeMillis(); } /** + * Called after a request has been handled. * + * @param reval the request evaluator that handled the request + */ + public void commit(RequestEvaluator reval) { + lastModified = Math.max(lastModified, cacheNode.lastModified()); + } + + /** + * Returns the time this session was last touched. * * @return ... */ @@ -174,7 +191,8 @@ public class Session implements Serializable { } /** - * + * Returns the time this session was last modified, meaning the last time + * its user status changed or its cache node was modified. * * @return ... */ @@ -183,7 +201,7 @@ public class Session implements Serializable { } /** - * + * Set the last modified time on this session. * * @param date ... */ @@ -194,7 +212,7 @@ public class Session implements Serializable { } /** - * + * Return the time this session was created. * * @return ... */ @@ -203,7 +221,7 @@ public class Session implements Serializable { } /** - * + * Return a string representation for this session. * * @return ... */ diff --git a/src/helma/framework/core/SessionBean.java b/src/helma/framework/core/SessionBean.java index 5bbe11b9..e4918865 100644 --- a/src/helma/framework/core/SessionBean.java +++ b/src/helma/framework/core/SessionBean.java @@ -75,7 +75,7 @@ public class SessionBean implements Serializable { * Disassociate this session from any user object it may have been associated with. */ public void logout() { - session.getApp().logoutSession(session); + session.logout(); } /** @@ -132,7 +132,7 @@ public class SessionBean implements Serializable { * @return ... */ public String get_id() { - return session.getSessionID(); + return session.getSessionId(); } /** @@ -141,7 +141,7 @@ public class SessionBean implements Serializable { * @return ... */ public String getCookie() { - return session.getSessionID(); + return session.getSessionId(); } /** diff --git a/src/helma/framework/core/SessionManager.java b/src/helma/framework/core/SessionManager.java index a9af1a23..4a51bb7b 100644 --- a/src/helma/framework/core/SessionManager.java +++ b/src/helma/framework/core/SessionManager.java @@ -31,10 +31,14 @@ public class SessionManager { sessions = new Hashtable(); } - public void setApplication(Application app) { + public void init(Application app) { this.app = app; } + public void shutdown() { + sessions.clear(); + } + public Session createSession(String sessionId) { Session session = getSession(sessionId); @@ -74,7 +78,7 @@ public class SessionManager { */ public void discardSession(Session session) { logoutSession(session); - sessions.remove(session.getSessionID()); + sessions.remove(session.getSessionId()); } /** @@ -237,7 +241,7 @@ public class SessionManager { if ((now - session.lastTouched()) < (sessionTimeout * 60000)) { session.setApp(app); - newSessions.put(session.getSessionID(), session); + newSessions.put(session.getSessionId(), session); } ct++;