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
This commit is contained in:
parent
b953e6e3b2
commit
732fab12b8
5 changed files with 61 additions and 38 deletions
|
@ -276,7 +276,7 @@ public final class Application implements IPathElement, Runnable {
|
||||||
String sessionMgrImpl = props.getProperty("sessionManagerImpl",
|
String sessionMgrImpl = props.getProperty("sessionManagerImpl",
|
||||||
"helma.framework.core.SessionManager");
|
"helma.framework.core.SessionManager");
|
||||||
sessionMgr = (SessionManager) Class.forName(sessionMgrImpl).newInstance();
|
sessionMgr = (SessionManager) Class.forName(sessionMgrImpl).newInstance();
|
||||||
sessionMgr.setApplication(this);
|
sessionMgr.init(this);
|
||||||
|
|
||||||
// read the sessions if wanted
|
// read the sessions if wanted
|
||||||
if ("true".equalsIgnoreCase(getProperty("persistentSessions"))) {
|
if ("true".equalsIgnoreCase(getProperty("persistentSessions"))) {
|
||||||
|
@ -428,7 +428,7 @@ public final class Application implements IPathElement, Runnable {
|
||||||
if ("true".equalsIgnoreCase(getProperty("persistentSessions"))) {
|
if ("true".equalsIgnoreCase(getProperty("persistentSessions"))) {
|
||||||
sessionMgr.storeSessionData(null);
|
sessionMgr.storeSessionData(null);
|
||||||
}
|
}
|
||||||
|
sessionMgr.shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized boolean isRunning() {
|
public synchronized boolean isRunning() {
|
||||||
|
@ -1392,7 +1392,7 @@ public final class Application implements IPathElement, Runnable {
|
||||||
|
|
||||||
if (userhandle != null) {
|
if (userhandle != null) {
|
||||||
try {
|
try {
|
||||||
Object[] param = { session.getSessionID() };
|
Object[] param = { session.getSessionId() };
|
||||||
|
|
||||||
thisEvaluator.invokeInternal(userhandle, "onLogout", param);
|
thisEvaluator.invokeInternal(userhandle, "onLogout", param);
|
||||||
} catch (Exception ignore) {
|
} catch (Exception ignore) {
|
||||||
|
|
|
@ -684,6 +684,7 @@ public final class RequestEvaluator implements Runnable {
|
||||||
res.writeErrorReport(app.getName(), "Request timed out");
|
res.writeErrorReport(app.getName(), "Request timed out");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
session.commit(this);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,39 +23,39 @@ import java.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This represents a session currently using the Hop application.
|
* This represents a session currently using the Hop application.
|
||||||
* This comprends anybody who happens to surf the site.
|
* This includes anybody who happens to request a page from this application.
|
||||||
* Depending on whether the user is logged in or not, the user object holds a
|
* Depending on whether the user is logged in or not, the session holds a
|
||||||
* persistent user node.
|
* persistent user node.
|
||||||
*/
|
*/
|
||||||
public class Session implements Serializable {
|
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
|
// 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
|
// 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
|
// 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.
|
||||||
public TransientNode cacheNode;
|
protected TransientNode cacheNode;
|
||||||
long onSince;
|
protected long onSince;
|
||||||
long lastTouched;
|
protected long lastTouched;
|
||||||
long lastModified;
|
protected long lastModified;
|
||||||
|
|
||||||
// used to remember messages to the user between requests -
|
// used to remember messages to the user between requests, mainly between redirects.
|
||||||
// used for redirects.
|
protected String message;
|
||||||
String message;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new Session object.
|
* Creates a new Session object.
|
||||||
*
|
*
|
||||||
* @param sessionID ...
|
* @param sessionId ...
|
||||||
* @param app ...
|
* @param app ...
|
||||||
*/
|
*/
|
||||||
public Session(String sessionID, Application app) {
|
public Session(String sessionId, Application app) {
|
||||||
this.sessionID = sessionID;
|
this.sessionId = sessionId;
|
||||||
this.app = app;
|
this.app = app;
|
||||||
this.uid = null;
|
this.uid = null;
|
||||||
this.userHandle = 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) {
|
public void login(INode usernode) {
|
||||||
if (usernode == null) {
|
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() {
|
public void logout() {
|
||||||
userHandle = null;
|
userHandle = null;
|
||||||
|
@ -100,7 +100,7 @@ public class Session implements Serializable {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Returns true if this session is currently associated with a user object.
|
||||||
*
|
*
|
||||||
* @return ...
|
* @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.
|
* Gets the user Node from this Application's NodeManager.
|
||||||
*/
|
*/
|
||||||
|
@ -131,7 +138,7 @@ public class Session implements Serializable {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Get this session's application
|
||||||
*
|
*
|
||||||
* @return ...
|
* @return ...
|
||||||
*/
|
*/
|
||||||
|
@ -140,7 +147,7 @@ public class Session implements Serializable {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Set this session's application
|
||||||
*
|
*
|
||||||
* @param app ...
|
* @param app ...
|
||||||
*/
|
*/
|
||||||
|
@ -149,23 +156,33 @@ public class Session implements Serializable {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Return this session's id.
|
||||||
*
|
*
|
||||||
* @return ...
|
* @return ...
|
||||||
*/
|
*/
|
||||||
public String getSessionID() {
|
public String getSessionId() {
|
||||||
return sessionID;
|
return sessionId;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Called at the beginning of a request to let the session know it's
|
||||||
|
* being used.
|
||||||
*/
|
*/
|
||||||
public void touch() {
|
public void touch() {
|
||||||
lastTouched = System.currentTimeMillis();
|
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 ...
|
* @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 ...
|
* @return ...
|
||||||
*/
|
*/
|
||||||
|
@ -183,7 +201,7 @@ public class Session implements Serializable {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Set the last modified time on this session.
|
||||||
*
|
*
|
||||||
* @param date ...
|
* @param date ...
|
||||||
*/
|
*/
|
||||||
|
@ -194,7 +212,7 @@ public class Session implements Serializable {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Return the time this session was created.
|
||||||
*
|
*
|
||||||
* @return ...
|
* @return ...
|
||||||
*/
|
*/
|
||||||
|
@ -203,7 +221,7 @@ public class Session implements Serializable {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Return a string representation for this session.
|
||||||
*
|
*
|
||||||
* @return ...
|
* @return ...
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -75,7 +75,7 @@ public class SessionBean implements Serializable {
|
||||||
* Disassociate this session from any user object it may have been associated with.
|
* Disassociate this session from any user object it may have been associated with.
|
||||||
*/
|
*/
|
||||||
public void logout() {
|
public void logout() {
|
||||||
session.getApp().logoutSession(session);
|
session.logout();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -132,7 +132,7 @@ public class SessionBean implements Serializable {
|
||||||
* @return ...
|
* @return ...
|
||||||
*/
|
*/
|
||||||
public String get_id() {
|
public String get_id() {
|
||||||
return session.getSessionID();
|
return session.getSessionId();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -141,7 +141,7 @@ public class SessionBean implements Serializable {
|
||||||
* @return ...
|
* @return ...
|
||||||
*/
|
*/
|
||||||
public String getCookie() {
|
public String getCookie() {
|
||||||
return session.getSessionID();
|
return session.getSessionId();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -31,10 +31,14 @@ public class SessionManager {
|
||||||
sessions = new Hashtable();
|
sessions = new Hashtable();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setApplication(Application app) {
|
public void init(Application app) {
|
||||||
this.app = app;
|
this.app = app;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void shutdown() {
|
||||||
|
sessions.clear();
|
||||||
|
}
|
||||||
|
|
||||||
public Session createSession(String sessionId) {
|
public Session createSession(String sessionId) {
|
||||||
Session session = getSession(sessionId);
|
Session session = getSession(sessionId);
|
||||||
|
|
||||||
|
@ -74,7 +78,7 @@ public class SessionManager {
|
||||||
*/
|
*/
|
||||||
public void discardSession(Session session) {
|
public void discardSession(Session session) {
|
||||||
logoutSession(session);
|
logoutSession(session);
|
||||||
sessions.remove(session.getSessionID());
|
sessions.remove(session.getSessionId());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -237,7 +241,7 @@ public class SessionManager {
|
||||||
|
|
||||||
if ((now - session.lastTouched()) < (sessionTimeout * 60000)) {
|
if ((now - session.lastTouched()) < (sessionTimeout * 60000)) {
|
||||||
session.setApp(app);
|
session.setApp(app);
|
||||||
newSessions.put(session.getSessionID(), session);
|
newSessions.put(session.getSessionId(), session);
|
||||||
}
|
}
|
||||||
|
|
||||||
ct++;
|
ct++;
|
||||||
|
|
Loading…
Add table
Reference in a new issue