Skip to content

Commit

Permalink
ATS-28
Browse files Browse the repository at this point in the history
  • Loading branch information
madness-inc committed Nov 30, 2021
1 parent 4bf6b39 commit 97c4697
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
Expand Up @@ -19,6 +19,7 @@

import org.apache.catalina.Session;
import org.apache.catalina.session.PersistentManagerBase;
import org.apache.catalina.session.StandardSession;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.appng.tomcat.session.Utils;
Expand All @@ -38,6 +39,11 @@ public HazelcastStore getStore() {
return (HazelcastStore) super.getStore();
}

@Override
public StandardSession createEmptySession() {
return (StandardSession) super.createEmptySession();
}

@Override
public void add(Session session) {
// do nothing, we don't want to use Map<String,Session> sessions!
Expand All @@ -50,8 +56,8 @@ public void remove(Session session, boolean update) {
}

@Override
public Session createSession(String sessionId) {
Session session = super.createSession(sessionId);
public StandardSession createSession(String sessionId) {
StandardSession session = (StandardSession) super.createSession(sessionId);
try {
getStore().save(session);
} catch (IOException e) {
Expand Down
Expand Up @@ -165,13 +165,18 @@ protected void initInternal() {
}

public void entryExpired(EntryEvent<String, SessionData> event) {
SessionData oldValue = event.getOldValue();
String siteName = oldValue.getSite();
SessionData expired = event.getOldValue();
String siteName = expired.getSite();

try (ByteArrayInputStream in = new ByteArrayInputStream(oldValue.getData());
try (ByteArrayInputStream in = new ByteArrayInputStream(expired.getData());
ObjectInputStream ois = Utils.getObjectInputStream(in, siteName, getManager().getContext())) {
StandardSession session = new StandardSession(getManager());
session.readObjectData(ois);
if (log.isDebugEnabled()) {
log.debug("expired: " + expired + " (accessed "
+ ((float) (System.currentTimeMillis() - session.getLastAccessedTime()) / 1000)
+ "ms ago, TTL: " + session.getMaxInactiveInterval() + "s)");
}
session.expire(true);
} catch (IOException | ClassNotFoundException e) {
log.error("Error expiring session " + event.getKey(), e);
Expand Down Expand Up @@ -208,9 +213,6 @@ public void save(Session session) throws IOException {
((StandardSession) session).writeObjectData(oos);
String site = Utils.getSiteName(session.getSession());
SessionData sessionData = new SessionData(session.getId(), site, bos.toByteArray());
session.setManager(getManager());
session.access();
session.endAccess();
getSessions().put(session.getId(), sessionData, session.getMaxInactiveInterval(), TimeUnit.SECONDS);
log.debug("saved: " + sessionData + " with TTL of " + session.getMaxInactiveInterval() + " seconds");
} finally {
Expand Down Expand Up @@ -240,8 +242,10 @@ public StandardSession load(String id) throws ClassNotFoundException, IOExceptio
try (ByteArrayInputStream is = new ByteArrayInputStream(sessionData.getData());
ObjectInputStream ois = Utils.getObjectInputStream(is, sessionData.getSite(),
manager.getContext())) {
session = (StandardSession) this.manager.createEmptySession();
session = getManager().createEmptySession();
session.readObjectData(ois);
session.access();
session.endAccess();
log.debug("loaded: " + sessionData);
}
}
Expand All @@ -253,7 +257,7 @@ public StandardSession load(String id) throws ClassNotFoundException, IOExceptio

public void remove(String id) throws IOException {
SessionData removed = getSessions().remove(id);
log.debug("removed: " + removed);
log.debug("removed: " + (null == removed ? id : removed));
}

public String[] keys() throws IOException {
Expand Down
Expand Up @@ -74,11 +74,13 @@ public void sessionCreated(HttpSessionEvent se) {
store.start();
manager.setStore(store);

StandardSession session = (StandardSession) manager.createSession("4711");
StandardSession session = manager.createSession("4711");
session.setMaxInactiveInterval(3/* seconds */);
session.setAttribute("foo", "test");

Assert.assertTrue(session.isNew());
store.save(session);
Assert.assertTrue(session.isNew());

TimeUnit.SECONDS.sleep(10);

Expand Down

0 comments on commit 97c4697

Please sign in to comment.