Annotation of java/classes/org/w3c/jigsaw/servlet/JigsawHttpSessionContext.java, revision 1.16
1.1 bmahe 1: // JigsawHttpSessionContext.java
1.16 ! ylafon 2: // $Id: JigsawHttpSessionContext.java,v 1.15 2007/02/11 10:56:04 ylafon 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:
1.14 ylafon 8: import java.util.Date;
9: import java.util.Enumeration;
10: import java.util.Hashtable;
1.16 ! ylafon 11: import javax.servlet.http.HttpSession;
! 12: import javax.servlet.http.HttpSessionContext;
! 13: import org.w3c.jigsaw.http.httpd;
1.14 ylafon 14: import org.w3c.util.PropertyMonitoring;
15:
1.1 bmahe 16: /**
1.16 ! ylafon 17: * @author Benoît Mahé (bmahe@w3.org)
! 18: * @version $Revision: 1.15 $
1.11 bmahe 19: * @deprecated since jsdk2.1
1.1 bmahe 20: */
1.16 ! ylafon 21: @Deprecated
1.9 bmahe 22: public class JigsawHttpSessionContext implements HttpSessionContext,
1.16 ! ylafon 23: PropertyMonitoring {
1.14 ylafon 24:
1.4 bmahe 25: class SessionSweeper extends Thread {
26:
1.16 ! ylafon 27: int delay;
! 28: JigsawHttpSessionContext ctxt = null;
1.4 bmahe 29:
1.16 ! ylafon 30: public void run() {
! 31: while (true) {
! 32: try {
! 33: sleep(delay);
! 34: } catch (InterruptedException ex) {
! 35: }
! 36: ctxt.sweepSession();
! 37: }
! 38: }
! 39:
! 40: void setDelay(int delay) {
! 41: this.delay = delay;
! 42: }
! 43:
! 44: SessionSweeper(JigsawHttpSessionContext ctxt, int delay) {
! 45: super("SessionSweeper");
! 46: this.delay = delay;
! 47: this.ctxt = ctxt;
! 48: setPriority(Thread.MIN_PRIORITY);
! 49: setDaemon(true);
! 50: this.start();
! 51: }
1.4 bmahe 52:
53: }
1.14 ylafon 54:
1.16 ! ylafon 55: private Hashtable<String,JigsawHttpSession> sessions;
1.1 bmahe 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: */
1.16 ! ylafon 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;
1.9 bmahe 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.16 ! ylafon 82: long now = System.currentTimeMillis();
! 83: oldestIdleSession = null;
! 84: for (JigsawHttpSession session : sessions.values()) {
! 85: long interval = now - session.getLastAccessedTime();
! 86: int maxinactiveinterval = session.getMaxInactiveInterval() * 1000;
! 87: long maxinterval =
! 88: (maxinactiveinterval > 0) ? maxinactiveinterval : maxidle;
! 89: if (interval > maxinterval)
! 90: session.invalidate();
! 91: else {
! 92: if (oldestIdleSession != null) {
! 93: if (oldestIdleSession.getLastAccessedTime() >
! 94: session.getLastAccessedTime())
! 95: oldestIdleSession = session;
! 96: } else {
! 97: oldestIdleSession = session;
! 98: }
! 99: }
! 100: }
1.3 bmahe 101: }
102:
1.4 bmahe 103: protected synchronized void removeOldestIdleSession() {
1.16 ! ylafon 104: if (oldestIdleSession != null) {
! 105: oldestIdleSession.invalidate();
! 106: oldestIdleSession = null;
! 107: }
1.3 bmahe 108: }
1.1 bmahe 109:
1.10 bmahe 110: /**
1.16 ! ylafon 111: * Returns an enumeration of all of the session IDs in this context.
! 112: *
1.10 bmahe 113: * @return an enumeration of all session IDs in this context.
1.11 bmahe 114: * @deprecated since jsdk2.1
1.10 bmahe 115: */
1.16 ! ylafon 116: @Deprecated
! 117: public Enumeration<String> getIds() {
1.1 bmahe 118: return sessions.keys();
119: }
120:
1.10 bmahe 121: /**
122: * Returns the session bound to the specified session ID.
1.16 ! ylafon 123: *
! 124: * @param sessionId - the ID of a particular session object
! 125: * @return the session. Returns null if the session ID does
! 126: * not refer to a valid session.
1.11 bmahe 127: * @deprecated since jsdk2.1
1.10 bmahe 128: */
1.16 ! ylafon 129: @Deprecated
! 130: public HttpSession getSession(String sessionId) {
! 131: return sessions.get(sessionId);
1.1 bmahe 132: }
133:
1.10 bmahe 134: /**
135: * Add a session in this context.
1.16 ! ylafon 136: *
1.10 bmahe 137: * @param session - The JigsawHttpSession to add.
138: * @return The session ID.
139: */
140: protected synchronized String addSession(JigsawHttpSession session) {
1.16 ! ylafon 141: if (sessionCount >= maxsession)
! 142: removeOldestIdleSession();
! 143: String id = getNewSessionId();
! 144: sessions.put(id, session);
! 145: sessionCount++;
! 146: return id;
1.1 bmahe 147: }
148:
1.10 bmahe 149: /**
150: * Remove a session of this session context.
1.16 ! ylafon 151: *
1.10 bmahe 152: * @param id - The session ID.
153: */
154: protected void removeSession(String id) {
1.16 ! ylafon 155: sessions.remove(id);
! 156: sessionCount--;
1.1 bmahe 157: }
158:
1.10 bmahe 159: private String getNewSessionId() {
1.16 ! ylafon 160: return "J" + String.valueOf(new Date().hashCode()) + "-" + (sessionNb++);
1.1 bmahe 161: }
162:
1.16 ! ylafon 163: public JigsawHttpSessionContext(httpd server, ServletProps props) {
! 164: this.props = props;
! 165: this.sessions = new Hashtable<String,JigsawHttpSession>(3);
! 166: this.maxidle = props.getSessionsMaxIdleTime();
! 167: this.maxsession = props.getMaxSessionsNumber();
! 168: this.sweeper = new SessionSweeper(this, props.getSessionsSweepDelay());
! 169: server.getProperties().registerObserver(this);
1.1 bmahe 170: }
171:
172: }
Webmaster