KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > webman > template > jsp > WebManSession


1 package de.webman.template.jsp;
2
3
4 import java.io.IOException JavaDoc;
5 import java.io.ObjectInputStream JavaDoc;
6 import java.io.ObjectOutputStream JavaDoc;
7 import java.io.Serializable JavaDoc;
8 import java.util.Enumeration JavaDoc;
9 import java.util.Hashtable JavaDoc;
10 import java.util.Vector JavaDoc;
11 import javax.servlet.ServletException JavaDoc;
12 import javax.servlet.ServletContext JavaDoc;
13 import javax.servlet.http.HttpSession JavaDoc;
14 import javax.servlet.http.HttpSessionBindingEvent JavaDoc;
15 import javax.servlet.http.HttpSessionBindingListener JavaDoc;
16 import javax.servlet.http.HttpSessionContext JavaDoc;
17
18
19 /**
20  * Standard implementation of the <b>Session</b> interface. This object is
21  * serializable, so that it can be stored in persistent storage or transferred
22  * to a different JVM for distributable session support.
23  * @author $Author: alex $
24  * @version $Revision: 1.3 $
25  */

26 final class WebManSession
27     implements HttpSession JavaDoc, Serializable JavaDoc {
28
29     private ServletContext JavaDoc context;
30     
31     /**
32      * Construct a new Session associated with the specified Manager.
33      *
34      * @param manager The manager with which this Session is associated
35      */

36     WebManSession(ServletContext JavaDoc _context)
37     {
38         context = _context;
39     }
40
41     /**
42         2.3
43     */

44     public ServletContext JavaDoc getServletContext()
45     {
46         return context;
47     }
48
49     /**
50      * The collection of user data attributes associated with this Session.
51      */

52     private Hashtable JavaDoc attributes = new Hashtable JavaDoc();
53
54
55     /**
56      * The time this session was created, in milliseconds since midnight,
57      * January 1, 1970 GMT.
58      */

59     private long creationTime;
60
61
62     /**
63      * The session identifier of this Session.
64      */

65     private String JavaDoc id = null;
66
67
68     /**
69      * Descriptive information describing this Session implementation.
70      */

71     private static final String JavaDoc INFO = "StandardSession/1.0";
72
73
74     /**
75      * The last accessed time for this Session.
76      */

77     private long lastAccessedTime = creationTime;
78
79
80     /**
81      * The maximum time interval, in seconds, between client requests before
82      * the servlet container may invalidate this session. A negative time
83      * indicates that the session should never time out.
84      */

85     private int maxInactiveInterval = -1;
86
87
88     /**
89      * Flag indicating whether this session is new or not.
90      */

91     private boolean isNew = true;
92
93
94     /**
95      * Flag indicating whether this session is valid or not.
96      */

97     private boolean isValid = false;
98
99
100     /**
101      * The HTTP session context associated with this session.
102      */

103     private static HttpSessionContext JavaDoc sessionContext = null;
104
105
106     /**
107      * The current accessed time for this session.
108      */

109     private long thisAccessedTime = creationTime;
110
111
112     // ----------------------------------------------------- Session Properties
113

114
115     /**
116      * Set the creation time for this session. This method is called by the
117      * Manager when an existing Session instance is reused.
118      *
119      * @param time The new creation time
120      */

121     public void setCreationTime(long time) {
122
123     this.creationTime = time;
124     this.lastAccessedTime = time;
125     this.thisAccessedTime = time;
126
127     }
128
129
130     /**
131      * Return the session identifier for this session.
132      */

133     public String JavaDoc getId() {
134
135     return (this.id);
136
137     }
138
139
140     /**
141      * Set the session identifier for this session.
142      *
143      * @param id The new session identifier
144      */

145     public void setId(String JavaDoc id) {
146
147     this.id = id;
148
149     }
150
151
152     /**
153      * Return descriptive information about this Session implementation and
154      * the corresponding version number, in the format
155      * <code>&lt;description&gt;/&lt;version&gt;</code>.
156      */

157     public String JavaDoc getInfo()
158     {
159         return INFO;
160     }
161
162
163     /**
164      * Return the last time the client sent a request associated with this
165      * session, as the number of milliseconds since midnight, January 1, 1970
166      * GMT. Actions that your application takes, such as getting or setting
167      * a value associated with the session, do not affect the access time.
168      */

169     public long getLastAccessedTime() {
170
171     return (this.lastAccessedTime);
172
173     }
174
175
176
177     /**
178      * Return the maximum time interval, in seconds, between client requests
179      * before the servlet container will invalidate the session. A negative
180      * time indicates that the session should never time out.
181      *
182      * @exception IllegalStateException if this method is called on
183      * an invalidated session
184      */

185     public int getMaxInactiveInterval() {
186
187
188     return (this.maxInactiveInterval);
189
190     }
191
192
193     /**
194      * Set the maximum time interval, in seconds, between client requests
195      * before the servlet container will invalidate the session. A negative
196      * time indicates that the session should never time out.
197      *
198      * @param interval The new maximum interval
199      */

200     public void setMaxInactiveInterval(int interval) {
201
202     this.maxInactiveInterval = interval;
203
204     }
205
206
207     /**
208      * Return the <code>HttpSession</code> for which this object
209      * is the facade.
210      */

211     public HttpSession JavaDoc getSession() {
212
213     return ((HttpSession JavaDoc) this);
214
215     }
216
217
218     // ------------------------------------------------- Session Public Methods
219

220
221     /**
222      * Update the accessed time information for this session. This method
223      * should be called by the context when a request comes in for a particular
224      * session, even if the application does not reference it.
225      */

226     public void access() {
227
228     this.lastAccessedTime = this.thisAccessedTime;
229     this.thisAccessedTime = System.currentTimeMillis();
230     this.isNew=false;
231     }
232
233
234     /**
235      * Perform the internal processing required to invalidate this session,
236      * without triggering an exception if the session has already expired.
237      */

238     public void expire() {
239
240
241     // Unbind any objects associated with this session
242
Vector JavaDoc results = new Vector JavaDoc();
243     Enumeration JavaDoc attrs = getAttributeNames();
244     while (attrs.hasMoreElements()) {
245         String JavaDoc attr = (String JavaDoc) attrs.nextElement();
246         results.addElement(attr);
247     }
248     Enumeration JavaDoc names = results.elements();
249     while (names.hasMoreElements()) {
250         String JavaDoc name = (String JavaDoc) names.nextElement();
251         removeAttribute(name);
252     }
253
254     // Mark this session as invalid
255
setValid(false);
256
257     }
258
259
260     /**
261      * Release all object references, and initialize instance variables, in
262      * preparation for reuse of this object.
263      */

264     public void recycle()
265     {
266
267         // Reset the instance variables associated with this Session
268
attributes.clear();
269         creationTime = 0;
270         id = null;
271         lastAccessedTime = 0;
272         maxInactiveInterval = -1;
273         isNew = true;
274         isValid = false;
275
276     }
277
278
279     // ------------------------------------------------ Session Package Methods
280

281
282     /**
283      * Return the <code>isValid</code> flag for this session.
284      */

285     boolean isValid() {
286
287     return (this.isValid);
288
289     }
290
291
292     /**
293      * Set the <code>isNew</code> flag for this session.
294      *
295      * @param isNew The new value for the <code>isNew</code> flag
296      */

297     void setNew(boolean isNew) {
298
299     this.isNew = isNew;
300
301     }
302
303
304     /**
305      * Set the <code>isValid</code> flag for this session.
306      *
307      * @param isValid The new value for the <code>isValid</code> flag
308      */

309     void setValid(boolean isValid) {
310
311     this.isValid = isValid;
312     }
313
314
315     // ------------------------------------------------- HttpSession Properties
316

317
318     /**
319      * Return the time when this session was created, in milliseconds since
320      * midnight, January 1, 1970 GMT.
321      *
322      * @exception IllegalStateException if this method is called on an
323      * invalidated session
324      */

325     public long getCreationTime() {
326
327     return (this.creationTime);
328
329     }
330
331
332     /**
333      * Return the session context with which this session is associated.
334      *
335      * @deprecated As of Version 2.1, this method is deprecated and has no
336      * replacement. It will be removed in a future version of the
337      * Java Servlet API.
338      */

339     public HttpSessionContext JavaDoc getSessionContext() {
340
341     if (sessionContext == null)
342         sessionContext = new SessionContextImpl();
343     return (sessionContext);
344
345     }
346
347
348     // ----------------------------------------------HttpSession Public Methods
349

350
351     /**
352      * Return the object bound with the specified name in this session, or
353      * <code>null</code> if no object is bound with that name.
354      *
355      * @param name Name of the attribute to be returned
356      *
357      * @exception IllegalStateException if this method is called on an
358      * invalidated session
359      */

360     public Object JavaDoc getAttribute(String JavaDoc name) {
361
362     return (attributes.get(name));
363
364     }
365
366
367     /**
368      * Return an <code>Enumeration</code> of <code>String</code> objects
369      * containing the names of the objects bound to this session.
370      *
371      * @exception IllegalStateException if this method is called on an
372      * invalidated session
373      */

374     public Enumeration JavaDoc getAttributeNames()
375     {
376         return (attributes.keys());
377     }
378
379
380     /**
381      * Return the object bound with the specified name in this session, or
382      * <code>null</code> if no object is bound with that name.
383      *
384      * @param name Name of the value to be returned
385      *
386      * @exception IllegalStateException if this method is called on an
387      * invalidated session
388      *
389      * @deprecated As of Version 2.2, this method is replaced by
390      * <code>getAttribute()</code>
391      */

392     public Object JavaDoc getValue(String JavaDoc name) {
393
394     return (getAttribute(name));
395
396     }
397
398
399     /**
400      * Return the set of names of objects bound to this session. If there
401      * are no such objects, a zero-length array is returned.
402      *
403      * @exception IllegalStateException if this method is called on an
404      * invalidated session
405      *
406      * @deprecated As of Version 2.2, this method is replaced by
407      * <code>getAttributeNames()</code>
408      */

409     public String JavaDoc[] getValueNames() {
410
411
412     Vector JavaDoc results = new Vector JavaDoc();
413     Enumeration JavaDoc attrs = getAttributeNames();
414     while (attrs.hasMoreElements()) {
415         String JavaDoc attr = (String JavaDoc) attrs.nextElement();
416         results.addElement(attr);
417     }
418     String JavaDoc names[] = new String JavaDoc[results.size()];
419     for (int i = 0; i < names.length; i++)
420         names[i] = (String JavaDoc) results.elementAt(i);
421     return (names);
422
423     }
424
425
426     /**
427      * Invalidates this session and unbinds any objects bound to it.
428      *
429      * @exception IllegalStateException if this method is called on
430      * an invalidated session
431      */

432     public void invalidate() {
433
434     // Cause this session to expire
435
expire();
436
437     }
438
439
440     /**
441      * Return <code>true</code> if the client does not yet know about the
442      * session, or if the client chooses not to join the session. For
443      * example, if the server used only cookie-based sessions, and the client
444      * has disabled the use of cookies, then a session would be new on each
445      * request.
446      *
447      * @exception IllegalStateException if this method is called on an
448      * invalidated session
449      */

450     public boolean isNew() {
451     return (this.isNew);
452
453     }
454
455
456     /**
457      * Bind an object to this session, using the specified name. If an object
458      * of the same name is already bound to this session, the object is
459      * replaced.
460      * <p>
461      * After this method executes, and if the object implements
462      * <code>HttpSessionBindingListener</code>, the container calls
463      * <code>valueBound()</code> on the object.
464      *
465      * @param name Name to which the object is bound, cannot be null
466      * @param value Object to be bound, cannot be null
467      *
468      * @exception IllegalStateException if this method is called on an
469      * invalidated session
470      *
471      * @deprecated As of Version 2.2, this method is replaced by
472      * <code>setAttribute()</code>
473      */

474     public void putValue(String JavaDoc name, Object JavaDoc value) {
475
476     setAttribute(name, value);
477
478     }
479
480
481     /**
482      * Remove the object bound with the specified name from this session. If
483      * the session does not have an object bound with this name, this method
484      * does nothing.
485      * <p>
486      * After this method executes, and if the object implements
487      * <code>HttpSessionBindingListener</code>, the container calls
488      * <code>valueUnbound()</code> on the object.
489      *
490      * @param name Name of the object to remove from this session.
491      *
492      * @exception IllegalStateException if this method is called on an
493      * invalidated session
494      */

495     public void removeAttribute(String JavaDoc name) {
496
497
498     synchronized (attributes) {
499         Object JavaDoc object = attributes.get(name);
500         if (object == null)
501         return;
502         attributes.remove(name);
503         if (object instanceof HttpSessionBindingListener JavaDoc) {
504         ((HttpSessionBindingListener JavaDoc) object).valueUnbound
505             (new HttpSessionBindingEvent JavaDoc((HttpSession JavaDoc) this, name));
506         }
507     }
508
509     }
510
511
512     /**
513      * Remove the object bound with the specified name from this session. If
514      * the session does not have an object bound with this name, this method
515      * does nothing.
516      * <p>
517      * After this method executes, and if the object implements
518      * <code>HttpSessionBindingListener</code>, the container calls
519      * <code>valueUnbound()</code> on the object.
520      *
521      * @param name Name of the object to remove from this session.
522      *
523      * @exception IllegalStateException if this method is called on an
524      * invalidated session
525      *
526      * @deprecated As of Version 2.2, this method is replaced by
527      * <code>removeAttribute()</code>
528      */

529     public void removeValue(String JavaDoc name) {
530
531     removeAttribute(name);
532
533     }
534
535
536     /**
537      * Bind an object to this session, using the specified name. If an object
538      * of the same name is already bound to this session, the object is
539      * replaced.
540      * <p>
541      * After this method executes, and if the object implements
542      * <code>HttpSessionBindingListener</code>, the container calls
543      * <code>valueBound()</code> on the object.
544      *
545      * @param name Name to which the object is bound, cannot be null
546      * @param value Object to be bound, cannot be null
547      *
548      * @exception IllegalArgumentException if an attempt is made to add a
549      * non-serializable object in an environment marked distributable.
550      * @exception IllegalStateException if this method is called on an
551      * invalidated session
552      */

553     public void setAttribute(String JavaDoc name, Object JavaDoc value)
554     {
555         // System.out.println("Set attribute: " + name + " , " + value + " " + this);
556
synchronized (attributes) {
557             removeAttribute(name);
558             attributes.put(name, value);
559             if (value instanceof HttpSessionBindingListener JavaDoc)
560             ((HttpSessionBindingListener JavaDoc) value).valueBound
561                 (new HttpSessionBindingEvent JavaDoc((HttpSession JavaDoc) this, name));
562         }
563
564     }
565
566
567     // -------------------------------------------- HttpSession Private Methods
568

569
570     /**
571      * Read a serialized version of this session object from the specified
572      * object input stream.
573      * <p>
574      * <b>IMPLEMENTATION NOTE</b>: The reference to the owning Manager
575      * is not restored by this method, and must be set explicitly.
576      *
577      * @param stream The input stream to read from
578      *
579      * @exception ClassNotFoundException if an unknown class is specified
580      * @exception IOException if an input/output error occurs
581      */

582     private void readObject(ObjectInputStream JavaDoc stream)
583         throws ClassNotFoundException JavaDoc, IOException JavaDoc {
584
585         // Deserialize the scalar instance variables (except Manager)
586
creationTime = ((Long JavaDoc) stream.readObject()).longValue();
587         id = (String JavaDoc) stream.readObject();
588         lastAccessedTime = ((Long JavaDoc) stream.readObject()).longValue();
589                 thisAccessedTime = ((Long JavaDoc) stream.readObject()).longValue();
590         maxInactiveInterval = ((Integer JavaDoc) stream.readObject()).intValue();
591         isNew = ((Boolean JavaDoc) stream.readObject()).booleanValue();
592         isValid = ((Boolean JavaDoc) stream.readObject()).booleanValue();
593
594         attributes = (Hashtable JavaDoc) stream.readObject();
595     }
596
597
598     /**
599      * Write a serialized version of this session object to the specified
600      * object output stream.
601      * <p>
602      * <b>IMPLEMENTATION NOTE</b>: The owning Manager will not be stored
603      * in the serialized representation of this Session. After calling
604      * <code>readObject()</code>, you must set the associated Manager
605      * explicitly.
606      * <p>
607      * <b>IMPLEMENTATION NOTE</b>: Any attribute that is not Serializable
608      * will be silently ignored. If you do not want any such attributes,
609      * be sure the <code>distributable</code> property of our associated
610      * Manager is set to <code>true</code>.
611      * <p>
612      * <b>IMPLEMENTATION NOTE</b>: If we can't serialize the object stored in
613      * the session, then check to see if it implements
614      * HttpSessionBindingListener and then call its
615      * valueUnbound method, allowing it to save its state
616      * correctly instead of just being lost into the etherworld
617      *
618      * @param stream The output stream to write to
619      *
620      * @exception IOException if an input/output error occurs
621      */

622     private void writeObject(ObjectOutputStream JavaDoc stream) throws IOException JavaDoc {
623
624         // Write the scalar instance variables (except Manager)
625
stream.writeObject(new Long JavaDoc(creationTime));
626         stream.writeObject(id);
627         stream.writeObject(new Long JavaDoc(lastAccessedTime));
628                 stream.writeObject(new Long JavaDoc(thisAccessedTime));
629         stream.writeObject(new Integer JavaDoc(maxInactiveInterval));
630         stream.writeObject(new Boolean JavaDoc(isNew));
631         stream.writeObject(new Boolean JavaDoc(isValid));
632
633         if (attributes.size() > 0) {
634             // Accumulate the names of serializable attributes
635
Hashtable JavaDoc results = new Hashtable JavaDoc(attributes.size());
636         
637             for (Enumeration JavaDoc e = attributes.keys(); e.hasMoreElements() ; ) {
638                 String JavaDoc key = (String JavaDoc) e.nextElement();
639                 Object JavaDoc value = attributes.get(key);
640                 if (value instanceof Serializable JavaDoc) {
641                     results.put(key, value);
642                 }
643                 // if we can't serialize the object stored in
644
// the session, then check to see if it implements
645
// HttpSessionBindingListener and then call its
646
// valueUnbound method, allowing it to save its state
647
// correctly instead of just being lost into the etherworld
648
else if (value instanceof HttpSessionBindingListener JavaDoc ) {
649                     try {
650                         ((HttpSessionBindingListener JavaDoc)value)
651                         .valueUnbound(new HttpSessionBindingEvent JavaDoc(this, key));
652                     } catch (Exception JavaDoc f)
653                     {
654                         // JTest Mist
655
f.hashCode();
656                         // ignored
657
}
658                 }
659             }
660             stream.writeObject(results);
661         } else {
662             stream.writeObject(new Hashtable JavaDoc());
663         }
664     }
665 }
666
667  class SessionContextImpl implements HttpSessionContext JavaDoc {
668
669     /**
670      *
671      * @deprecated
672      */

673     
674     public HttpSession JavaDoc getSession(String JavaDoc sessionId) {
675         return null;
676     }
677
678     /**
679      *
680      * @deprecated
681      */

682
683     public Enumeration JavaDoc getIds() {
684         // cheap hack to get an empty enum
685
Vector JavaDoc v = new Vector JavaDoc();
686
687         return v.elements();
688     }
689 }
690
Popular Tags