1 /*
2 * Copyright (c) 2005-2007 Creative Sphere Limited.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 *
10 * Creative Sphere - initial API and implementation
11 *
12 */
13 package org.abstracthorizon.danube.http.session;
14
15 import org.abstracthorizon.danube.http.HTTPConnection;
16
17 import java.util.Iterator;
18
19 /**
20 * Simple session manager implementation that checks for expired sessions
21 * on invocation of {@link HTTPSessionManager} methods. If method is invoked sooner then
22 * {@link #minScanInterval} then scan won't happen.
23 *
24 * @author Daniel Sendula
25 */
26 public class SimpleSessionManager extends SimpleCookieSessionManager {
27
28 /** Default session timeout as 30 minutes */
29 public static final int DEFAULT_SESSION_TIMEOUT = 30 * 60 * 1000; // 30 minutes
30
31 /** Default minimum interval between scans for expired sessions */
32 public static final int DEFAULT_MIN_SCAN_INTERVAL = 10 * 1000; // 10 secs
33
34 /** Current session timeout */
35 protected int sessionTimeout = DEFAULT_SESSION_TIMEOUT;
36
37 /** When was last scan performed */
38 protected long lastScan = 0;
39
40 /** Minimum interval between two scans for expired sessions */
41 protected int minScanInterval = DEFAULT_MIN_SCAN_INTERVAL;
42
43 /**
44 * Finds a session for supplied connection
45 * @param connection connection
46 * @param create flag defining should session be created it doesn't exist already
47 */
48 public Object findSession(HTTPConnection connection, boolean create) {
49 checkForExpiredSessions();
50 Session session = (Session)super.findSession(connection, create);
51 if (session != null) {
52 session.lastAccessed = System.currentTimeMillis();
53 }
54 return session;
55 }
56
57 /**
58 * Searches for the session using givne session id
59 * @param sessionId session id object
60 * @param create flag defining should session be created it doesn't exist already
61 */
62 public Object findSession(Object sessionId, boolean create) {
63 checkForExpiredSessions();
64 Session session = (Session)super.findSession(sessionId, create);
65 if (session != null) {
66 session.lastAccessed = System.currentTimeMillis();
67 }
68 return session;
69 }
70
71
72 /**
73 * Removes session from connection
74 * @param connection connection
75 */
76 public void removeSession(HTTPConnection connection) {
77 checkForExpiredSessions();
78 super.removeSession(connection);
79 }
80
81 /**
82 * This object removes existing session
83 * @param session session to be removed
84 */
85 public void removeSession(Object session) {
86 checkForExpiredSessions();
87 super.removeSession(session);
88 }
89
90 /**
91 * Rewrites URL for given connection
92 * @param url URL to be rewritten
93 */
94 public String rewriteURL(HTTPConnection connection, String url) {
95 return url;
96 }
97
98 /**
99 * Checks if there are expired sessions. This method won't run if
100 * it was invoked in less then {@link #minScanInterval} time.
101 */
102 protected void checkForExpiredSessions() {
103 if (lastScan + minScanInterval < System.currentTimeMillis()) {
104 lastScan = System.currentTimeMillis();
105 scanForExpiredSessions();
106 }
107 }
108
109 /**
110 * Scans all defined sessions for expired ones
111 */
112 protected void scanForExpiredSessions() {
113 synchronized (sessions) {
114 Iterator<Session> it = sessions.values().iterator();
115 long then = System.currentTimeMillis() - getSessionTimeout();
116 while (it.hasNext()) {
117 Session session = it.next();
118 if (session.lastAccessed < then) {
119 it.remove();
120 }
121 }
122 }
123 }
124
125 /**
126 * Returns session timeout
127 * @return session timeout
128 */
129 public int getSessionTimeout() {
130 return sessionTimeout;
131 }
132
133 /**
134 * Sets session timeout
135 * @param sessionTimeout session timeout
136 */
137 public void setSessionTimeout(int sessionTimeout) {
138 this.sessionTimeout = sessionTimeout;
139 }
140
141 /**
142 * Returns minimum interval between scans for expired sessions
143 * @return minimum interval between scans for expired sessions
144 */
145 public int getMinScanInterval() {
146 return minScanInterval;
147 }
148
149 /**
150 * Sets minimum interval between scans for expired sessions
151 * @param minScanInterval minimum interval between scans for expired sessions
152 */
153 public void setMinScanInterval(int minScanInterval) {
154 this.minScanInterval = minScanInterval;
155 }
156
157 }