- public synchronized Object get(Object key, Object def) {
- if(props.containsKey(key))
- return(props.get(key));
- else
- return(def);
- }
-
- public synchronized Object put(Object key, Object val) {
- return(props.put(key, val));
- }
-
- private synchronized void expire() {
- for(Listener l : ll)
- l.expire(this);
+ 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<String, Cookie> 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());
+ }