From f0f0cfd1377d5134bd1c466f88f2583615f65b6b Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Wed, 28 Oct 2009 17:14:27 +0100 Subject: [PATCH] Added an explicit destruction action for sessions. --- src/dolda/jsvc/util/PerSession.java | 2 +- src/dolda/jsvc/util/Session.java | 27 +++++++++++++++++++-------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/dolda/jsvc/util/PerSession.java b/src/dolda/jsvc/util/PerSession.java index 716b154..562e2c9 100644 --- a/src/dolda/jsvc/util/PerSession.java +++ b/src/dolda/jsvc/util/PerSession.java @@ -96,7 +96,7 @@ public class PerSession implements Responder { if(resp instanceof ContextResponder) { final ContextResponder cr = (ContextResponder)resp; sess.listen(new Session.Listener() { - public void expire(Session sess) { + public void destroy(Session sess) { cr.destroy(); } }); diff --git a/src/dolda/jsvc/util/Session.java b/src/dolda/jsvc/util/Session.java index 8357cb8..679a72f 100644 --- a/src/dolda/jsvc/util/Session.java +++ b/src/dolda/jsvc/util/Session.java @@ -9,6 +9,7 @@ public class Session implements java.io.Serializable { private static final Map cache = new WeakHashMap(); private static final SecureRandom prng; private static long lastclean = 0; + private final String id; private final Map props = new HashMap(); private long ctime = System.currentTimeMillis(), atime = ctime, etime = 86400 * 1000; private Collection ll = new HashSet(); @@ -22,7 +23,11 @@ public class Session implements java.io.Serializable { } public static interface Listener { - public void expire(Session sess); + public void destroy(Session sess); + } + + private Session(String id) { + this.id = id; } public synchronized void listen(Listener l) { @@ -40,9 +45,16 @@ public class Session implements java.io.Serializable { return(props.put(key, val)); } + public void destroy() { + synchronized(Session.class) { + sessions.remove(id); + } + expire(); + } + private synchronized void expire() { for(Listener l : ll) - l.expire(this); + l.destroy(this); } public synchronized static int num() { @@ -60,8 +72,8 @@ public class Session implements java.io.Serializable { return(buf.toString()); } - private static Session create(Request req) { - Session sess = new Session(); + private static Session create(Request req, String id) { + Session sess = new Session(id); long etime = 0; int ct; ct = Integer.parseInt(req.ctx().libconfig("jsvc.session.expire", "0")); @@ -103,10 +115,9 @@ public class Session implements java.io.Serializable { if(sc != null) sess = sessions.get(sc.value); if(sess == null) { - String id = newid(); - sess = create(req); - sessions.put(id, sess); - sc = new Cookie("jsvc-session", id); + sess = create(req, newid()); + sessions.put(sess.id, sess); + sc = new Cookie("jsvc-session", sess.id); sc.expires = new Date(System.currentTimeMillis() + (86400L * 365L * 1000L)); sc.path = req.ctx().sysconfig("jsvc.session.path", req.rooturl().getPath()); String pd = req.ctx().sysconfig("jsvc.session.domain", null); -- 2.11.0