1 5 package com.terracotta.session; 6 7 import com.tc.object.bytecode.Manageable; 8 import com.tc.object.bytecode.ManagerUtil; 9 import com.terracotta.session.util.Assert; 10 import com.terracotta.session.util.ContextMgr; 11 import com.terracotta.session.util.LifecycleEventMgr; 12 import com.terracotta.session.util.Lock; 13 import com.terracotta.session.util.Timestamp; 14 15 import java.util.Hashtable ; 16 import java.util.Map ; 17 import java.util.Set ; 18 19 public class SessionDataStore { 20 21 private final Map store; private final Map dtmStore; private final int maxIdleTimeoutSeconds; 24 private final ContextMgr ctxMgr; 25 private final LifecycleEventMgr lifecycleEventMgr; 26 27 public SessionDataStore(String appName, int maxIdleTimeoutSeconds, LifecycleEventMgr lifecycleEventMgr, 28 ContextMgr ctxMgr) { 29 Assert.pre(appName != null && appName.length() > 0); 30 31 this.maxIdleTimeoutSeconds = maxIdleTimeoutSeconds; 32 this.lifecycleEventMgr = lifecycleEventMgr; 33 this.ctxMgr = ctxMgr; 34 35 final String sessionRootName = "tc:session_" + appName; 36 final String dtmRootName = "@tc:session_timestamp_" + appName; 37 final Lock lock = new Lock(sessionRootName); 38 lock.getWriteLock(); 39 try { 40 this.store = (Hashtable ) ManagerUtil.lookupOrCreateRootNoDepth(sessionRootName, new Hashtable ()); 41 ((Manageable) store).__tc_managed().disableAutoLocking(); 42 this.dtmStore = (Hashtable ) ManagerUtil.lookupOrCreateRootNoDepth(dtmRootName, new Hashtable ()); 43 ((Manageable) dtmStore).__tc_managed().disableAutoLocking(); 44 } finally { 45 lock.commitLock(); 46 } 47 Assert.post(store != null); 48 } 49 50 58 public SessionData createSessionData(final SessionId sessId) { 59 Assert.pre(sessId != null); 60 SessionData rv = null; 61 sessId.getWriteLock(); 62 rv = new SessionData(maxIdleTimeoutSeconds); 63 rv.associate(sessId, lifecycleEventMgr, ctxMgr); 64 store.put(sessId.getKey(), rv); 65 dtmStore.put(sessId.getKey(), rv.getTimestamp()); 66 rv.startRequest(); 67 return rv; 68 } 69 70 77 public SessionData find(final SessionId sessId) { 78 Assert.pre(sessId != null); 79 80 SessionData rv = null; 81 sessId.getWriteLock(); 82 try { 83 rv = (SessionData) store.get(sessId.getKey()); 84 if (rv != null) { 85 rv.associate(sessId, lifecycleEventMgr, ctxMgr); 86 rv.startRequest(); 87 if (!rv.isValid()) rv = null; 88 else { 89 updateTimestampIfNeeded(rv); 90 } 91 } 92 } finally { 93 if (rv == null) sessId.commitLock(); 94 } 95 return rv; 96 } 97 98 void updateTimestampIfNeeded(SessionData sd) { 99 Assert.pre(sd != null); 100 final long now = System.currentTimeMillis(); 101 final Timestamp t = sd.getTimestamp(); 102 final long diff = t.getMillis() - now; 103 if (diff < (sd.getMaxInactiveMillis() / 2) || diff > (sd.getMaxInactiveMillis())) { 104 t.setMillis(now + sd.getMaxInactiveMillis()); 105 } 106 } 107 108 public void remove(final SessionId id) { 109 Assert.pre(id != null); 110 id.getWriteLock(); 111 try { 112 store.remove(id.getKey()); 113 dtmStore.remove(id.getKey()); 114 } finally { 115 id.commitLock(); 116 } 117 } 118 119 public String [] getAllKeys() { 120 String [] rv; 121 synchronized (store) { 122 Set keys = store.keySet(); 123 rv = (String []) keys.toArray(new String [keys.size()]); 124 } 125 Assert.post(rv != null); 126 return rv; 127 } 128 129 Timestamp findTimestampUnlocked(final SessionId sessId) { 130 return (Timestamp) dtmStore.get(sessId.getKey()); 131 } 132 133 SessionData findSessionDataUnlocked(final SessionId sessId) { 134 final SessionData rv = (SessionData) store.get(sessId.getKey()); 135 if (rv != null) { 136 rv.associate(sessId, lifecycleEventMgr, ctxMgr); 137 } 138 return rv; 139 } 140 141 } 142 | Popular Tags |