X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fdolda%2Fjsvc%2Futil%2FSession.java;h=01c6d161ea7dd6e3ccc96be1a233a13429a127d5;hb=55a299a00e99f3c63a3ece90072aad80d8a00744;hp=31b65a7719e9f6459c1f8a560b9a765624c8a3d9;hpb=d5dd6a2dada08df30133b739d0ce1fa8f2a0d2e9;p=jsvc.git diff --git a/src/dolda/jsvc/util/Session.java b/src/dolda/jsvc/util/Session.java index 31b65a7..01c6d16 100644 --- a/src/dolda/jsvc/util/Session.java +++ b/src/dolda/jsvc/util/Session.java @@ -4,26 +4,156 @@ import dolda.jsvc.*; import java.util.*; import java.security.SecureRandom; -public class Session implements java.io.Serializable { - private static final Map sessions = new HashMap(); - private static final SecureRandom prng; - private static long lastclean = 0; - private final Map props = new HashMap(); - private long ctime = System.currentTimeMillis(), atime = ctime, etime = 86400 * 1000; +public abstract class Session implements java.io.Serializable { + public static final ContextParam store = new ContextParam(new MemoryStorage()); + private static final Map cache = new WeakHashMap(); + private final Map props = new IdentityHashMap(); + public long ctime = System.currentTimeMillis(), atime = ctime, etime = 86400 * 1000; private Collection ll = new HashSet(); - static { - try { - prng = SecureRandom.getInstance("SHA1PRNG"); - } catch(java.security.NoSuchAlgorithmException e) { - throw(new Error(e)); + public static interface Listener { + public void destroy(Session sess); + } + + public static interface Storage { + public Session get(Request req); + } + + public static abstract class BaseStorage implements Storage { + private static final SecureRandom prng; + + static { + try { + prng = SecureRandom.getInstance("SHA1PRNG"); + } catch(java.security.NoSuchAlgorithmException e) { + throw(new Error(e)); + } + } + + public Session get(Request req) { + MultiMap cookies = Cookie.get(req); + Cookie sc = cookies.get("jsvc-session"); + + Session sess = null; + if(sc != null) + sess = get(sc.value); + if(sess == null) { + sess = create(req); + 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); + if(pd != null) + sc.domain = pd; + sc.addto(req); + } + return(sess); + } + + protected abstract Session get(String id); + protected abstract Session create(Request req); + + public static String newid() { + byte[] rawid = new byte[16]; + prng.nextBytes(rawid); + StringBuilder buf = new StringBuilder(); + for(byte b : rawid) { + buf.append(Misc.int2hex((b & 0xf0) >> 4, false)); + buf.append(Misc.int2hex(b & 0x0f, false)); + } + return(buf.toString()); } } - public static interface Listener { - public void expire(Session sess); + public static class MemoryStorage extends BaseStorage { + private final Map sessions = new HashMap(); + private static long lastclean = 0; + + private class MemorySession extends Session { + private final long id = BaseStorage.prng.nextLong(); + + private MemorySession(Request req) { + super(req); + } + + public void destroy() { + synchronized(sessions) { + sessions.remove(id); + } + super.destroy(); + } + + private void expire() { + super.destroy(); + } + + public String id() { + return(Long.toString(id)); + } + } + + public int num() { + synchronized(sessions) { + return(sessions.size()); + } + } + + public Session get(String id) { + long idl; + try { + idl = Long.parseLong(id); + } catch(NumberFormatException e) { + return(null); + } + synchronized(sessions) { + return(sessions.get(idl)); + } + } + + public synchronized Session create(Request req) { + MemorySession sess = new MemorySession(req); + synchronized(sessions) { + sessions.put(sess.id, sess); + } + return(sess); + } + + private void clean() { + long now = System.currentTimeMillis(); + synchronized(sessions) { + for(Iterator i = sessions.values().iterator(); i.hasNext();) { + MemorySession sess = i.next(); + if(now > sess.atime + sess.etime) { + i.remove(); + sess.expire(); + } + } + } + } + + public Session get(Request req) { + long now = System.currentTimeMillis(); + if(now - lastclean > 3600 * 1000) { + clean(); + lastclean = now; + } + + return(super.get(req)); + } + } + + protected Session(Request req) { + int ct; + ct = Integer.parseInt(req.ctx().libconfig("jsvc.session.expire", "0")); + if(ct > 0) + etime = ct; + ct = Integer.parseInt(req.ctx().sysconfig("jsvc.session.expire", "0")); + if(ct > 0) + etime = ct; } + public abstract String id(); + public void listen(Listener l) { synchronized(ll) { ll.add(l); @@ -39,87 +169,30 @@ public class Session implements java.io.Serializable { } } - public Object put(Object key, Object val) { - synchronized(props) { - return(props.put(key, val)); - } + public synchronized Object put(Object key, Object val) { + return(props.put(key, val)); } - private void expire() { + public void destroy() { synchronized(ll) { for(Listener l : ll) - l.expire(this); + l.destroy(this); } } - public static int num() { - synchronized(sessions) { - return(sessions.size()); - } - } - - private static String newid() { - byte[] rawid = new byte[16]; - prng.nextBytes(rawid); - StringBuilder buf = new StringBuilder(); - for(byte b : rawid) { - buf.append(Misc.int2hex((b & 0xf0) >> 4, false)); - buf.append(Misc.int2hex(b & 0x0f, false)); - } - return(buf.toString()); - } - - private static Session create(Request req) { - Session sess = new Session(); - long etime = 0; - int ct; - ct = Integer.parseInt(req.ctx().libconfig("jsvc.session.expire", "0")); - if(ct > 0) - sess.etime = ct; - ct = Integer.parseInt(req.ctx().sysconfig("jsvc.session.expire", "0")); - if(ct > 0) - sess.etime = ct; - return(sess); - } - - private static void clean() { - long now = System.currentTimeMillis(); - synchronized(sessions) { - for(Iterator i = sessions.values().iterator(); i.hasNext();) { - Session sess = i.next(); - if(now > sess.atime + sess.etime) { - i.remove(); - sess.expire(); - } - } - } - } - public static Session get(Request req) { - long now = System.currentTimeMillis(); - if(now - lastclean > 3600 * 1000) { - clean(); - lastclean = now; - } - - MultiMap cookies = Cookie.get(req); - Cookie sc = cookies.get("jsvc-session"); - Session sess = null; - synchronized(sessions) { - if(sc != null) - sess = sessions.get(sc.value); + Session sess; + synchronized(req) { + synchronized(cache) { + sess = cache.get(req); + } if(sess == null) { - String id = newid(); - sess = create(req); - sessions.put(id, sess); - sc = new Cookie("jsvc-session", 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); - if(pd != null) - sc.domain = pd; - sc.addto(req); + sess = store.get().get(req); + synchronized(cache) { + cache.put(req, sess); + } } + sess.atime = System.currentTimeMillis(); } return(sess); }