Annotation of java/classes/org/w3c/jigsaw/servlet/JigsawHttpSession.java, revision 1.5
1.1 bmahe 1: // JigsawHttpSession.java
1.5 ! bmahe 2: // $Id: JigsawHttpSession.java,v 1.4 1998/06/04 16:20:31 bmahe Exp $
1.1 bmahe 3: // (c) COPYRIGHT MIT and INRIA, 1998.
4: // Please first read the full copyright statement in file COPYRIGHT.html
5:
6: package org.w3c.jigsaw.servlet;
7:
8: import java.io.*;
9: import javax.servlet.*;
10: import javax.servlet.http.*;
11: import java.net.*;
12: import java.util.*;
13:
14: /**
1.5 ! bmahe 15: * @version $Revision: 1.4 $
1.1 bmahe 16: * @author Benoît Mahé (bmahe@w3.org)
17: */
18: public class JigsawHttpSession implements HttpSession {
19:
20: private JigsawHttpSessionContext sc = null;
21:
22: private String id = null;
23:
24: private long creationTime = -1;
25: private long lastAccessedTime = -1;
26:
27: private boolean isValid = false;
28: private boolean isNew = false;
29:
30: private Cookie cookie = null;
31:
32: private Hashtable values = null;
33:
1.5 ! bmahe 34: /**
! 35: * Returns the identifier assigned to this session. An HttpSession's
! 36: * identifier is a unique string that is created and maintained by
! 37: * HttpSessionContext.
! 38: * @return the identifier assigned to this session
! 39: * @exception IllegalStateException if an attempt is made to access
! 40: * session data after the session has been invalidated
! 41: */
1.1 bmahe 42: public String getId() {
43: return id;
44: }
45:
1.5 ! bmahe 46: /**
! 47: * Returns the context in which this session is bound.
! 48: * @return the context in which this session is bound.
! 49: * @exception IllegalStateException if an attempt is made to access
! 50: * session data after the session has been invalidated
! 51: */
1.1 bmahe 52: public HttpSessionContext getSessionContext() {
53: return sc;
54: }
55:
1.5 ! bmahe 56: /**
! 57: * Returns the time at which this session representation was created,
! 58: * in milliseconds since midnight, January 1, 1970 UTC.
! 59: * @return the time when the session was created
! 60: * @exception IllegalStateException if an attempt is made to access
! 61: * session data after the session has been invalidated
! 62: */
1.1 bmahe 63: public long getCreationTime() {
64: return creationTime;
65: }
66:
1.5 ! bmahe 67: /**
! 68: * Returns the last time the client sent a request carrying the identifier
! 69: * assigned to the session. Time is expressed as milliseconds
! 70: * since midnight, January 1, 1970 UTC. Application level operations,
! 71: * such as getting or setting a value associated with the session,
! 72: * does not affect the access time.
! 73: * @return the last time the client sent a request carrying the identifier
! 74: * assigned to the session
! 75: * @exception IllegalStateException if an attempt is made to access
! 76: * session data after the session has been invalidated
! 77: */
1.1 bmahe 78: public long getLastAccessedTime() {
79: return lastAccessedTime;
80: }
81:
1.5 ! bmahe 82: protected void setLastAccessedTime() {
1.1 bmahe 83: lastAccessedTime = System.currentTimeMillis();
84: }
85:
1.5 ! bmahe 86: /**
! 87: * Causes this representation of the session to be invalidated and removed
! 88: * from its context.
! 89: * @exception IllegalStateException if an attempt is made to access
! 90: * session data after the session has been invalidated
! 91: */
1.1 bmahe 92: public void invalidate() {
93: isValid = false;
1.3 bmahe 94: sc.removeSession(id);
1.1 bmahe 95: }
96:
1.5 ! bmahe 97: /**
! 98: * Returns the object bound to the given name in the session's application
! 99: * layer data. Returns null if there is no such binding.
! 100: * @param name - the name of the binding to find
! 101: * @return the value bound to that name, or null if the binding does
! 102: * not exist.
! 103: * @exception IllegalStateException if an attempt is made to access
! 104: * session data after the session has been invalidated
! 105: */
1.1 bmahe 106: public Object getValue(String name) {
107: if (!isValid)
108: throw new IllegalStateException("Invalid session");
109: return values.get(name);
110: }
111:
1.5 ! bmahe 112: /**
! 113: * Binds the specified object into the session's application layer data
! 114: * with the given name. Any existing binding with the same name
! 115: * is replaced. New (or existing) values that implement the
! 116: * HttpSessionBindingListener interface will call its valueBound() method.
! 117: * @param name - the name to which the data object will be bound.
! 118: * This parameter cannot be null.
! 119: * @param value - the data object to be bound. This parameter cannot
! 120: * be null.
! 121: * @exception IllegalStateException if an attempt is made to access
! 122: * session data after the session has been invalidated
! 123: */
1.1 bmahe 124: public void putValue(String name, Object value)
125: {
126: if (!isValid)
127: throw new IllegalStateException("Invalid session");
128: removeValue(name);
129: values.put(name, value);
130: if (value instanceof HttpSessionBindingListener)
131: valueBound((HttpSessionBindingListener)value, name);
132: }
133:
1.5 ! bmahe 134: /**
! 135: * Removes the object bound to the given name in the session's application
! 136: * layer data. Does nothing if there is no object bound to the
! 137: * given name. The value that implements the HttpSessionBindingListener
! 138: * interface will call its valueUnbound() method.
! 139: * @param name - the name of the object to remove
! 140: * @exception IllegalStateException if an attempt is made to access
! 141: * session data after the session has been invalidated
! 142: */
1.1 bmahe 143: public void removeValue(String name) {
144: if (!isValid)
145: throw new IllegalStateException("Invalid session");
146: Object value = values.get(name);
147: if (value != null) {
148: values.remove(name);
149: if (value instanceof HttpSessionBindingListener)
150: valueUnbound((HttpSessionBindingListener)value, name);
151: }
152: }
153:
154: protected void valueBound(HttpSessionBindingListener value, String name)
155: {
156: value.valueBound(new HttpSessionBindingEvent(this, name));
157: }
158:
159: protected void valueUnbound(HttpSessionBindingListener value, String name)
160: {
161: value.valueUnbound(new HttpSessionBindingEvent(this, name));
162: }
163:
1.5 ! bmahe 164: /**
! 165: * Returns an array of the names of all the application layer data objects
! 166: * bound into the session. For example, if you want to delete
! 167: * all of the data objects bound into the session, use this method to
! 168: * obtain their names.
! 169: * @return an array containing the names of all of the application layer
! 170: * data objects bound into the session
! 171: * @exception IllegalStateException if an attempt is made to access
! 172: * session data after the session has been invalidated
! 173: */
1.1 bmahe 174: public String[] getValueNames() {
175: if (!isValid)
176: throw new IllegalStateException("Invalid session");
177: String names[] = new String[values.size()];
178: Enumeration enum = values.keys();
179: int i = 0;
180: while (enum.hasMoreElements())
181: names[i++] = (String)enum.nextElement();
182: return names;
183: }
184:
1.5 ! bmahe 185: /**
! 186: * A session is considered to be "new" if it has been created by the
! 187: * server, but the client has not yet acknowledged joining the
! 188: * session. For example, if the server supported only cookie-based
! 189: * sessions and the client had completely disabled the use of
! 190: * cookies, then calls to HttpServletRequest.getSession() would always
! 191: * return "new" sessions.
! 192: * @return true if the session has been created by the server but the
! 193: * client has not yet acknowledged joining the session; false otherwise
! 194: * @exception IllegalStateException if an attempt is made to access
! 195: * session data after the session has been invalidated
! 196: */
1.1 bmahe 197: public boolean isNew() {
198: return isNew;
1.2 bmahe 199: }
200:
1.4 bmahe 201: protected void setNoMoreNew() {
1.2 bmahe 202: isNew = false;
1.1 bmahe 203: }
204:
1.5 ! bmahe 205: protected boolean isValid() {
1.1 bmahe 206: return isValid;
207: }
208:
1.5 ! bmahe 209: protected Cookie getCookie() {
1.1 bmahe 210: return cookie;
211: }
212:
213: public JigsawHttpSession(JigsawHttpSessionContext context, Cookie cookie) {
214: this.values = new Hashtable();
215: this.creationTime = System.currentTimeMillis();
216: this.lastAccessedTime = creationTime;
217: this.sc = context;
218: this.id = context.addSession(this);
219: this.cookie = cookie;
220: cookie.setValue(this.id);
221: isValid = true;
222: isNew = true;
223: }
224:
225: }
Webmaster