KickJava   Java API By Example, From Geeks To Geeks.

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


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.session.SessionSupport;
8 import com.terracotta.session.util.ContextMgr;
9 import com.terracotta.session.util.LifecycleEventMgr;
10 import com.terracotta.session.util.StringArrayEnumeration;
11 import com.terracotta.session.util.Timestamp;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.Enumeration JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Set JavaDoc;
19
20 import javax.servlet.ServletContext JavaDoc;
21 import javax.servlet.http.HttpSessionContext JavaDoc;
22
23 public class SessionData implements Session, SessionSupport {
24   private final Map JavaDoc attributes = new HashMap JavaDoc();
25   private final Map JavaDoc internalAttributes = new HashMap JavaDoc();
26   private transient Map JavaDoc transientAttributes;
27   private final long createTime;
28   private final Timestamp timestamp;
29
30   private long lastAccessedTime;
31   private long maxIdleMillis;
32   private transient long requestStartMillis;
33
34   private transient SessionId sessionId;
35   private transient LifecycleEventMgr eventMgr;
36   private transient ContextMgr contextMgr;
37   private transient boolean invalidated = false;
38
39   protected SessionData(int maxIdleSeconds) {
40     this.createTime = System.currentTimeMillis();
41     this.lastAccessedTime = 0;
42     setMaxInactiveMillis(maxIdleSeconds * 1000);
43     this.timestamp = new Timestamp(this.createTime + maxIdleMillis);
44   }
45
46   void associate(SessionId sid, LifecycleEventMgr lifecycleEventMgr, ContextMgr ctxMgr) {
47     this.sessionId = sid;
48     this.eventMgr = lifecycleEventMgr;
49     this.contextMgr = ctxMgr;
50   }
51
52   public SessionId getSessionId() {
53     return this.sessionId;
54   }
55
56   public SessionData getSessionData() {
57     return this;
58   }
59
60   public ServletContext JavaDoc getServletContext() {
61     return contextMgr.getServletContext();
62   }
63
64   public HttpSessionContext JavaDoc getSessionContext() {
65     return contextMgr.getSessionContext();
66   }
67
68   public synchronized boolean isValid() {
69     if (invalidated) { return false; }
70     final boolean isValid = getIdleMillis() < getMaxInactiveMillis();
71     return isValid;
72   }
73
74   public boolean isNew() {
75     checkIfValid();
76     return sessionId.isNew();
77   }
78
79   public synchronized void invalidate() {
80     if (invalidated) { throw new IllegalStateException JavaDoc("session already invalidated"); }
81
82     try {
83       eventMgr.fireSessionDestroyedEvent(this);
84
85       String JavaDoc[] attrs = (String JavaDoc[]) attributes.keySet().toArray(new String JavaDoc[attributes.size()]);
86
87       for (int i = 0; i < attrs.length; i++) {
88         unbindAttribute(attrs[i]);
89       }
90     } finally {
91       invalidated = true;
92     }
93   }
94
95   private void checkIfValid() {
96     if (!isValid()) throw new IllegalStateException JavaDoc("This session is invalid");
97   }
98
99   synchronized void startRequest() {
100     requestStartMillis = System.currentTimeMillis();
101   }
102
103   public void setMaxInactiveInterval(int v) {
104     setMaxInactiveMillis(v * 1000);
105     if (isValid() && v == 0) {
106       invalidate();
107     }
108   }
109
110   /**
111    * returns idle millis.
112    */

113   synchronized long getIdleMillis() {
114     if (lastAccessedTime == 0) return 0;
115     if (requestStartMillis > lastAccessedTime) return requestStartMillis - lastAccessedTime;
116     return Math.max(System.currentTimeMillis() - lastAccessedTime, 0);
117   }
118
119   synchronized void finishRequest() {
120     requestStartMillis = 0;
121     lastAccessedTime = System.currentTimeMillis();
122   }
123
124   public synchronized long getCreationTime() {
125     checkIfValid();
126     return createTime;
127   }
128
129   public synchronized long getLastAccessedTime() {
130     checkIfValid();
131     return lastAccessedTime;
132   }
133
134   public void setAttribute(String JavaDoc name, Object JavaDoc value) {
135     setAttributeReturnOld(name, value);
136   }
137
138   public synchronized Object JavaDoc setAttributeReturnOld(String JavaDoc name, Object JavaDoc value) {
139     checkIfValid();
140     if (value == null) {
141       return unbindAttribute(name);
142     } else {
143       return bindAttribute(name, value);
144     }
145   }
146
147   public void putValue(String JavaDoc name, Object JavaDoc val) {
148     setAttribute(name, val);
149   }
150
151   public synchronized Object JavaDoc getAttribute(String JavaDoc name) {
152     checkIfValid();
153     return attributes.get(name);
154   }
155
156   public Object JavaDoc getValue(String JavaDoc name) {
157     return getAttribute(name);
158   }
159
160   public synchronized String JavaDoc[] getValueNames() {
161     checkIfValid();
162     Set JavaDoc keys = attributes.keySet();
163     return (String JavaDoc[]) keys.toArray(new String JavaDoc[keys.size()]);
164   }
165
166   public Enumeration getAttributeNames() {
167     return new StringArrayEnumeration(getValueNames());
168   }
169
170   public void removeAttribute(String JavaDoc name) {
171     removeAttributeReturnOld(name);
172   }
173
174   public synchronized Object JavaDoc removeAttributeReturnOld(String JavaDoc name) {
175     checkIfValid();
176     return unbindAttribute(name);
177   }
178
179   public void removeValue(String JavaDoc name) {
180     removeAttribute(name);
181   }
182
183   synchronized long getMaxInactiveMillis() {
184     return maxIdleMillis;
185   }
186
187   public int getMaxInactiveInterval() {
188     return (int) getMaxInactiveMillis() / 1000;
189   }
190
191   private void setMaxInactiveMillis(long v) {
192     maxIdleMillis = v;
193   }
194
195   public synchronized Object JavaDoc getInternalAttribute(String JavaDoc name) {
196     checkIfValid();
197     return internalAttributes.get(name);
198   }
199
200   public synchronized Object JavaDoc setInternalAttribute(String JavaDoc name, Object JavaDoc value) {
201     checkIfValid();
202     return internalAttributes.put(name, value);
203   }
204
205   public synchronized Object JavaDoc removeInternalAttribute(String JavaDoc name) {
206     checkIfValid();
207     return internalAttributes.remove(name);
208   }
209
210   public synchronized Object JavaDoc getTransientAttribute(String JavaDoc name) {
211     checkIfValid();
212     return getTransientAttributes().get(name);
213   }
214
215   public synchronized Object JavaDoc setTransientAttribute(String JavaDoc name, Object JavaDoc value) {
216     checkIfValid();
217     return getTransientAttributes().put(name, value);
218   }
219
220   public synchronized Object JavaDoc removeTransientAttribute(String JavaDoc name) {
221     checkIfValid();
222     return getTransientAttributes().remove(name);
223   }
224
225   public synchronized Collection JavaDoc getTransientAttributeKeys() {
226     checkIfValid();
227     return new ArrayList JavaDoc(getTransientAttributes().keySet());
228   }
229
230   private Object JavaDoc bindAttribute(String JavaDoc name, Object JavaDoc newVal) {
231     Object JavaDoc oldVal = getAttribute(name);
232     if (newVal != oldVal) eventMgr.bindAttribute(this, name, newVal);
233
234     oldVal = attributes.put(name, newVal);
235
236     if (oldVal != newVal) eventMgr.unbindAttribute(this, name, oldVal);
237
238     // now deal with attribute listener events
239
if (oldVal != null) eventMgr.replaceAttribute(this, name, oldVal, newVal);
240     else eventMgr.setAttribute(this, name, newVal);
241
242     return oldVal;
243   }
244
245   private Object JavaDoc unbindAttribute(String JavaDoc name) {
246     Object JavaDoc oldVal = attributes.remove(name);
247     if (oldVal != null) {
248       eventMgr.unbindAttribute(this, name, oldVal);
249       eventMgr.removeAttribute(this, name, oldVal);
250     }
251     return oldVal;
252   }
253
254   public void resumeRequest() {
255     TerracottaSessionManager.resumeRequest(this);
256   }
257
258   public void pauseRequest() {
259     TerracottaSessionManager.pauseRequest(this);
260   }
261
262   private Map JavaDoc getTransientAttributes() {
263     if (transientAttributes == null) {
264       transientAttributes = new HashMap JavaDoc();
265     }
266     return transientAttributes;
267   }
268
269   public String JavaDoc getId() {
270     return sessionId.getExternalId();
271   }
272
273   Timestamp getTimestamp() {
274     return timestamp;
275   }
276
277 }
278
Popular Tags