Skip to content

Commit

Permalink
Fixed - RedissonSessionManager throws java.lang.ClassNotFoundExceptio…
Browse files Browse the repository at this point in the history
…n if readMode=MEMORY #1867
  • Loading branch information
Nikita Koksharov committed Jan 17, 2019
1 parent 228319d commit ae14dba
Show file tree
Hide file tree
Showing 28 changed files with 306 additions and 160 deletions.
Expand Up @@ -15,8 +15,14 @@
*/
package org.redisson.tomcat;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import org.apache.catalina.util.CustomObjectInputStream;

/**
*
* @author Nikita Koksharov
Expand All @@ -31,10 +37,6 @@ public class AttributeMessage implements Serializable {
public AttributeMessage() {
}

public AttributeMessage(String sessionId) {
this.sessionId = sessionId;
}

public AttributeMessage(String nodeId, String sessionId) {
this.nodeId = nodeId;
this.sessionId = sessionId;
Expand All @@ -48,4 +50,22 @@ public String getNodeId() {
return nodeId;
}

protected byte[] toByteArray(Object value) throws IOException {
if (value == null) {
return null;
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bos);
out.writeObject(value);
out.flush();
return bos.toByteArray();
}

protected Object toObject(ClassLoader classLoader, byte[] value) throws IOException, ClassNotFoundException {
if (value == null) {
return null;
}
CustomObjectInputStream in = new CustomObjectInputStream(new ByteArrayInputStream(value), classLoader);
return in.readObject();
}
}
Expand Up @@ -28,11 +28,6 @@ public AttributeRemoveMessage() {
super();
}

public AttributeRemoveMessage(String sessionId, String name) {
super(sessionId);
this.name = name;
}

public AttributeRemoveMessage(String nodeId, String sessionId, String name) {
super(nodeId, sessionId);
this.name = name;
Expand Down
Expand Up @@ -15,6 +15,8 @@
*/
package org.redisson.tomcat;

import java.io.IOException;

/**
*
* @author Nikita Koksharov
Expand All @@ -23,29 +25,23 @@
public class AttributeUpdateMessage extends AttributeMessage {

private String name;
private Object value;
private byte[] value;

public AttributeUpdateMessage() {
}

public AttributeUpdateMessage(String sessionId, String name, Object value) {
super(sessionId);
this.name = name;
this.value = value;
}

public AttributeUpdateMessage(String nodeId, String sessionId, String name, Object value) {
public AttributeUpdateMessage(String nodeId, String sessionId, String name, Object value) throws IOException {
super(nodeId, sessionId);
this.name = name;
this.value = value;
this.value = toByteArray(value);
}

public String getName() {
return name;
}

public Object getValue() {
return value;
public Object getValue(ClassLoader classLoader) throws IOException, ClassNotFoundException {
return toObject(classLoader, value);
}

}
Expand Up @@ -25,10 +25,6 @@ public class AttributesClearMessage extends AttributeMessage {
public AttributesClearMessage() {
}

public AttributesClearMessage(String sessionId) {
super(sessionId);
}

public AttributesClearMessage(String nodeId, String sessionId) {
super(nodeId, sessionId);
}
Expand Down
Expand Up @@ -15,7 +15,10 @@
*/
package org.redisson.tomcat;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
*
Expand All @@ -24,23 +27,32 @@
*/
public class AttributesPutAllMessage extends AttributeMessage {

private Map<String, Object> attrs;
private Map<String, byte[]> attrs;

public AttributesPutAllMessage() {
}

public AttributesPutAllMessage(String sessionId, Map<String, Object> attrs) {
super(sessionId);
this.attrs = attrs;
}

public AttributesPutAllMessage(String nodeId, String sessionId, Map<String, Object> attrs) {
public AttributesPutAllMessage(String nodeId, String sessionId, Map<String, Object> attrs) throws IOException {
super(nodeId, sessionId);
this.attrs = attrs;
if (attrs != null) {
this.attrs = new HashMap<String, byte[]>();
for (Entry<String, Object> entry: attrs.entrySet()) {
this.attrs.put(entry.getKey(), toByteArray(entry.getValue()));
}
} else {
this.attrs = null;
}
}

public Map<String, Object> getAttrs() {
return attrs;
public Map<String, Object> getAttrs(ClassLoader classLoader) throws IOException, ClassNotFoundException {
if (attrs == null) {
return null;
}
Map<String, Object> result = new HashMap<String, Object>();
for (Entry<String, byte[]> entry: attrs.entrySet()) {
result.put(entry.getKey(), toObject(classLoader, entry.getValue()));
}
return result;
}

}
Expand Up @@ -15,6 +15,7 @@
*/
package org.redisson.tomcat;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -132,7 +133,11 @@ protected AttributesPutAllMessage createPutAllMessage(Map<String, Object> newMap
for (Entry<String, Object> entry : newMap.entrySet()) {
map.put(entry.getKey(), entry.getValue());
}
return new AttributesPutAllMessage(redissonManager.getNodeId(), getId(), map);
try {
return new AttributesPutAllMessage(redissonManager.getNodeId(), getId(), map);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}

@Override
Expand All @@ -148,7 +153,11 @@ public void setMaxInactiveInterval(int interval) {
private void fastPut(String name, Object value) {
map.fastPut(name, value);
if (readMode == ReadMode.MEMORY) {
topic.publish(new AttributeUpdateMessage(redissonManager.getNodeId(), getId(), name, value));
try {
topic.publish(new AttributeUpdateMessage(redissonManager.getNodeId(), getId(), name, value));
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
}

Expand Down
Expand Up @@ -158,7 +158,9 @@ public RMap<String, Object> getMap(String sessionId) {
}

public RTopic getTopic() {
return redisson.getTopic("redisson:tomcat_session_updates:" + container.getName());
String separator = keyPrefix == null || keyPrefix.isEmpty() ? "" : ":";
final String name = keyPrefix + separator + "redisson:tomcat_session_updates:" + ((Context) getContainer()).getName();
return redisson.getTopic(name);
}

@Override
Expand Down Expand Up @@ -228,6 +230,13 @@ public RedissonClient getRedisson() {
public void start() throws LifecycleException {
redisson = buildClient();

final ClassLoader applicationClassLoader;
if (Thread.currentThread().getContextClassLoader() != null) {
applicationClassLoader = Thread.currentThread().getContextClassLoader();
} else {
applicationClassLoader = getClass().getClassLoader();
}

if (updateMode == UpdateMode.AFTER_REQUEST) {
getEngine().getPipeline().addValve(new UpdateValve(this));
}
Expand All @@ -252,17 +261,17 @@ public void onMessage(CharSequence channel, AttributeMessage msg) {

if (msg instanceof AttributesPutAllMessage) {
AttributesPutAllMessage m = (AttributesPutAllMessage) msg;
for (Entry<String, Object> entry : m.getAttrs().entrySet()) {
for (Entry<String, Object> entry : m.getAttrs(applicationClassLoader).entrySet()) {
session.superSetAttribute(entry.getKey(), entry.getValue(), true);
}
}

if (msg instanceof AttributeUpdateMessage) {
AttributeUpdateMessage m = (AttributeUpdateMessage)msg;
session.superSetAttribute(m.getName(), m.getValue(), true);
session.superSetAttribute(m.getName(), m.getValue(applicationClassLoader), true);
}
}
} catch (IOException e) {
} catch (Exception e) {
log.error("Can't handle topic message", e);
}
}
Expand Down
Expand Up @@ -15,8 +15,14 @@
*/
package org.redisson.tomcat;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import org.apache.catalina.util.CustomObjectInputStream;

/**
*
* @author Nikita Koksharov
Expand All @@ -31,10 +37,6 @@ public class AttributeMessage implements Serializable {
public AttributeMessage() {
}

public AttributeMessage(String sessionId) {
this.sessionId = sessionId;
}

public AttributeMessage(String nodeId, String sessionId) {
this.nodeId = nodeId;
this.sessionId = sessionId;
Expand All @@ -48,4 +50,22 @@ public String getNodeId() {
return nodeId;
}

protected byte[] toByteArray(Object value) throws IOException {
if (value == null) {
return null;
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bos);
out.writeObject(value);
out.flush();
return bos.toByteArray();
}

protected Object toObject(ClassLoader classLoader, byte[] value) throws IOException, ClassNotFoundException {
if (value == null) {
return null;
}
CustomObjectInputStream in = new CustomObjectInputStream(new ByteArrayInputStream(value), classLoader);
return in.readObject();
}
}
Expand Up @@ -28,11 +28,6 @@ public AttributeRemoveMessage() {
super();
}

public AttributeRemoveMessage(String sessionId, String name) {
super(sessionId);
this.name = name;
}

public AttributeRemoveMessage(String nodeId, String sessionId, String name) {
super(nodeId, sessionId);
this.name = name;
Expand Down
Expand Up @@ -15,6 +15,8 @@
*/
package org.redisson.tomcat;

import java.io.IOException;

/**
*
* @author Nikita Koksharov
Expand All @@ -23,29 +25,23 @@
public class AttributeUpdateMessage extends AttributeMessage {

private String name;
private Object value;
private byte[] value;

public AttributeUpdateMessage() {
}

public AttributeUpdateMessage(String sessionId, String name, Object value) {
super(sessionId);
this.name = name;
this.value = value;
}

public AttributeUpdateMessage(String nodeId, String sessionId, String name, Object value) {
public AttributeUpdateMessage(String nodeId, String sessionId, String name, Object value) throws IOException {
super(nodeId, sessionId);
this.name = name;
this.value = value;
this.value = toByteArray(value);
}

public String getName() {
return name;
}

public Object getValue() {
return value;
public Object getValue(ClassLoader classLoader) throws IOException, ClassNotFoundException {
return toObject(classLoader, value);
}

}
Expand Up @@ -25,10 +25,6 @@ public class AttributesClearMessage extends AttributeMessage {
public AttributesClearMessage() {
}

public AttributesClearMessage(String sessionId) {
super(sessionId);
}

public AttributesClearMessage(String nodeId, String sessionId) {
super(nodeId, sessionId);
}
Expand Down

0 comments on commit ae14dba

Please sign in to comment.