Skip to content

Commit

Permalink
first draft
Browse files Browse the repository at this point in the history
  • Loading branch information
madness-inc committed Jun 13, 2019
1 parent cc061d9 commit fbf1458
Show file tree
Hide file tree
Showing 5 changed files with 411 additions and 3 deletions.
11 changes: 8 additions & 3 deletions pom.xml
Expand Up @@ -194,13 +194,13 @@
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
<version>8.5.35</version>
<version>8.5.41</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.5.0</version>
<version>3.10.2</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand All @@ -224,8 +224,13 @@
<dependency>
<groupId>org.appng</groupId>
<artifactId>appng-api</artifactId>
<version>1.14.4</version>
<version>1.18.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-client</artifactId>
<version>3.12</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,83 @@
package org.appng.tomcat.session.hazelcast;

import java.io.IOException;

import org.apache.catalina.Container;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.Session;
import org.apache.catalina.session.PersistentManagerBase;
import org.apache.juli.logging.Log;
import org.appng.tomcat.session.Utils;

public class HazelcastPersistentManager extends PersistentManagerBase {

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

private String name;

protected void destroyInternal() throws LifecycleException {
super.destroyInternal();
}

@Override
public void add(Session session) {
// do nothing, we don't want to use Map<String,Session> sessions!
}

@Override
public void remove(Session session, boolean update) {
// avoid super.remove
removeSession(session.getId());
}

@Override
public Session findSession(String id) throws IOException {
// do not call super, instead load the session directly from the store
try {
log.info("Manager FIND: " + id);
return getStore().load(id);
} catch (ClassNotFoundException e) {
throw new IOException("error loading class for session " + id, e);
}
}

@Override
protected String generateSessionId() {
String sessionId = super.generateSessionId();
log.info("Manager CREATED: " + sessionId);
return sessionId;
}

@Override
public void removeSuper(Session session) {
log.info("Manager REMOVE SUPER: " + session.getId());
super.removeSuper(session);
}

@Override
public String getName() {
if (this.name == null) {
Container container = getContext();

String contextName = container.getName();
if (!contextName.startsWith("/")) {
contextName = "/" + contextName;
}

String hostName = "";
String engineName = "";

if (container.getParent() != null) {
Container host = container.getParent();
hostName = host.getName();
if (host.getParent() != null) {
engineName = host.getParent().getName();
}
}

this.name = "/" + engineName + "/" + hostName + contextName;
}
return this.name;
}

}
@@ -0,0 +1,70 @@
package org.appng.tomcat.session.hazelcast;

import java.io.IOException;

import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import com.hazelcast.nio.serialization.DataSerializable;

public class HazelcastSession implements DataSerializable {
private String id;
private String application;
private byte[] data;
private Long created;
private Long lastModified;

public void writeData(ObjectDataOutput out) throws IOException {
out.writeUTF(id);
out.writeByteArray(data);
out.writeObject(created);
out.writeObject(lastModified);
}

public void readData(ObjectDataInput in) throws IOException {
id = in.readUTF();
data = in.readByteArray();
created = in.readObject();
lastModified = in.readObject();
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getApplication() {
return application;
}

public void setApplication(String application) {
this.application = application;
}

public byte[] getData() {
return data;
}

public void setData(byte[] data) {
this.data = data;
}

public Long getCreated() {
return created;
}

public void setCreated(Long created) {
this.created = created;
}

public Long getLastModified() {
return lastModified;
}

public void setLastModified(Long lastModified) {
this.lastModified = lastModified;
}

}
@@ -0,0 +1,64 @@
/*
* Copyright 2015-2017 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.hazelcast;

import java.io.IOException;

import javax.servlet.ServletException;

import org.apache.catalina.Session;
import org.apache.catalina.Valve;
import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;
import org.apache.catalina.valves.PersistentValve;
import org.apache.juli.logging.Log;
import org.appng.tomcat.session.Utils;

/**
* A {@link Valve} that uses {@link HazelcastPersistentManager} to store a
* {@link Session}
*/
public class HazelcastSessionTrackerValve extends PersistentValve {

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

@Override
public void invoke(Request request, Response response) throws IOException, ServletException {
try {
getNext().invoke(request, response);
} finally {
long start = System.currentTimeMillis();
HazelcastPersistentManager manager = (HazelcastPersistentManager) request.getContext().getManager();
Session session = request.getSessionInternal(false);
if (session != null) {
if (!Utils.isTemplateRequest(request)) {
if (session.isValid()) {
log.debug(String.format("Request with session completed, saving session %s", session.getId()));
manager.getStore().save(session);
} else {
log.debug(String.format("HTTP Session has been invalidated, removing %s", session.getId()));
manager.remove(session);
}
}
}
long duration = System.currentTimeMillis() - start;
if (log.isDebugEnabled() && duration > 0) {
log.debug(String.format("handling session for %s took %sms", request.getServletPath(), duration));
}
}
}

}

0 comments on commit fbf1458

Please sign in to comment.