Skip to content

Commit

Permalink
ATS-14
Browse files Browse the repository at this point in the history
  • Loading branch information
madness-inc committed Sep 2, 2019
1 parent aa2841a commit 110b4c4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 47 deletions.
Expand Up @@ -65,18 +65,19 @@ public MongoStore getStore() {
@Override
public Session createSession(String sessionId) {
Session session = super.createSession(sessionId);
getStore().setSessionActive(session);
try {
save(session);
} catch (IOException e) {
log.warn(String.format("Error creating session: %s", session.getIdInternal()));
session = null;
getStore().setSessionInactive();
}
getStore().setThreadLocalSession(session);
return session;
}

public void afterRequest() {
getStore().removeThreadLocalSession();
getStore().setSessionInactive();
}

public void save(Session session) throws IOException {
Expand Down
96 changes: 51 additions & 45 deletions src/main/java/org/appng/tomcat/session/mongo/MongoStore.java
Expand Up @@ -57,7 +57,11 @@
*/
public class MongoStore extends StoreBase {

/** The currently active session for this thread */
protected ThreadLocal<Session> currentSession = new ThreadLocal<>();

/** The ID of the currently active session for this thread */
protected ThreadLocal<String> currentSessionId = new ThreadLocal<>();

private final Log log = Utils.getLog(MongoStore.class);

Expand Down Expand Up @@ -269,51 +273,48 @@ public void processExpires() {
* {@inheritDoc}
*/
public StandardSession load(String id) throws ClassNotFoundException, IOException {

StandardSession session = (StandardSession) currentSession.get();
if (null != session) {
if (session.getIdInternal().equals(id)) {
trace("Session from ThreadLocal: %s", id);
return session;
StandardSession session = null;

if (null == id) {
setSessionInactive();
} else if (id.equals(currentSessionId.get())) {
session = (StandardSession) currentSession.get();
debug("Returning currently active session %s for thread %s", currentSessionId.get(),
Thread.currentThread().getName());
} else {
long start = System.currentTimeMillis();
BasicDBObject sessionQuery = sessionQuery(id);
DBObject mongoSession = this.collection.findOne(sessionQuery);
if (null == mongoSession) {
info("Session %s not found, returning null!", id);
setSessionInactive();
} else {
debug("Session from ThreadLocal differed! Requested: %s, found: %s", id, session.getIdInternal());
removeThreadLocalSession();
session = null;
}
}

long start = System.currentTimeMillis();

BasicDBObject sessionQuery = sessionQuery(id);
DBObject mongoSession = this.collection.findOne(sessionQuery);
if (null == mongoSession) {
info("Session %s not found, returning null!", id);
return null;
}

Container container = manager.getContext();
Context context = (Context) container;
ClassLoader appContextLoader = context.getLoader().getClassLoader();

byte[] data = (byte[]) mongoSession.get(sessionDataProperty);
if (data != null) {
try (ObjectInputStream ois = Utils.getObjectInputStream(appContextLoader,
manager.getContext().getServletContext(), data)) {

session = (StandardSession) this.manager.createEmptySession();
session.readObjectData(ois);
session.setManager(this.manager);

debug("Loaded session %s with query %s in %s ms (lastModified %s)", id, sessionQuery,
System.currentTimeMillis() - start, new Date(session.getLastAccessedTime()));

setThreadLocalSession(session);
} catch (ReflectiveOperationException roe) {
getLog().error(String.format("Error loading session: %s", id), roe);
throw roe;
Container container = manager.getContext();
Context context = (Context) container;
ClassLoader appContextLoader = context.getLoader().getClassLoader();

byte[] data = (byte[]) mongoSession.get(sessionDataProperty);
if (data != null) {
try (ObjectInputStream ois = Utils.getObjectInputStream(appContextLoader,
manager.getContext().getServletContext(), data)) {

session = (StandardSession) this.manager.createEmptySession();
session.readObjectData(ois);
session.setManager(this.manager);

debug("Loaded session %s with query %s in %s ms (lastModified %s)", id, sessionQuery,
System.currentTimeMillis() - start, new Date(session.getLastAccessedTime()));

setSessionActive(session);
} catch (ReflectiveOperationException roe) {
getLog().error(String.format("Error loading session: %s", id), roe);
throw roe;
}
} else {
warn("No data for session: %s, returning null!", id);
setSessionInactive();
}
}
} else {
warn("No data for session: %s", id);
}
return session;
}
Expand Down Expand Up @@ -533,12 +534,17 @@ private Log getLog() {
return log;
}

void removeThreadLocalSession() {
void setSessionInactive() {
String id = currentSessionId.get();
debug("Removing session from thread %s: %s", Thread.currentThread().getName(), id);
currentSession.remove();
currentSessionId.remove();
}

void setThreadLocalSession(Session session) {
void setSessionActive(Session session) {
currentSession.set(session);
currentSessionId.set(session.getId());
debug("Setting session for thread %s: %s", Thread.currentThread().getName(), session.getId());
}

// Setters
Expand Down

0 comments on commit 110b4c4

Please sign in to comment.