-
Notifications
You must be signed in to change notification settings - Fork 1
/
SessionManager.java
287 lines (256 loc) · 8.86 KB
/
SessionManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* Copyright 2015-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.appng.tomcat.session;
import java.io.IOException;
import java.util.Date;
import java.util.Locale;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.LifecycleState;
import org.apache.catalina.session.ManagerBase;
import org.apache.juli.logging.Log;
/**
* Basic implementation of {@link ManagerBase} using a persistent storage.
*
* @param <T>
* the type of the persistent storage
*/
public abstract class SessionManager<T> extends ManagerBase {
private static final double NANOS_TO_MILLIS = 1000000d;
protected boolean sticky = true;
protected int sessionSaveIntervalSeconds = 10;
/**
* Updates the session in the persistent storage.
*
* @param id
* the id of the session
* @param sessionData
* the session's {@link SessionData}
*
* @throws IOException
* if an error occurs while updating the session
*/
protected abstract void updateSession(String id, SessionData sessionData) throws IOException;
/**
* Retrieves the {@link SessionData} from the persistent storage
*
* @param id
* the id of the session
*
* @return the {@link SessionData} of the session, or {@code null} if the session could not be found
*
* @throws IOException
* if an error occurs while retrieving the data
*/
protected abstract SessionData findSessionInternal(String id) throws IOException;
/**
* Remove the session from the underlying persistent storage
*
* @param session
* the session to remove
*/
public abstract void removeInternal(org.apache.catalina.Session session);
public abstract Log log();
/**
* Returns the persistent storage.
*
* @return the persistent storage
*/
protected abstract T getPersistentSessions();
@Override
protected void stopInternal() throws LifecycleException {
super.stopInternal();
setState(LifecycleState.STOPPING);
}
@Override
public Session createEmptySession() {
return new Session(this);
}
@Override
public Session createSession(String sessionId) {
Session session = (Session) super.createSession(sessionId);
if (log().isTraceEnabled()) {
log().trace(String.format("%s has been created (isNew: %s)", session.getId(), session.isNew()));
}
return session;
}
@Override
public Session findSession(String id) throws IOException {
long start = System.nanoTime();
// Support multiple threads accessing the same, locally cached session.
// Even if we're not sticky, this is OK, because SessionTrackerValve calls
// manager.removeLocal(session) in that case!
Session session = (Session) super.findSession(id);
if (null == session) {
SessionData sessionData = findSessionInternal(id);
if (null == sessionData) {
if (log().isDebugEnabled()) {
log().debug(String.format("Session %s not found!", id));
}
} else {
try {
session = Session.load(this, sessionData);
this.add(session);
if (log().isDebugEnabled()) {
if (null == session) {
log().debug(String.format("Session %s found, but has expired!", id));
} else {
log().debug(String.format(Locale.ENGLISH, "Loaded %s in %.2fms", sessionData,
getDuration(start)));
}
}
} catch (ClassNotFoundException e) {
log().error("Error loading session" + id, e);
}
}
} else {
if (log().isDebugEnabled()) {
log().debug(
String.format(Locale.ENGLISH, "Loaded %s from local store in %.2fms.", id, getDuration(start)));
}
session.access();
}
return session;
}
protected double getDuration(long nanoStart) {
return ((System.nanoTime() - nanoStart)) / NANOS_TO_MILLIS;
}
public final boolean commit(Session session) throws IOException {
return commit(session, null);
}
/**
* If one of these condition matches, the session is persisted to the store:
* <ul>
* <li>Is this manager non-sticky?
* <li>Is the session marked as dirty?
* <li>Is the session's new checksum different from the old checksum?
* <li>Is it more than {@code sessionSaveIntervalSeconds} seconds ago when the session was last accessed?
* </ul>
*
* @param session
* @param alternativeSiteName
*
* @return
*
* @throws IOException
*/
public final boolean commit(org.apache.catalina.Session session, String alternativeSiteName) throws IOException {
session.endAccess();
boolean saved = false;
long start = System.nanoTime();
SessionData oldSessionData = null;
Session sessionInternal = Session.class.cast(session);
boolean sessionDirty = sessionInternal.isDirty();
int checksum = -1;
int oldChecksum = -1;
long secondsSinceAccess = 0;
if (!sticky || sessionDirty || (checksum = sessionInternal
.checksum()) != (oldChecksum = ((oldSessionData = findSessionInternal(session.getId())).checksum()))
|| (secondsSinceAccess = oldSessionData.secondsSinceAccessed()) > sessionSaveIntervalSeconds) {
SessionData sessionData = sessionInternal.serialize(alternativeSiteName);
updateSession(sessionInternal.getId(), sessionData);
saved = true;
if (log().isDebugEnabled()) {
String reason = sticky
? (sessionDirty ? "dirty-flag was set"
: (secondsSinceAccess > sessionSaveIntervalSeconds
? "last accessed " + secondsSinceAccess + " seconds ago"
: String.format("checksum %s <> %s", oldChecksum, checksum)))
: "sticky=false";
log().debug(String.format(Locale.ENGLISH, "%s has been saved in %.2fms (%s)", sessionData,
getDuration(start), reason));
}
} else if (log().isDebugEnabled()) {
log().debug(String.format("%s has not been changed (checksum %s)", session.getId(),
sessionInternal.checksum()));
}
return saved;
}
@Override
public void add(org.apache.catalina.Session session) {
super.add(session);
if (log().isTraceEnabled()) {
log().trace(String.format("%s has been added to local cache.", session.getId()));
}
}
@Override
public void remove(org.apache.catalina.Session session, boolean expired) {
super.remove(session, expired);
removeInternal(session);
if (expired && log().isDebugEnabled()) {
String message = String.format(Locale.ENGLISH,
"%s has expired (created: %s, last accessed: %s, maxLifeTime: %ss, age: %ss)", session.getId(),
new Date(session.getCreationTimeInternal()), new Date(session.getLastAccessedTimeInternal()),
session.getMaxInactiveInterval(),
(System.currentTimeMillis() - session.getLastAccessedTimeInternal()) / 1000);
log().debug(message);
}
}
// method to be called by processExpires
protected boolean expireInternal(String id, SessionData sessionData) throws ClassNotFoundException, IOException {
if (null == sessionData) {
if (log().isDebugEnabled()) {
log().debug(String.format("%s not found in persistent store", id));
}
if (sticky) {
removeLocal(sessions.get(id));
}
} else {
Session session = Session.load(this, sessionData);
if (null == session) {
// session is not valid, so manager.remove(session, true) already has been called
// which in turn will remove the session from the local cache and also from the persistent store
if (log().isTraceEnabled()) {
log().trace(String.format("%s has been removed by internal expiration", id));
}
return true;
}
}
return false;
}
/**
* Remove this Session from the active Sessions, but not from the persistent sessions
*
* @param session
* Session to be removed
*/
public void removeLocal(org.apache.catalina.Session session) {
if (null != session && session.getIdInternal() != null) {
org.apache.catalina.Session removed = sessions.remove(session.getIdInternal());
if (log().isTraceEnabled()) {
String message = null == removed ? "%s was not locally cached."
: "%s has been removed from local cache.";
log().trace(String.format(message, session.getId()));
}
}
}
public boolean isSticky() {
return sticky;
}
public void setSticky(boolean sticky) {
this.sticky = sticky;
}
public void setSessionSaveIntervalSeconds(int sessionSaveIntervalSeconds) {
this.sessionSaveIntervalSeconds = sessionSaveIntervalSeconds;
}
@Override
public void load() throws ClassNotFoundException, IOException {
// don't load all sessions when starting
}
@Override
public void unload() throws IOException {
// don't save all sessions when stopping
}
}