1 package dolda.jsvc.util;
5 import java.security.SecureRandom;
7 public abstract class Session implements java.io.Serializable {
8 public static final ContextParam<Storage> store = new ContextParam<Storage>(new MemoryStorage());
9 private static final Map<Request, Session> cache = new WeakHashMap<Request, Session>();
10 private final Map<Object, Object> props = new IdentityHashMap<Object, Object>();
11 public long ctime = System.currentTimeMillis(), atime = ctime, etime = 86400 * 1000;
12 private Collection<Listener> ll = new HashSet<Listener>();
14 public static interface Listener {
15 public void destroy(Session sess);
18 public static interface Storage {
19 public Session get(Request req);
22 public static abstract class BaseStorage implements Storage {
23 private static final SecureRandom prng;
27 prng = SecureRandom.getInstance("SHA1PRNG");
28 } catch(java.security.NoSuchAlgorithmException e) {
33 public Session get(Request req) {
34 MultiMap<String, Cookie> cookies = Cookie.get(req);
35 Cookie sc = cookies.get("jsvc-session");
42 sc = new Cookie("jsvc-session", sess.id());
43 sc.expires = new Date(System.currentTimeMillis() + (86400L * 365L * 1000L));
44 sc.path = req.ctx().sysconfig("jsvc.session.path", req.rooturl().getPath());
45 String pd = req.ctx().sysconfig("jsvc.session.domain", null);
53 protected abstract Session get(String id);
54 protected abstract Session create(Request req);
56 public static String newid() {
57 byte[] rawid = new byte[16];
58 prng.nextBytes(rawid);
59 StringBuilder buf = new StringBuilder();
61 buf.append(Misc.int2hex((b & 0xf0) >> 4, false));
62 buf.append(Misc.int2hex(b & 0x0f, false));
64 return(buf.toString());
68 public static class MemoryStorage extends BaseStorage {
69 private final Map<Long, MemorySession> sessions = new HashMap<Long, MemorySession>();
70 private static long lastclean = 0;
72 private class MemorySession extends Session {
73 private final long id = BaseStorage.prng.nextLong();
75 private MemorySession(Request req) {
79 public void destroy() {
80 synchronized(sessions) {
86 private void expire() {
91 return(Long.toString(id));
96 synchronized(sessions) {
97 return(sessions.size());
101 public Session get(String id) {
104 idl = Long.parseLong(id);
105 } catch(NumberFormatException e) {
108 synchronized(sessions) {
109 return(sessions.get(idl));
113 public synchronized Session create(Request req) {
114 MemorySession sess = new MemorySession(req);
115 synchronized(sessions) {
116 sessions.put(sess.id, sess);
121 private void clean() {
122 long now = System.currentTimeMillis();
123 synchronized(sessions) {
124 for(Iterator<MemorySession> i = sessions.values().iterator(); i.hasNext();) {
125 MemorySession sess = i.next();
126 if(now > sess.atime + sess.etime) {
134 public Session get(Request req) {
135 long now = System.currentTimeMillis();
136 if(now - lastclean > 3600 * 1000) {
141 return(super.get(req));
145 protected Session(Request req) {
147 ct = Integer.parseInt(req.ctx().libconfig("jsvc.session.expire", "0"));
150 ct = Integer.parseInt(req.ctx().sysconfig("jsvc.session.expire", "0"));
155 public abstract String id();
157 public void listen(Listener l) {
163 public Object get(Object key, Object def) {
164 synchronized(props) {
165 if(props.containsKey(key))
166 return(props.get(key));
172 public synchronized Object put(Object key, Object val) {
173 return(props.put(key, val));
176 public void destroy() {
183 public static Session get(Request req) {
186 synchronized(cache) {
187 sess = cache.get(req);
190 sess = store.get().get(req);
191 synchronized(cache) {
192 cache.put(req, sess);
195 sess.atime = System.currentTimeMillis();
200 public static Session get() {
201 return(get(RequestThread.request()));