Annotation of java/classes/org/w3c/jigsaw/servlet/JigsawHttpSessionContext.java, revision 1.12

1.1       bmahe       1: // JigsawHttpSessionContext.java
1.12    ! bmahe       2: // $Id: JigsawHttpSessionContext.java,v 1.11 1999/03/17 18:01:01 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: 
1.9       bmahe      14: import org.w3c.util.*;
                     15: import org.w3c.jigsaw.http.*;
                     16: 
1.1       bmahe      17: /**
1.12    ! bmahe      18:  * @version $Revision: 1.11 $
1.1       bmahe      19:  * @author  Benoît Mahé (bmahe@w3.org)
1.11      bmahe      20:  * @deprecated since jsdk2.1
1.1       bmahe      21:  */
1.9       bmahe      22: public class JigsawHttpSessionContext implements HttpSessionContext,
                     23:                                                 PropertyMonitoring
                     24: {
1.4       bmahe      25:  
                     26:     class SessionSweeper extends Thread {
                     27: 
1.8       bmahe      28:        int delay;
1.4       bmahe      29:        JigsawHttpSessionContext ctxt = null;
                     30: 
                     31:        public void run() {
                     32:            while (true) {
                     33:                try {
1.7       bmahe      34:                    sleep(delay); 
1.4       bmahe      35:                } catch (InterruptedException ex) {}
                     36:                ctxt.sweepSession();
                     37:            }
                     38:        }
                     39: 
1.9       bmahe      40:        void setDelay(int delay) {
                     41:            this.delay = delay;
                     42:        }
                     43: 
1.7       bmahe      44:        SessionSweeper(JigsawHttpSessionContext ctxt, int delay) {
1.4       bmahe      45:            super("SessionSweeper");
1.7       bmahe      46:            this.delay = delay;
1.4       bmahe      47:            this.ctxt = ctxt;
                     48:            setPriority(Thread.MIN_PRIORITY);
                     49:            setDaemon(true);
                     50:            this.start();
                     51:        }
                     52: 
                     53:     }
                     54:    
1.1       bmahe      55:     private Hashtable sessions;
                     56:     private int sessionNb = 0;
1.3       bmahe      57:     private int sessionCount = 0;
1.8       bmahe      58:     private int maxsession;
                     59:     private long maxidle;
1.3       bmahe      60:     private JigsawHttpSession oldestIdleSession = null;
1.9       bmahe      61:     private SessionSweeper sweeper = null;
                     62:     private ServletProps props = null;
                     63: 
                     64:     /**
                     65:      * PropertyMonitoring implementation.
                     66:      */
                     67:     public boolean propertyChanged (String name) {
                     68:        if (name.equals(ServletProps.SERVLET_SESSION_IDLE)) {
                     69:            this.maxidle = props.getSessionsMaxIdleTime();
                     70:        } else if (name.equals(ServletProps.SERVLET_MAX_SESSION)) {
                     71:            this.maxsession = props.getMaxSessionsNumber();
                     72:        } else if (name.equals(ServletProps.SERVLET_SESSION_SWEEP)) {
                     73:            sweeper.setDelay(props.getSessionsSweepDelay());
                     74:        }
                     75:        return true;
                     76:     }
1.3       bmahe      77: 
1.9       bmahe      78:     /**
                     79:      * Remove sessions with idle time > max idle time
                     80:      */
1.4       bmahe      81:     protected synchronized void sweepSession() {
1.3       bmahe      82:        long now = System.currentTimeMillis();
                     83:        Enumeration enum = sessions.keys();
1.4       bmahe      84:        oldestIdleSession = null;
1.3       bmahe      85:        while (enum.hasMoreElements()) {
                     86:            JigsawHttpSession session = 
                     87:                (JigsawHttpSession) sessions.get(enum.nextElement());
1.11      bmahe      88:            long interval = now - session.getLastAccessedTime();
1.12    ! bmahe      89:            System.out.println("Idle since : "+interval);
        !            90:            int maxinactiveinterval = session.getMaxInactiveInterval()*1000;
        !            91:            System.out.println("Max inactive interval : "+maxinactiveinterval);
1.11      bmahe      92:            long maxinterval = 
                     93:                (maxinactiveinterval > 0) ? maxinactiveinterval : maxidle;
1.12    ! bmahe      94:            System.out.println("Max interval : "+maxinterval);
1.11      bmahe      95:            if (interval > maxinterval)
1.3       bmahe      96:                session.invalidate();
                     97:            else {
                     98:                if (oldestIdleSession != null) {
                     99:                    if (oldestIdleSession.getLastAccessedTime() > 
                    100:                        session.getLastAccessedTime())
                    101:                        oldestIdleSession = session;
                    102:                } else {
                    103:                    oldestIdleSession = session;
                    104:                }
                    105:            }
                    106:        }
                    107:     }
                    108: 
1.4       bmahe     109:     protected synchronized void removeOldestIdleSession() {
1.3       bmahe     110:        if (oldestIdleSession != null) {
                    111:            oldestIdleSession.invalidate();
                    112:            oldestIdleSession = null;
                    113:        }
                    114:     }
1.1       bmahe     115: 
1.10      bmahe     116:     /**
                    117:      * Returns an enumeration of all of the session IDs in this context. 
                    118:      * @return an enumeration of all session IDs in this context.
1.11      bmahe     119:      * @deprecated since jsdk2.1
1.10      bmahe     120:      */
1.1       bmahe     121:     public Enumeration getIds()
                    122:     {
                    123:         return sessions.keys();
                    124:     }
                    125: 
1.10      bmahe     126:     /**
                    127:      * Returns the session bound to the specified session ID.
                    128:      * @param sessionID - the ID of a particular session object 
                    129:      * @return the session. Returns null if the session ID does 
                    130:      * not refer to a valid session. 
1.11      bmahe     131:      * @deprecated since jsdk2.1
1.10      bmahe     132:      */
1.1       bmahe     133:     public  HttpSession getSession(String sessionId) {
                    134:        return (HttpSession)sessions.get(sessionId);
                    135:     }
                    136: 
1.10      bmahe     137:     /**
                    138:      * Add a session in this context.
                    139:      * @param session - The JigsawHttpSession to add.
                    140:      * @return The session ID.
                    141:      */
                    142:     protected synchronized String addSession(JigsawHttpSession session) {
1.3       bmahe     143:        if (sessionCount >= maxsession)
                    144:            removeOldestIdleSession();
1.1       bmahe     145:        String id = getNewSessionId();
                    146:        sessions.put(id, session);
1.3       bmahe     147:        sessionCount++;
1.1       bmahe     148:        return id;
                    149:     }
                    150: 
1.10      bmahe     151:     /**
                    152:      * Remove a session of this session context.
                    153:      * @param id - The session ID.
                    154:      */
                    155:     protected void removeSession(String id) {
1.1       bmahe     156:        sessions.remove(id);
1.3       bmahe     157:        sessionCount--;
1.1       bmahe     158:     }
                    159: 
1.10      bmahe     160:     private String getNewSessionId() {
1.2       bmahe     161:        return "J"+String.valueOf(new Date().hashCode())+"-"+(sessionNb++);
1.1       bmahe     162:     }
                    163: 
1.9       bmahe     164:     public JigsawHttpSessionContext(httpd server, ServletProps props) 
1.7       bmahe     165:     {
1.9       bmahe     166:        this.props      = props;
1.3       bmahe     167:        this.sessions   = new Hashtable(3);
1.9       bmahe     168:        this.maxidle    = props.getSessionsMaxIdleTime();
                    169:        this.maxsession = props.getMaxSessionsNumber();
                    170:        this.sweeper = new SessionSweeper(this, props.getSessionsSweepDelay());
                    171:        server.getProperties().registerObserver(this);
1.1       bmahe     172:     }
                    173: 
                    174: }

Webmaster