Skip to content

Commit

Permalink
ATS-31 moved package, update license header
Browse files Browse the repository at this point in the history
  • Loading branch information
madness-inc committed Jun 23, 2022
1 parent 7576fb4 commit f3c5d4e
Show file tree
Hide file tree
Showing 27 changed files with 81 additions and 744 deletions.
30 changes: 6 additions & 24 deletions README.adoc
Expand Up @@ -92,30 +92,12 @@ Add the following into your Tomcat `context.xml` (or the context block of the `s

[source,xml]
----
<Valve className="org.appng.tomcat.session.hazelcast.HazelcastSessionTrackerValve" />
<Manager className="org.appng.tomcat.session.hazelcast.HazelcastPersistentManager">
<!-- standard configuration, using multicast -->
<Store className="org.appng.tomcat.session.hazelcast.HazelcastStore" mode="multicast" port="5701" instanceName="dev" clusterName="appNG"
multicastGroup="224.2.2.3" multicastPort="54327" multicastTimeoutSeconds="2" multicastTimeToLive="32" />
<!-- optionally, for all modes except 'classpath':
managementCenterEnabled="true"
-->
<!-- uses a config file from the classpath -->
<!-- Store className="org.appng.tomcat.session.hazelcast.HazelcastStore" mode="classpath" configFile="hazelcast.xml" /-->
<!-- uses tcp, allowing to use a comma separated list of addresses -->
<!-- Store className="org.appng.tomcat.session.hazelcast.HazelcastStore" mode="tcp" port="5701" instanceName="dev" clusterName="appNG"
tcpMembers="localhost:5701" /-->
<!-- client mode, allowing to use a comma separated list of addresses -->
<!-- Store className="org.appng.tomcat.session.hazelcast.HazelcastStore" mode="client" instanceName="dev" clusterName="appNG"
addresses="localhost:5701" /-->
<!-- standalone mode, for development purposes -->
<!-- Store className="org.appng.tomcat.session.hazelcast.HazelcastStore" mode="standalone" port="5701" instanceName="dev" clusterName="appNG" /-->
</Manager>
<?xml version='1.0' encoding='utf-8'?>
<Context>
<Valve className="org.appng.tomcat.session.hazelcast.v2.HazelcastSessionTrackerValve" />
<!-- configFile must use a path from classpath -->
<Manager className="org.appng.tomcat.session.hazelcast.v2.HazelcastManager" configFile="hazelcast.xml" />
</Context>
----

Copy the following files into the `TOMCAT_BASE/lib` directory:
Expand Down
8 changes: 4 additions & 4 deletions pom.xml
Expand Up @@ -194,7 +194,7 @@
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
<version>9.0.54</version>
<version>9.0.64</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand All @@ -206,13 +206,13 @@
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.6.3</version>
<version>4.2.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.20.RELEASE</version>
<version>4.3.30.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -230,7 +230,7 @@
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<version>5.0</version>
<version>5.1.2</version>
</dependency>
</dependencies>
</project>
7 changes: 7 additions & 0 deletions src/main/java/HostName.java
@@ -0,0 +1,7 @@
public class HostName {

public static void main(String[] args) throws java.net.UnknownHostException {
System.err.println(java.net.InetAddress.getLocalHost().getHostName());
}

}
2 changes: 1 addition & 1 deletion src/main/java/org/appng/tomcat/session/Constants.java
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2021 the original author or authors.
* 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.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/appng/tomcat/session/SessionData.java
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2021 the original author or authors.
* 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.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/appng/tomcat/session/Utils.java
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2021 the original author or authors.
* 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.
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2021 the original author or authors.
* 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.
Expand All @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.appng.tomcat.session.hazelcast.v2;
package org.appng.tomcat.session.hazelcast;

import java.io.IOException;
import java.util.Arrays;
Expand All @@ -27,7 +27,6 @@
import org.apache.juli.logging.Log;
import org.appng.tomcat.session.SessionData;
import org.appng.tomcat.session.Utils;
import org.appng.tomcat.session.hazelcast.HazelcastSession;

import com.hazelcast.config.ClasspathXmlConfig;
import com.hazelcast.core.Hazelcast;
Expand All @@ -38,7 +37,7 @@ public class HazelcastManager extends ManagerBase {

private static final double NANOS_TO_MILLIS = 1000000d;
private final Log log = Utils.getLog(HazelcastManager.class);
private String configFile = "WEB-INF/classes/hazelcast.xml";
private String configFile = "hazelcast.xml";
private String mapName = "tomcat.sessions";
private HazelcastInstance instance;

Expand Down

This file was deleted.

@@ -1,5 +1,5 @@
/*
* Copyright 2015-2021 the original author or authors.
* 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.
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2021 the original author or authors.
* 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.
Expand All @@ -16,45 +16,70 @@
package org.appng.tomcat.session.hazelcast;

import java.io.IOException;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

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.catalina.valves.ValveBase;
import org.apache.juli.logging.Log;
import org.appng.tomcat.session.Utils;

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

private final Log log = Utils.getLog(HazelcastSessionTrackerValve.class);
protected Pattern filter = Pattern.compile("^(/template/.*)|((/health)(\\?.*)?)$");
protected String siteNameHeader = "x-appng-site";

@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 (session.isValid()) {
log.trace(String.format("Request with session completed, saving session %s", session.getId()));
manager.getStore().save(session);
} else {
log.trace(String.format("HTTP Session has been invalidated, removing %s", session.getId()));
manager.remove(session);
if (commitRequired(request.getDecodedRequestURI()) && null != session) {
long start = System.currentTimeMillis();
HazelcastManager manager = (HazelcastManager) request.getContext().getManager();
boolean committed = manager.commit(session, request.getHeader(siteNameHeader));
if (log.isDebugEnabled()) {
log.debug(String.format("Handling session %s for %s took %dms (committed: %s)", session.getId(),
request.getServletPath(), System.currentTimeMillis() - start, committed));
}
}
}
}

protected boolean commitRequired(String uri) {
return null == filter || !filter.matcher(uri).matches();
}

public String getSiteNameHeader() {
return siteNameHeader;
}

public void setSiteNameHeader(String siteNameHeader) {
this.siteNameHeader = siteNameHeader;
}

public String getFilter() {
return null == filter ? null : filter.toString();
}

long duration = System.currentTimeMillis() - start;
if (log.isDebugEnabled() && duration > 0) {
log.debug(String.format("handling session for %s took %sms", request.getServletPath(), duration));
public void setFilter(String filter) {
if (filter == null || filter.length() == 0) {
this.filter = null;
} else {
try {
this.filter = Pattern.compile(filter);
} catch (PatternSyntaxException pse) {
log.error("ivalid pattern", pse);
}
}
}
Expand Down

0 comments on commit f3c5d4e

Please sign in to comment.