KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > http > server22 > HSession


1 package com.quadcap.http.server22;
2
3 /* Copyright 1998 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.util.Date JavaDoc;
42 import java.util.Enumeration JavaDoc;
43 import java.util.Hashtable JavaDoc;
44 import java.util.Vector JavaDoc;
45
46 import javax.servlet.http.HttpSession JavaDoc;
47 import javax.servlet.http.HttpSessionBindingEvent JavaDoc;
48 import javax.servlet.http.HttpSessionBindingListener JavaDoc;
49 import javax.servlet.http.HttpSessionContext JavaDoc;
50
51 import com.quadcap.util.Debug;
52
53 /**
54  * This class implements the <code>javax.servlet.http.HttpSession</code> interface.
55  *
56  * @author Stan Bailes
57  */

58 public class HSession implements HttpSession JavaDoc {
59     WebApplication app;
60     String JavaDoc sessionId;
61     boolean valid = true;
62     long creationTime = new Date JavaDoc().getTime();
63     long lastAccessTime = creationTime;
64     int maxInactiveInterval;
65     Hashtable JavaDoc appData = new Hashtable JavaDoc();
66     boolean newSession = true;
67
68     /**
69      * Construct a new session object.
70      *
71      * @param app the server context (session context) owning this session.
72      * @param sessionId this session's id.
73      */

74     public HSession(WebApplication app, String JavaDoc sessionId,
75                     int maxInactiveInterval)
76     {
77         this.app = app;
78         this.sessionId = sessionId;
79     this.maxInactiveInterval = maxInactiveInterval;
80     }
81     
82     /**
83      * Returns the identifier assigned to this session. An HttpSession's
84      * identifier is a unique string that is created and maintained by
85      * HttpSessionContext.
86      *
87      * @return the identifier assigned to this session
88      * @exception IllegalStateException if an attempt is made to access
89      * session data after the session has been invalidated
90      */

91     public String JavaDoc getId() {
92         if (!valid) throw new IllegalStateException JavaDoc("session not valid");
93         return sessionId;
94     }
95
96     /**
97      * Returns the context in which this session is bound.
98      *
99      * @return the name of the context in which this session is bound
100      * @exception IllegalStateException if an attempt is made to access
101      * session data after the session has been invalidated
102      * @deprecated in 2.2
103      */

104     public HttpSessionContext JavaDoc getSessionContext() {
105         return new HttpSessionContext JavaDoc() {
106             public Enumeration JavaDoc getIds() { return new Vector JavaDoc().elements(); }
107             public HttpSession JavaDoc getSession(String JavaDoc s) { return null; }
108         };
109     }
110
111     /**
112      * Returns the time at which this session representation was created,
113      * in milliseconds since midnight, January 1, 1970 UTC.
114      *
115      * @return the time when the session was created
116      * @exception IllegalStateException if an attempt is made to access
117      * session data after the session has been invalidated
118      */

119     public long getCreationTime() {
120         if (!valid) throw new IllegalStateException JavaDoc("session not valid");
121         return creationTime;
122     }
123
124     /**
125      * Returns the last time the client sent a request carrying the identifier
126      * assigned to the session. Time is expressed
127      * as milliseconds since midnight, January 1,
128      * 1970 UTC.
129      * Application level operations, such as getting or setting a value
130      * associated with the session, does not affect the access time.
131      *
132      * <P> This information is particularly useful in session management
133      * policies. For example,
134      * <UL>
135      * <LI>a session manager could leave all sessions
136      * which have not been used in a long time
137      * in a given context.
138      * <LI>the sessions can be sorted according to age to optimize some task.
139      * </UL>
140      *
141      * @return the last time the client sent a request carrying the identifier
142      * assigned to the session
143      * @exception IllegalStateException if an attempt is made to access
144      * session data after the session has been invalidated
145      */

146     public long getLastAccessedTime() {
147         if (!valid) throw new IllegalStateException JavaDoc("session not valid");
148         return lastAccessTime;
149     }
150
151     /**
152      * Causes this representation of the session to be invalidated and removed
153      * from its context.
154      *
155      * @exception IllegalStateException if an attempt is made to access
156      * session data after the session has been invalidated
157      */

158     public void invalidate() {
159         if (!valid) throw new IllegalStateException JavaDoc("session not valid");
160     Enumeration JavaDoc e = appData.keys();
161     while (e.hasMoreElements()) {
162         String JavaDoc key = e.nextElement().toString();
163         removeValue(key);
164     }
165         app.removeSession(this);
166         valid = false;
167     }
168
169     /**
170      * Binds the specified object into the session's application layer data
171      * with the given name. Any existing binding with the same name is
172      * replaced. New (or existing) values that implement the
173      * HttpSessionBindingListener interface will call its
174      * valueBound() method.
175      *
176      * @param name the name to which the data object will be bound. This
177      * parameter cannot be null.
178      * @param value the data object to be bound.
179      * This parameter cannot be null.
180      * @exception IllegalStateException if an attempt is made to access
181      * session data after the session has been invalidated.
182      */

183     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
184         if (!valid) throw new IllegalStateException JavaDoc("session not valid");
185         appData.put(name, value);
186         if (value instanceof HttpSessionBindingListener JavaDoc) {
187             HttpSessionBindingEvent JavaDoc event =
188                 new HttpSessionBindingEvent JavaDoc(this, name);
189             HttpSessionBindingListener JavaDoc listener =
190                 (HttpSessionBindingListener JavaDoc)value;
191             listener.valueBound(event);
192         }
193     }
194
195     /**
196      * Returns the object bound to the given name in the session's
197      * application layer data. Returns null if there is no such binding.
198      *
199      * @param name the name of the binding to find
200      * @return the value bound to that name, or null if the binding does
201      * not exist.
202      * @exception IllegalStateException if an attempt is made to access
203      * HttpSession's session data after it has been invalidated
204      */

205     public Object JavaDoc getAttribute(String JavaDoc name) {
206         if (!valid) throw new IllegalStateException JavaDoc("session not valid");
207         return appData.get(name);
208     }
209
210     /**
211      * Removes the object bound to the given name in the session's
212      * application layer data. Does nothing if there is no object
213      * bound to the given name. The value that implements the
214      * HttpSessionBindingListener interface will call its
215      * valueUnbound() method.
216      *
217      * @param name the name of the object to remove
218      * @exception IllegalStateException if an attempt is made to access
219      * session data after the session has been invalidated
220      */

221     public void removeAttribute(String JavaDoc name) {
222         if (!valid) throw new IllegalStateException JavaDoc("session not valid");
223         Object JavaDoc value = appData.get(name);
224         if (value != null) {
225             appData.remove(name);
226             if (value instanceof HttpSessionBindingListener JavaDoc) {
227                 HttpSessionBindingEvent JavaDoc event =
228                     new HttpSessionBindingEvent JavaDoc(this, name);
229                 HttpSessionBindingListener JavaDoc listener =
230                     (HttpSessionBindingListener JavaDoc)value;
231                 listener.valueUnbound(event);
232             }
233         }
234     }
235
236     /**
237      * Return an enumeration of the names of objects in the session's
238      * application layer data.
239      *
240      * @return an enumeration of names
241      */

242     public Enumeration JavaDoc getAttributeNames() {
243         if (!valid) throw new IllegalStateException JavaDoc("session not valid");
244         return appData.keys();
245     }
246     
247     /**
248      * Returns an array of the names of all the application layer
249      * data objects bound into the session. For example, if you want to delete
250      * all of the data objects bound into the session, use this method to
251      * obtain their names.
252      *
253      * @return an array containing the names of all of the application layer
254      * data objects bound into the session
255      * @exception IllegalStateException if an attempt is made to access
256      * session data after the session has been invalidated
257      * @deprecated
258      */

259     public String JavaDoc[] getValueNames() {
260         if (!valid) throw new IllegalStateException JavaDoc("session not valid");
261         String JavaDoc[] names = new String JavaDoc[appData.size()];
262         Enumeration JavaDoc e = appData.keys();
263         int i = 0;
264         while (e.hasMoreElements()) {
265             names[i++] = (String JavaDoc)e.nextElement();
266         }
267         return names;
268     }
269
270     /**
271      * @deprecated
272      */

273     public Object JavaDoc getValue(String JavaDoc name) {
274         return getAttribute(name);
275     }
276
277     /**
278      * @deprecated
279      */

280     public void putValue(String JavaDoc name, Object JavaDoc value) {
281         setAttribute(name, value);
282     }
283
284     /**
285      * @deprecated
286      */

287     public void removeValue(String JavaDoc name) {
288         removeAttribute(name);
289     }
290
291     /**
292      * A session is considered to be "new" if it has been created by the
293      * server, but the client has not yet acknowledged joining the session.
294      * For example,
295      * if the server supported only cookie-based sessions and the client had
296      * completely disabled the use of cookies, then calls to
297      * HttpServletRequest.getSession() would
298      * always return "new" sessions.
299      *
300      * @return true if the session has been created by the server but the
301      * client has not yet acknowledged joining the session; false otherwise
302      * @exception IllegalStateException if an attempt is made to access
303      * session data after the session has been invalidated
304      */

305     public boolean isNew() {
306         if (!valid) throw new IllegalStateException JavaDoc("session not valid");
307         return newSession;
308     }
309
310     public int getMaxInactiveInterval() {
311         if (!valid) throw new IllegalStateException JavaDoc("session not valid");
312         return maxInactiveInterval;
313     }
314     public void setMaxInactiveInterval(int ivl) {
315         if (!valid) throw new IllegalStateException JavaDoc("session not valid");
316     this.maxInactiveInterval = ivl;
317     }
318
319     /**
320      * ---- Package-private implementation -----
321      */

322
323     boolean isValid() { return valid; }
324     void setNotNew() { newSession = false; }
325     void updateLastAccess() { lastAccessTime = new Date JavaDoc().getTime(); }
326 }
327
Popular Tags