KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > terracotta > session > SessionDataStore


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

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 JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.Set JavaDoc;
18
19 public class SessionDataStore {
20
21   private final Map JavaDoc store; // <SessionData>
22
private final Map JavaDoc dtmStore; // <Timestamp>
23
private final int maxIdleTimeoutSeconds;
24   private final ContextMgr ctxMgr;
25   private final LifecycleEventMgr lifecycleEventMgr;
26
27   public SessionDataStore(String JavaDoc 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 JavaDoc sessionRootName = "tc:session_" + appName;
36     final String JavaDoc dtmRootName = "@tc:session_timestamp_" + appName;
37     final Lock lock = new Lock(sessionRootName);
38     lock.getWriteLock();
39     try {
40       this.store = (Hashtable JavaDoc) ManagerUtil.lookupOrCreateRootNoDepth(sessionRootName, new Hashtable JavaDoc());
41       ((Manageable) store).__tc_managed().disableAutoLocking();
42       this.dtmStore = (Hashtable JavaDoc) ManagerUtil.lookupOrCreateRootNoDepth(dtmRootName, new Hashtable JavaDoc());
43       ((Manageable) dtmStore).__tc_managed().disableAutoLocking();
44     } finally {
45       lock.commitLock();
46     }
47     Assert.post(store != null);
48   }
49
50   /**
51    * <ol>
52    * <li>get WRITE_LOCK for sessId
53    * <li>creates session data
54    * <li>put newly-created SessionData into the global Map
55    * <li>returns newly-created SessionData
56    * </ol>
57    */

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   /**
71    * <ol>
72    * <li>get WRITE_LOCK for sessId
73    * <li>look up SessionData for sessId.getKey() in the global Map
74    * <li>if SessionData is invalid, unlock sessId and return null (invalidator will take care of killing this session)
75    * <li>return valid SessionData
76    */

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 JavaDoc[] getAllKeys() {
120     String JavaDoc[] rv;
121     synchronized (store) {
122       Set JavaDoc keys = store.keySet();
123       rv = (String JavaDoc[]) keys.toArray(new String JavaDoc[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