1
2
3
4
5
6
7
8
9
10
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
21
22
23
24
25
26 public class SimpleSessionManager extends SimpleCookieSessionManager {
27
28
29 public static final int DEFAULT_SESSION_TIMEOUT = 30 * 60 * 1000;
30
31
32 public static final int DEFAULT_MIN_SCAN_INTERVAL = 10 * 1000;
33
34
35 protected int sessionTimeout = DEFAULT_SESSION_TIMEOUT;
36
37
38 protected long lastScan = 0;
39
40
41 protected int minScanInterval = DEFAULT_MIN_SCAN_INTERVAL;
42
43
44
45
46
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
59
60
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
74
75
76 public void removeSession(HTTPConnection connection) {
77 checkForExpiredSessions();
78 super.removeSession(connection);
79 }
80
81
82
83
84
85 public void removeSession(Object session) {
86 checkForExpiredSessions();
87 super.removeSession(session);
88 }
89
90
91
92
93
94 public String rewriteURL(HTTPConnection connection, String url) {
95 return url;
96 }
97
98
99
100
101
102 protected void checkForExpiredSessions() {
103 if (lastScan + minScanInterval < System.currentTimeMillis()) {
104 lastScan = System.currentTimeMillis();
105 scanForExpiredSessions();
106 }
107 }
108
109
110
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
127
128
129 public int getSessionTimeout() {
130 return sessionTimeout;
131 }
132
133
134
135
136
137 public void setSessionTimeout(int sessionTimeout) {
138 this.sessionTimeout = sessionTimeout;
139 }
140
141
142
143
144
145 public int getMinScanInterval() {
146 return minScanInterval;
147 }
148
149
150
151
152
153 public void setMinScanInterval(int minScanInterval) {
154 this.minScanInterval = minScanInterval;
155 }
156
157 }