Fixed - ClassNotFoundException arise in Tomcat session manager #1454

pull/1547/head
Nikita 7 years ago
parent 1a61d03655
commit bb491f60f1

@ -23,12 +23,12 @@ package org.redisson.tomcat;
public class AttributeUpdateMessage extends AttributeMessage {
private String name;
private Object value;
private byte[] value;
public AttributeUpdateMessage() {
}
public AttributeUpdateMessage(String sessionId, String name, Object value) {
public AttributeUpdateMessage(String sessionId, String name, byte[] value) {
super(sessionId);
this.name = name;
this.value = value;
@ -38,7 +38,7 @@ public class AttributeUpdateMessage extends AttributeMessage {
return name;
}
public Object getValue() {
public byte[] getValue() {
return value;
}

@ -24,17 +24,17 @@ import java.util.Map;
*/
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) {
public AttributesPutAllMessage(String sessionId, Map<String, byte[]> attrs) {
super(sessionId);
this.attrs = attrs;
}
public Map<String, Object> getAttrs() {
public Map<String, byte[]> getAttrs() {
return attrs;
}

@ -93,7 +93,7 @@ public class RedissonSession extends StandardSession {
newMap.put("session:thisAccessedTime", thisAccessedTime);
map.putAll(newMap);
if (readMode == ReadMode.MEMORY) {
topic.publish(new AttributesPutAllMessage(getId(), newMap));
topic.publish(createPutAllMessage(newMap));
}
}
}
@ -108,7 +108,7 @@ public class RedissonSession extends StandardSession {
newMap.put("session:thisAccessedTime", thisAccessedTime);
map.putAll(newMap);
if (readMode == ReadMode.MEMORY) {
topic.publish(new AttributesPutAllMessage(getId(), newMap));
topic.publish(createPutAllMessage(newMap));
}
if (getMaxInactiveInterval() >= 0) {
map.expire(getMaxInactiveInterval(), TimeUnit.SECONDS);
@ -116,6 +116,14 @@ public class RedissonSession extends StandardSession {
}
}
protected AttributesPutAllMessage createPutAllMessage(Map<String, Object> newMap) {
Map<String, byte[]> map = new HashMap<String, byte[]>();
for (Entry<String, Object> entry : newMap.entrySet()) {
map.put(entry.getKey(), redissonManager.encode(entry.getValue()));
}
return new AttributesPutAllMessage(getId(), map);
}
@Override
public void setMaxInactiveInterval(int interval) {
super.setMaxInactiveInterval(interval);
@ -131,7 +139,7 @@ public class RedissonSession extends StandardSession {
private void fastPut(String name, Object value) {
map.fastPut(name, value);
if (readMode == ReadMode.MEMORY) {
topic.publish(new AttributeUpdateMessage(getId(), name, value));
topic.publish(new AttributeUpdateMessage(getId(), name, redissonManager.encode(value)));
}
}
@ -213,7 +221,7 @@ public class RedissonSession extends StandardSession {
map.putAll(newMap);
if (readMode == ReadMode.MEMORY) {
topic.publish(new AttributesPutAllMessage(getId(), newMap));
topic.publish(createPutAllMessage(newMap));
}
if (maxInactiveInterval >= 0) {

@ -39,6 +39,9 @@ import org.redisson.api.listener.MessageListener;
import org.redisson.client.codec.Codec;
import org.redisson.config.Config;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
/**
* Redisson Session Manager for Apache Tomcat
*
@ -193,6 +196,27 @@ public class RedissonSessionManager extends ManagerBase implements Lifecycle {
}
}
protected Object decode(byte[] bb) throws IOException {
ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(bb.length);
buf.writeBytes(bb);
Object value = redisson.getConfig().getCodec().getValueDecoder().decode(buf, null);
buf.release();
return value;
}
public byte[] encode(Object value) {
try {
ByteBuf encoded = redisson.getConfig().getCodec().getValueEncoder().encode(value);
byte[] dst = new byte[encoded.readableBytes()];
encoded.readBytes(dst);
encoded.release();
return dst;
} catch (IOException e) {
log.error("Unable to encode object", e);
throw new IllegalStateException(e);
}
}
public RedissonClient getRedisson() {
return redisson;
}
@ -225,14 +249,14 @@ public class RedissonSessionManager extends ManagerBase implements Lifecycle {
if (msg instanceof AttributesPutAllMessage) {
AttributesPutAllMessage m = (AttributesPutAllMessage) msg;
for (Entry<String, Object> entry : m.getAttrs().entrySet()) {
session.superSetAttribute(entry.getKey(), entry.getValue(), true);
for (Entry<String, byte[]> entry : m.getAttrs().entrySet()) {
session.superSetAttribute(entry.getKey(), decode(entry.getValue()), true);
}
}
if (msg instanceof AttributeUpdateMessage) {
AttributeUpdateMessage m = (AttributeUpdateMessage)msg;
session.superSetAttribute(m.getName(), m.getValue(), true);
session.superSetAttribute(m.getName(), decode(m.getValue()), true);
}
}
} catch (IOException e) {

@ -23,12 +23,12 @@ package org.redisson.tomcat;
public class AttributeUpdateMessage extends AttributeMessage {
private String name;
private Object value;
private byte[] value;
public AttributeUpdateMessage() {
}
public AttributeUpdateMessage(String sessionId, String name, Object value) {
public AttributeUpdateMessage(String sessionId, String name, byte[] value) {
super(sessionId);
this.name = name;
this.value = value;
@ -38,7 +38,7 @@ public class AttributeUpdateMessage extends AttributeMessage {
return name;
}
public Object getValue() {
public byte[] getValue() {
return value;
}

@ -24,17 +24,17 @@ import java.util.Map;
*/
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) {
public AttributesPutAllMessage(String sessionId, Map<String, byte[]> attrs) {
super(sessionId);
this.attrs = attrs;
}
public Map<String, Object> getAttrs() {
public Map<String, byte[]> getAttrs() {
return attrs;
}

@ -93,7 +93,7 @@ public class RedissonSession extends StandardSession {
newMap.put("session:thisAccessedTime", thisAccessedTime);
map.putAll(newMap);
if (readMode == ReadMode.MEMORY) {
topic.publish(new AttributesPutAllMessage(getId(), newMap));
topic.publish(createPutAllMessage(newMap));
}
}
}
@ -108,13 +108,21 @@ public class RedissonSession extends StandardSession {
newMap.put("session:thisAccessedTime", thisAccessedTime);
map.putAll(newMap);
if (readMode == ReadMode.MEMORY) {
topic.publish(new AttributesPutAllMessage(getId(), newMap));
topic.publish(createPutAllMessage(newMap));
}
if (getMaxInactiveInterval() >= 0) {
map.expire(getMaxInactiveInterval(), TimeUnit.SECONDS);
}
}
}
protected AttributesPutAllMessage createPutAllMessage(Map<String, Object> newMap) {
Map<String, byte[]> map = new HashMap<String, byte[]>();
for (Entry<String, Object> entry : newMap.entrySet()) {
map.put(entry.getKey(), redissonManager.encode(entry.getValue()));
}
return new AttributesPutAllMessage(getId(), map);
}
@Override
public void setMaxInactiveInterval(int interval) {
@ -131,7 +139,7 @@ public class RedissonSession extends StandardSession {
private void fastPut(String name, Object value) {
map.fastPut(name, value);
if (readMode == ReadMode.MEMORY) {
topic.publish(new AttributeUpdateMessage(getId(), name, value));
topic.publish(new AttributeUpdateMessage(getId(), name, redissonManager.encode(value)));
}
}
@ -213,7 +221,7 @@ public class RedissonSession extends StandardSession {
map.putAll(newMap);
if (readMode == ReadMode.MEMORY) {
topic.publish(new AttributesPutAllMessage(getId(), newMap));
topic.publish(createPutAllMessage(newMap));
}
if (maxInactiveInterval >= 0) {

@ -37,6 +37,9 @@ import org.redisson.api.listener.MessageListener;
import org.redisson.client.codec.Codec;
import org.redisson.config.Config;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
/**
* Redisson Session Manager for Apache Tomcat
*
@ -172,6 +175,27 @@ public class RedissonSessionManager extends ManagerBase {
}
}
protected Object decode(byte[] bb) throws IOException {
ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(bb.length);
buf.writeBytes(bb);
Object value = redisson.getConfig().getCodec().getValueDecoder().decode(buf, null);
buf.release();
return value;
}
public byte[] encode(Object value) {
try {
ByteBuf encoded = redisson.getConfig().getCodec().getValueEncoder().encode(value);
byte[] dst = new byte[encoded.readableBytes()];
encoded.readBytes(dst);
encoded.release();
return dst;
} catch (IOException e) {
log.error("Unable to encode object", e);
throw new IllegalStateException(e);
}
}
public RedissonClient getRedisson() {
return redisson;
}
@ -205,14 +229,14 @@ public class RedissonSessionManager extends ManagerBase {
if (msg instanceof AttributesPutAllMessage) {
AttributesPutAllMessage m = (AttributesPutAllMessage) msg;
for (Entry<String, Object> entry : m.getAttrs().entrySet()) {
session.superSetAttribute(entry.getKey(), entry.getValue(), true);
for (Entry<String, byte[]> entry : m.getAttrs().entrySet()) {
session.superSetAttribute(entry.getKey(), decode(entry.getValue()), true);
}
}
if (msg instanceof AttributeUpdateMessage) {
AttributeUpdateMessage m = (AttributeUpdateMessage)msg;
session.superSetAttribute(m.getName(), m.getValue(), true);
session.superSetAttribute(m.getName(), decode(m.getValue()), true);
}
}
} catch (IOException e) {

@ -23,12 +23,12 @@ package org.redisson.tomcat;
public class AttributeUpdateMessage extends AttributeMessage {
private String name;
private Object value;
private byte[] value;
public AttributeUpdateMessage() {
}
public AttributeUpdateMessage(String sessionId, String name, Object value) {
public AttributeUpdateMessage(String sessionId, String name, byte[] value) {
super(sessionId);
this.name = name;
this.value = value;
@ -38,7 +38,7 @@ public class AttributeUpdateMessage extends AttributeMessage {
return name;
}
public Object getValue() {
public byte[] getValue() {
return value;
}

@ -24,17 +24,17 @@ import java.util.Map;
*/
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) {
public AttributesPutAllMessage(String sessionId, Map<String, byte[]> attrs) {
super(sessionId);
this.attrs = attrs;
}
public Map<String, Object> getAttrs() {
public Map<String, byte[]> getAttrs() {
return attrs;
}

@ -93,7 +93,7 @@ public class RedissonSession extends StandardSession {
newMap.put("session:thisAccessedTime", thisAccessedTime);
map.putAll(newMap);
if (readMode == ReadMode.MEMORY) {
topic.publish(new AttributesPutAllMessage(getId(), newMap));
topic.publish(createPutAllMessage(newMap));
}
}
}
@ -108,7 +108,7 @@ public class RedissonSession extends StandardSession {
newMap.put("session:thisAccessedTime", thisAccessedTime);
map.putAll(newMap);
if (readMode == ReadMode.MEMORY) {
topic.publish(new AttributesPutAllMessage(getId(), newMap));
topic.publish(createPutAllMessage(newMap));
}
if (getMaxInactiveInterval() >= 0) {
map.expire(getMaxInactiveInterval(), TimeUnit.SECONDS);
@ -116,6 +116,14 @@ public class RedissonSession extends StandardSession {
}
}
protected AttributesPutAllMessage createPutAllMessage(Map<String, Object> newMap) {
Map<String, byte[]> map = new HashMap<String, byte[]>();
for (Entry<String, Object> entry : newMap.entrySet()) {
map.put(entry.getKey(), redissonManager.encode(entry.getValue()));
}
return new AttributesPutAllMessage(getId(), map);
}
@Override
public void setMaxInactiveInterval(int interval) {
super.setMaxInactiveInterval(interval);
@ -131,7 +139,7 @@ public class RedissonSession extends StandardSession {
private void fastPut(String name, Object value) {
map.fastPut(name, value);
if (readMode == ReadMode.MEMORY) {
topic.publish(new AttributeUpdateMessage(getId(), name, value));
topic.publish(new AttributeUpdateMessage(getId(), name, redissonManager.encode(value)));
}
}
@ -213,7 +221,7 @@ public class RedissonSession extends StandardSession {
map.putAll(newMap);
if (readMode == ReadMode.MEMORY) {
topic.publish(new AttributesPutAllMessage(getId(), newMap));
topic.publish(createPutAllMessage(newMap));
}
if (maxInactiveInterval >= 0) {

@ -36,6 +36,9 @@ import org.redisson.api.listener.MessageListener;
import org.redisson.client.codec.Codec;
import org.redisson.config.Config;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
/**
* Redisson Session Manager for Apache Tomcat
*
@ -171,6 +174,27 @@ public class RedissonSessionManager extends ManagerBase {
}
}
protected Object decode(byte[] bb) throws IOException {
ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(bb.length);
buf.writeBytes(bb);
Object value = redisson.getConfig().getCodec().getValueDecoder().decode(buf, null);
buf.release();
return value;
}
public byte[] encode(Object value) {
try {
ByteBuf encoded = redisson.getConfig().getCodec().getValueEncoder().encode(value);
byte[] dst = new byte[encoded.readableBytes()];
encoded.readBytes(dst);
encoded.release();
return dst;
} catch (IOException e) {
log.error("Unable to encode object", e);
throw new IllegalStateException(e);
}
}
public RedissonClient getRedisson() {
return redisson;
}
@ -204,14 +228,14 @@ public class RedissonSessionManager extends ManagerBase {
if (msg instanceof AttributesPutAllMessage) {
AttributesPutAllMessage m = (AttributesPutAllMessage) msg;
for (Entry<String, Object> entry : m.getAttrs().entrySet()) {
session.superSetAttribute(entry.getKey(), entry.getValue(), true);
for (Entry<String, byte[]> entry : m.getAttrs().entrySet()) {
session.superSetAttribute(entry.getKey(), decode(entry.getValue()), true);
}
}
if (msg instanceof AttributeUpdateMessage) {
AttributeUpdateMessage m = (AttributeUpdateMessage)msg;
session.superSetAttribute(m.getName(), m.getValue(), true);
session.superSetAttribute(m.getName(), decode(m.getValue()), true);
}
}
} catch (IOException e) {
@ -239,10 +263,14 @@ public class RedissonSessionManager extends ManagerBase {
}
try {
try {
Config c = new Config(config);
Codec codec = c.getCodec().getClass().getConstructor(ClassLoader.class)
.newInstance(Thread.currentThread().getContextClassLoader());
config.setCodec(codec);
} catch (Exception e) {
throw new IllegalStateException("Unable to initialize codec with ClassLoader parameter", e);
}
return Redisson.create(config);
} catch (Exception e) {

@ -23,12 +23,12 @@ package org.redisson.tomcat;
public class AttributeUpdateMessage extends AttributeMessage {
private String name;
private Object value;
private byte[] value;
public AttributeUpdateMessage() {
}
public AttributeUpdateMessage(String sessionId, String name, Object value) {
public AttributeUpdateMessage(String sessionId, String name, byte[] value) {
super(sessionId);
this.name = name;
this.value = value;
@ -38,7 +38,7 @@ public class AttributeUpdateMessage extends AttributeMessage {
return name;
}
public Object getValue() {
public byte[] getValue() {
return value;
}

@ -24,17 +24,17 @@ import java.util.Map;
*/
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) {
public AttributesPutAllMessage(String sessionId, Map<String, byte[]> attrs) {
super(sessionId);
this.attrs = attrs;
}
public Map<String, Object> getAttrs() {
public Map<String, byte[]> getAttrs() {
return attrs;
}

@ -93,7 +93,7 @@ public class RedissonSession extends StandardSession {
newMap.put("session:thisAccessedTime", thisAccessedTime);
map.putAll(newMap);
if (readMode == ReadMode.MEMORY) {
topic.publish(new AttributesPutAllMessage(getId(), newMap));
topic.publish(createPutAllMessage(newMap));
}
}
}
@ -108,7 +108,7 @@ public class RedissonSession extends StandardSession {
newMap.put("session:thisAccessedTime", thisAccessedTime);
map.putAll(newMap);
if (readMode == ReadMode.MEMORY) {
topic.publish(new AttributesPutAllMessage(getId(), newMap));
topic.publish(createPutAllMessage(newMap));
}
if (getMaxInactiveInterval() >= 0) {
map.expire(getMaxInactiveInterval(), TimeUnit.SECONDS);
@ -116,6 +116,14 @@ public class RedissonSession extends StandardSession {
}
}
protected AttributesPutAllMessage createPutAllMessage(Map<String, Object> newMap) {
Map<String, byte[]> map = new HashMap<String, byte[]>();
for (Entry<String, Object> entry : newMap.entrySet()) {
map.put(entry.getKey(), redissonManager.encode(entry.getValue()));
}
return new AttributesPutAllMessage(getId(), map);
}
@Override
public void setMaxInactiveInterval(int interval) {
super.setMaxInactiveInterval(interval);
@ -131,7 +139,7 @@ public class RedissonSession extends StandardSession {
private void fastPut(String name, Object value) {
map.fastPut(name, value);
if (readMode == ReadMode.MEMORY) {
topic.publish(new AttributeUpdateMessage(getId(), name, value));
topic.publish(new AttributeUpdateMessage(getId(), name, redissonManager.encode(value)));
}
}
@ -213,7 +221,7 @@ public class RedissonSession extends StandardSession {
map.putAll(newMap);
if (readMode == ReadMode.MEMORY) {
topic.publish(new AttributesPutAllMessage(getId(), newMap));
topic.publish(createPutAllMessage(newMap));
}
if (maxInactiveInterval >= 0) {

@ -36,6 +36,9 @@ import org.redisson.api.listener.MessageListener;
import org.redisson.client.codec.Codec;
import org.redisson.config.Config;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
/**
* Redisson Session Manager for Apache Tomcat
*
@ -171,6 +174,27 @@ public class RedissonSessionManager extends ManagerBase {
}
}
protected Object decode(byte[] bb) throws IOException {
ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(bb.length);
buf.writeBytes(bb);
Object value = redisson.getConfig().getCodec().getValueDecoder().decode(buf, null);
buf.release();
return value;
}
public byte[] encode(Object value) {
try {
ByteBuf encoded = redisson.getConfig().getCodec().getValueEncoder().encode(value);
byte[] dst = new byte[encoded.readableBytes()];
encoded.readBytes(dst);
encoded.release();
return dst;
} catch (IOException e) {
log.error("Unable to encode object", e);
throw new IllegalStateException(e);
}
}
public RedissonClient getRedisson() {
return redisson;
}
@ -204,14 +228,14 @@ public class RedissonSessionManager extends ManagerBase {
if (msg instanceof AttributesPutAllMessage) {
AttributesPutAllMessage m = (AttributesPutAllMessage) msg;
for (Entry<String, Object> entry : m.getAttrs().entrySet()) {
session.superSetAttribute(entry.getKey(), entry.getValue(), true);
for (Entry<String, byte[]> entry : m.getAttrs().entrySet()) {
session.superSetAttribute(entry.getKey(), decode(entry.getValue()), true);
}
}
if (msg instanceof AttributeUpdateMessage) {
AttributeUpdateMessage m = (AttributeUpdateMessage)msg;
session.superSetAttribute(m.getName(), m.getValue(), true);
session.superSetAttribute(m.getName(), decode(m.getValue()), true);
}
}
} catch (IOException e) {
@ -239,10 +263,14 @@ public class RedissonSessionManager extends ManagerBase {
}
try {
try {
Config c = new Config(config);
Codec codec = c.getCodec().getClass().getConstructor(ClassLoader.class)
.newInstance(Thread.currentThread().getContextClassLoader());
config.setCodec(codec);
} catch (Exception e) {
throw new IllegalStateException("Unable to initialize codec with ClassLoader parameter", e);
}
return Redisson.create(config);
} catch (Exception e) {

Loading…
Cancel
Save