-
Notifications
You must be signed in to change notification settings - Fork 1
/
MongoSessionManager.java
300 lines (247 loc) · 10 KB
/
MongoSessionManager.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
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
* 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.mongo;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.LifecycleState;
import org.apache.catalina.Session;
import org.apache.juli.logging.Log;
import org.appng.tomcat.session.SessionData;
import org.appng.tomcat.session.SessionManager;
import org.appng.tomcat.session.Utils;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.MongoException;
import com.mongodb.ReadConcern;
import com.mongodb.ReadPreference;
import com.mongodb.ServerAddress;
import com.mongodb.WriteConcern;
import com.mongodb.WriteResult;
/**
* A {@link SessionManager} implementation that uses a {@link MongoClient}
*/
public final class MongoSessionManager extends SessionManager<DBCollection> {
private final Log log = Utils.getLog(MongoSessionManager.class);
protected static final String PROP_SESSIONDATA = "data";
/** Property used to store the Session's ID */
private static final String PROP_ID = "session_id";
/** Property used to store the Session's context name */
protected static final String PROP_CONTEXT = "app";
/** Property used to store the Session's last modified date. */
protected static final String PROP_LAST_MODIFIED = "lastModified";
/** Property used to store the Session's creation date. */
protected static final String PROP_CREATION_TIME = "creationTime";
/** Mongo Collection for the Sessions */
protected DBCollection collection;
/** Name of the MongoDB Database to use. */
protected String dbName = "tomcat_session";
/** Name of the MongoDB Collection to store the sessions. Defaults to <em>tomcat.sessions</em> */
protected String collectionName = "tomcat.sessions";
/** {@link MongoClient} instance to use. */
protected MongoClient mongoClient;
/** Controls if the MongoClient will write to slaves. Equivalent to <em>slaveOk</em>. Defaults to false. */
protected boolean useSlaves = false;
/** The {@link ReadPreference}, using {@link ReadPreference#primary()} for maximum consistency by default */
protected ReadPreference readPreference = ReadPreference.primary();
/** The {@link ReadConcern}, using {@link ReadConcern#DEFAULT} for maximum consistency by default */
protected ReadConcern readConcern = ReadConcern.DEFAULT;
protected String connectionUri;
/** MongoDB User. Used if MongoDB is in <em>Secure</em> mode. */
protected String username;
/** MongoDB password. Used if MongoDB is in <em>Secure</em> mode */
protected String password;
/** Connection Timeout in milliseconds. Defaults to 0, or no timeout */
protected int connectionTimeoutMs = 0;
/** Connection Wait Timeout in milliseconds. Defaults to 0, or no timeout */
protected int connectionWaitTimeoutMs = 0;
/** MongoDB replica set name. */
protected String replicaSet;
/** Sets whether writes should be retried if they fail due to a network error. */
private boolean retryWrites = false;
/** Controls what {@link WriteConcern} the MongoClient will use. Defaults to {@link WriteConcern#MAJORITYs} */
protected WriteConcern writeConcern = WriteConcern.MAJORITY;
/** Maximum Number of connections the MongoClient will manage. Defaults to 20 */
protected int maxPoolSize = 20;
/** The socket timeout when connecting to a MongoDB server */
private int socketTimeout = 10000;
/**
* Sets the server selection timeout in milliseconds, which defines how long the driver will wait for server
* selection to succeed before throwing an exception.
*/
private int serverSelectionTimeout = 30000;
/**
* MongoDB Hosts. This can be a single host or a comma separated list using a [host:port] format. Lists are
* typically used for replica sets. This value is ignored if the <em>dbConnectionUri</em> is provided.
* <p>
*
* <pre>
* 127.0.0.1:27001,127.0.0.1:27002
* </pre>
* </p>
*/
protected String hosts;
@Override
public Log log() {
return log;
}
@Override
protected void startInternal() throws LifecycleException {
super.startInternal();
try {
if (this.connectionUri != null) {
log.info(String.format("Connecting to MongoDB [%s]", this.connectionUri));
this.mongoClient = new MongoClient(this.connectionUri);
} else {
if (this.useSlaves) {
readPreference = ReadPreference.secondaryPreferred();
}
// @formatter:off
MongoClientOptions options = MongoClientOptions.builder()
.connectTimeout(connectionTimeoutMs)
.maxWaitTime(connectionWaitTimeoutMs)
.connectionsPerHost(maxPoolSize)
.writeConcern(writeConcern)
.serverSelectionTimeout(serverSelectionTimeout)
.retryWrites(retryWrites)
.readPreference(readPreference)
.readConcern(readConcern)
.requiredReplicaSetName(replicaSet)
.socketTimeout(socketTimeout)
.build();
// @formatter:on
List<ServerAddress> hosts = new ArrayList<ServerAddress>();
for (String dbHost : this.hosts.split(",")) {
String[] hostInfo = dbHost.split(":");
hosts.add(new ServerAddress(hostInfo[0], Integer.parseInt(hostInfo[1])));
}
log.info(String.format("Connecting to MongoDB [%s]", this.hosts));
if (this.username != null || this.password != null) {
log.info(String.format("Authenticating using [%s]", this.username));
MongoCredential credential = MongoCredential.createCredential(username, dbName,
password.toCharArray());
this.mongoClient = new MongoClient(hosts, credential, options);
} else {
this.mongoClient = new MongoClient(hosts, options);
}
}
log.info(String.format("Using Database [%s]", this.dbName));
DB db = this.mongoClient.getDB(this.dbName);
this.collection = db.getCollection(this.collectionName);
log.info(String.format("Preparing indexes"));
BasicDBObject lastModifiedIndex = new BasicDBObject(PROP_LAST_MODIFIED, 1);
try {
this.collection.dropIndex(lastModifiedIndex);
this.collection.dropIndex(new BasicDBObject(PROP_CONTEXT, 1));
} catch (Exception e) {
/* these indexes may not exist, so ignore */
}
this.collection.createIndex(lastModifiedIndex);
this.collection.createIndex(new BasicDBObject(PROP_CONTEXT, 1));
log.info(String.format("[%s]: Store ready.", this.getName()));
} catch (MongoException me) {
log.error("Unable to Connect to MongoDB", me);
throw new LifecycleException(me);
}
setState(LifecycleState.STARTING);
}
@Override
protected void stopInternal() throws LifecycleException {
super.stopInternal();
this.mongoClient.close();
}
@Override
protected SessionData findSessionInternal(String id) throws IOException {
DBObject mongoSession = this.collection.findOne(sessionQuery(id));
if (null != mongoSession) {
byte[] sessionData = (byte[]) mongoSession.get(PROP_SESSIONDATA);
try (ByteArrayInputStream bais = new ByteArrayInputStream(sessionData);
ObjectInputStream ois = new ObjectInputStream(bais)) {
return (SessionData) ois.readObject();
} catch (ReflectiveOperationException roe) {
log.error(String.format("Error loading session: %s", id), roe);
throw new IOException(roe);
}
} else {
log.warn(String.format("Session not found: %s, returning null!", id));
}
return null;
}
private BasicDBObject sessionQuery(String id) {
return new BasicDBObject(PROP_ID, id).append(PROP_CONTEXT, this.getName());
}
@Override
protected void updateSession(String id, SessionData sessionData) throws IOException {
long start = System.nanoTime();
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos)) {
oos.writeObject(sessionData);
BasicDBObject sessionQuery = sessionQuery(sessionData.getId());
BasicDBObject mongoSession = (BasicDBObject) sessionQuery.copy();
mongoSession.put(PROP_SESSIONDATA, bos.toByteArray());
mongoSession.put(PROP_LAST_MODIFIED, Calendar.getInstance().getTime());
WriteResult updated = this.collection.update(sessionQuery, mongoSession, true, false);
log.debug(String.format(Locale.ENGLISH,
"Saved session %s with query %s in %.2fms (lastModified %s) acknowledged: %s", sessionData.getId(),
sessionQuery, getDuration(start), mongoSession.getDate(PROP_LAST_MODIFIED),
updated.wasAcknowledged()));
} catch (MongoException | IOException e) {
log.warn(String.format("Error saving session: %s", sessionData.getId()));
throw e;
}
}
@Override
public void removeInternal(Session session) {
String id = session.getId();
BasicDBObject sessionQuery = sessionQuery(id);
try {
this.collection.remove(sessionQuery);
log.debug(String.format("removed session %s (query: %s)", id, sessionQuery));
} catch (MongoException e) {
log.error("Unable to remove sessions for [" + id + ":" + this.getName() + "] from MongoDB", e);
throw e;
}
}
//@Override
protected int expireInternal() {
return 0;
}
public void setCollectionName(String collectionName) {
this.collectionName = collectionName;
}
public void setConnectionUri(String connectionUri) {
this.connectionUri = connectionUri;
}
public void setDbName(String dbName) {
this.dbName = dbName;
}
@Override
protected DBCollection getPersistentSessions() {
return collection;
}
}