Annotation of java/classes/org/w3c/jigsaw/servlet/JigsawHttpSession.java, revision 1.6

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

Webmaster