001: // JigsawHttpSessionContext.java
002: // $Id: JigsawHttpSessionContext.java,v 1.15 2007/02/11 10:56:04 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1998.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigsaw.servlet;
007:
008: import javax.servlet.http.HttpSession;
009: import javax.servlet.http.HttpSessionContext;
010:
011: import java.util.Date;
012: import java.util.Enumeration;
013: import java.util.Hashtable;
014:
015: import org.w3c.util.ObservableProperties;
016: import org.w3c.util.PropertyMonitoring;
017:
018: import org.w3c.jigsaw.http.httpd;
019:
020: /**
021: * @version $Revision: 1.15 $
022: * @author Benoît Mahé (bmahe@w3.org)
023: * @deprecated since jsdk2.1
024: */
025: public class JigsawHttpSessionContext implements HttpSessionContext,
026: PropertyMonitoring {
027:
028: class SessionSweeper extends Thread {
029:
030: int delay;
031: JigsawHttpSessionContext ctxt = null;
032:
033: public void run() {
034: while (true) {
035: try {
036: sleep(delay);
037: } catch (InterruptedException ex) {
038: }
039: ctxt.sweepSession();
040: }
041: }
042:
043: void setDelay(int delay) {
044: this .delay = delay;
045: }
046:
047: SessionSweeper(JigsawHttpSessionContext ctxt, int delay) {
048: super ("SessionSweeper");
049: this .delay = delay;
050: this .ctxt = ctxt;
051: setPriority(Thread.MIN_PRIORITY);
052: setDaemon(true);
053: this .start();
054: }
055:
056: }
057:
058: private Hashtable sessions;
059: private int sessionNb = 0;
060: private int sessionCount = 0;
061: private int maxsession;
062: private long maxidle;
063: private JigsawHttpSession oldestIdleSession = null;
064: private SessionSweeper sweeper = null;
065: private ServletProps props = null;
066:
067: /**
068: * PropertyMonitoring implementation.
069: */
070: public boolean propertyChanged(String name) {
071: if (name.equals(ServletProps.SERVLET_SESSION_IDLE)) {
072: this .maxidle = props.getSessionsMaxIdleTime();
073: } else if (name.equals(ServletProps.SERVLET_MAX_SESSION)) {
074: this .maxsession = props.getMaxSessionsNumber();
075: } else if (name.equals(ServletProps.SERVLET_SESSION_SWEEP)) {
076: sweeper.setDelay(props.getSessionsSweepDelay());
077: }
078: return true;
079: }
080:
081: /**
082: * Remove sessions with idle time > max idle time
083: */
084: protected synchronized void sweepSession() {
085: long now = System.currentTimeMillis();
086: Enumeration e = sessions.keys();
087: oldestIdleSession = null;
088: while (e.hasMoreElements()) {
089: JigsawHttpSession session = (JigsawHttpSession) sessions
090: .get(e.nextElement());
091: long interval = now - session.getLastAccessedTime();
092: int maxinactiveinterval = session.getMaxInactiveInterval() * 1000;
093: long maxinterval = (maxinactiveinterval > 0) ? maxinactiveinterval
094: : maxidle;
095: if (interval > maxinterval)
096: session.invalidate();
097: else {
098: if (oldestIdleSession != null) {
099: if (oldestIdleSession.getLastAccessedTime() > session
100: .getLastAccessedTime())
101: oldestIdleSession = session;
102: } else {
103: oldestIdleSession = session;
104: }
105: }
106: }
107: }
108:
109: protected synchronized void removeOldestIdleSession() {
110: if (oldestIdleSession != null) {
111: oldestIdleSession.invalidate();
112: oldestIdleSession = null;
113: }
114: }
115:
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.
119: * @deprecated since jsdk2.1
120: */
121: public Enumeration getIds() {
122: return sessions.keys();
123: }
124:
125: /**
126: * Returns the session bound to the specified session ID.
127: * @param sessionID - the ID of a particular session object
128: * @return the session. Returns null if the session ID does
129: * not refer to a valid session.
130: * @deprecated since jsdk2.1
131: */
132: public HttpSession getSession(String sessionId) {
133: return (HttpSession) sessions.get(sessionId);
134: }
135:
136: /**
137: * Add a session in this context.
138: * @param session - The JigsawHttpSession to add.
139: * @return The session ID.
140: */
141: protected synchronized String addSession(JigsawHttpSession session) {
142: if (sessionCount >= maxsession)
143: removeOldestIdleSession();
144: String id = getNewSessionId();
145: sessions.put(id, session);
146: sessionCount++;
147: return id;
148: }
149:
150: /**
151: * Remove a session of this session context.
152: * @param id - The session ID.
153: */
154: protected void removeSession(String id) {
155: sessions.remove(id);
156: sessionCount--;
157: }
158:
159: private String getNewSessionId() {
160: return "J" + String.valueOf(new Date().hashCode()) + "-"
161: + (sessionNb++);
162: }
163:
164: public JigsawHttpSessionContext(httpd server, ServletProps props) {
165: this .props = props;
166: this .sessions = new Hashtable(3);
167: this .maxidle = props.getSessionsMaxIdleTime();
168: this .maxsession = props.getMaxSessionsNumber();
169: this .sweeper = new SessionSweeper(this, props
170: .getSessionsSweepDelay());
171: server.getProperties().registerObserver(this);
172: }
173:
174: }
|