Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cc061d9
commit fbf1458
Showing
5 changed files
with
411 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/main/java/org/appng/tomcat/session/hazelcast/HazelcastPersistentManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
70 changes: 70 additions & 0 deletions
70
src/main/java/org/appng/tomcat/session/hazelcast/HazelcastSession.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
64 changes: 64 additions & 0 deletions
64
src/main/java/org/appng/tomcat/session/hazelcast/HazelcastSessionTrackerValve.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.