From ee51283731cc61e761371092350ea758b396b1ec Mon Sep 17 00:00:00 2001 From: Nikita Koksharov Date: Wed, 6 Mar 2019 11:27:09 +0300 Subject: [PATCH] Fixed - codec provided in Redisson configuration should be used for attribute messages serialization. #1905 --- .../org/redisson/tomcat/AttributeMessage.java | 35 ++++++++++++------- .../tomcat/AttributeUpdateMessage.java | 11 +++--- .../tomcat/AttributesPutAllMessage.java | 11 +++--- .../org/redisson/tomcat/RedissonSession.java | 4 +-- .../tomcat/RedissonSessionManager.java | 23 ++++++------ .../org/redisson/tomcat/AttributeMessage.java | 35 ++++++++++++------- .../tomcat/AttributeUpdateMessage.java | 11 +++--- .../tomcat/AttributesPutAllMessage.java | 11 +++--- .../org/redisson/tomcat/RedissonSession.java | 4 +-- .../tomcat/RedissonSessionManager.java | 15 ++++++-- .../org/redisson/tomcat/AttributeMessage.java | 35 ++++++++++++------- .../tomcat/AttributeUpdateMessage.java | 11 +++--- .../tomcat/AttributesPutAllMessage.java | 11 +++--- .../org/redisson/tomcat/RedissonSession.java | 4 +-- .../tomcat/RedissonSessionManager.java | 15 ++++++-- .../org/redisson/tomcat/AttributeMessage.java | 35 ++++++++++++------- .../tomcat/AttributeUpdateMessage.java | 11 +++--- .../tomcat/AttributesPutAllMessage.java | 11 +++--- .../org/redisson/tomcat/RedissonSession.java | 4 +-- .../tomcat/RedissonSessionManager.java | 15 ++++++-- 20 files changed, 203 insertions(+), 109 deletions(-) diff --git a/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/AttributeMessage.java b/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/AttributeMessage.java index abd840557..d072fb6a5 100644 --- a/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/AttributeMessage.java +++ b/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/AttributeMessage.java @@ -15,13 +15,15 @@ */ 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; +import org.redisson.client.protocol.Decoder; +import org.redisson.client.protocol.Encoder; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufUtil; +import io.netty.buffer.Unpooled; /** * @@ -50,22 +52,29 @@ public class AttributeMessage implements Serializable { return nodeId; } - protected byte[] toByteArray(Object value) throws IOException { + protected byte[] toByteArray(Encoder encoder, 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(); + + ByteBuf buf = encoder.encode(value); + try { + return ByteBufUtil.getBytes(buf); + } finally { + buf.release(); + } } - protected Object toObject(ClassLoader classLoader, byte[] value) throws IOException, ClassNotFoundException { + protected Object toObject(Decoder decoder, byte[] value) throws IOException, ClassNotFoundException { if (value == null) { return null; } - CustomObjectInputStream in = new CustomObjectInputStream(new ByteArrayInputStream(value), classLoader); - return in.readObject(); + + ByteBuf buf = Unpooled.wrappedBuffer(value); + try { + return decoder.decode(buf, null); + } finally { + buf.release(); + } } } diff --git a/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/AttributeUpdateMessage.java b/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/AttributeUpdateMessage.java index 7c64b19f0..85961eef1 100644 --- a/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/AttributeUpdateMessage.java +++ b/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/AttributeUpdateMessage.java @@ -17,6 +17,9 @@ package org.redisson.tomcat; import java.io.IOException; +import org.redisson.client.protocol.Decoder; +import org.redisson.client.protocol.Encoder; + /** * * @author Nikita Koksharov @@ -30,18 +33,18 @@ public class AttributeUpdateMessage extends AttributeMessage { public AttributeUpdateMessage() { } - public AttributeUpdateMessage(String nodeId, String sessionId, String name, Object value) throws IOException { + public AttributeUpdateMessage(String nodeId, String sessionId, String name, Object value, Encoder encoder) throws IOException { super(nodeId, sessionId); this.name = name; - this.value = toByteArray(value); + this.value = toByteArray(encoder, value); } public String getName() { return name; } - public Object getValue(ClassLoader classLoader) throws IOException, ClassNotFoundException { - return toObject(classLoader, value); + public Object getValue(Decoder decoder) throws IOException, ClassNotFoundException { + return toObject(decoder, value); } } diff --git a/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/AttributesPutAllMessage.java b/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/AttributesPutAllMessage.java index 0e097c8ad..4fe5c8cf5 100644 --- a/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/AttributesPutAllMessage.java +++ b/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/AttributesPutAllMessage.java @@ -20,6 +20,9 @@ import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; +import org.redisson.client.protocol.Decoder; +import org.redisson.client.protocol.Encoder; + /** * * @author Nikita Koksharov @@ -32,25 +35,25 @@ public class AttributesPutAllMessage extends AttributeMessage { public AttributesPutAllMessage() { } - public AttributesPutAllMessage(String nodeId, String sessionId, Map attrs) throws IOException { + public AttributesPutAllMessage(String nodeId, String sessionId, Map attrs, Encoder encoder) throws IOException { super(nodeId, sessionId); if (attrs != null) { this.attrs = new HashMap(); for (Entry entry: attrs.entrySet()) { - this.attrs.put(entry.getKey(), toByteArray(entry.getValue())); + this.attrs.put(entry.getKey(), toByteArray(encoder, entry.getValue())); } } else { this.attrs = null; } } - public Map getAttrs(ClassLoader classLoader) throws IOException, ClassNotFoundException { + public Map getAttrs(Decoder decoder) throws IOException, ClassNotFoundException { if (attrs == null) { return null; } Map result = new HashMap(); for (Entry entry: attrs.entrySet()) { - result.put(entry.getKey(), toObject(classLoader, entry.getValue())); + result.put(entry.getKey(), toObject(decoder, entry.getValue())); } return result; } diff --git a/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/RedissonSession.java b/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/RedissonSession.java index a892b9029..e3384dbce 100644 --- a/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/RedissonSession.java +++ b/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/RedissonSession.java @@ -187,7 +187,7 @@ public class RedissonSession extends StandardSession { map.put(entry.getKey(), entry.getValue()); } try { - return new AttributesPutAllMessage(redissonManager.getNodeId(), getId(), map); + return new AttributesPutAllMessage(redissonManager.getNodeId(), getId(), map, this.map.getCodec().getMapValueEncoder()); } catch (IOException e) { throw new IllegalStateException(e); } @@ -207,7 +207,7 @@ public class RedissonSession extends StandardSession { map.fastPut(name, value); if (readMode == ReadMode.MEMORY) { try { - topic.publish(new AttributeUpdateMessage(redissonManager.getNodeId(), getId(), name, value)); + topic.publish(new AttributeUpdateMessage(redissonManager.getNodeId(), getId(), name, value, this.map.getCodec().getMapValueEncoder())); } catch (IOException e) { throw new IllegalStateException(e); } diff --git a/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/RedissonSessionManager.java b/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/RedissonSessionManager.java index c9b253d91..642d18ae5 100644 --- a/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/RedissonSessionManager.java +++ b/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/RedissonSessionManager.java @@ -235,6 +235,16 @@ public class RedissonSessionManager extends ManagerBase implements Lifecycle { applicationClassLoader = getClass().getClassLoader(); } + Codec codec = redisson.getConfig().getCodec(); + Codec codecToUse; + try { + codecToUse = codec.getClass() + .getConstructor(ClassLoader.class, codec.getClass()) + .newInstance(applicationClassLoader, codec); + } catch (Exception e) { + throw new LifecycleException(e); + } + if (updateMode == UpdateMode.AFTER_REQUEST) { getEngine().getPipeline().addValve(new UpdateValve(this)); } @@ -259,14 +269,14 @@ public class RedissonSessionManager extends ManagerBase implements Lifecycle { if (msg instanceof AttributesPutAllMessage) { AttributesPutAllMessage m = (AttributesPutAllMessage) msg; - for (Entry entry : m.getAttrs(applicationClassLoader).entrySet()) { + for (Entry entry : m.getAttrs(codecToUse.getMapValueDecoder()).entrySet()) { session.superSetAttribute(entry.getKey(), entry.getValue(), true); } } if (msg instanceof AttributeUpdateMessage) { AttributeUpdateMessage m = (AttributeUpdateMessage)msg; - session.superSetAttribute(m.getName(), m.getValue(applicationClassLoader), true); + session.superSetAttribute(m.getName(), m.getValue(codecToUse.getMapValueDecoder()), true); } } } catch (Exception e) { @@ -294,15 +304,6 @@ public class RedissonSessionManager extends ManagerBase implements Lifecycle { } 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) { throw new LifecycleException(e); diff --git a/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/AttributeMessage.java b/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/AttributeMessage.java index abd840557..d072fb6a5 100644 --- a/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/AttributeMessage.java +++ b/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/AttributeMessage.java @@ -15,13 +15,15 @@ */ 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; +import org.redisson.client.protocol.Decoder; +import org.redisson.client.protocol.Encoder; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufUtil; +import io.netty.buffer.Unpooled; /** * @@ -50,22 +52,29 @@ public class AttributeMessage implements Serializable { return nodeId; } - protected byte[] toByteArray(Object value) throws IOException { + protected byte[] toByteArray(Encoder encoder, 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(); + + ByteBuf buf = encoder.encode(value); + try { + return ByteBufUtil.getBytes(buf); + } finally { + buf.release(); + } } - protected Object toObject(ClassLoader classLoader, byte[] value) throws IOException, ClassNotFoundException { + protected Object toObject(Decoder decoder, byte[] value) throws IOException, ClassNotFoundException { if (value == null) { return null; } - CustomObjectInputStream in = new CustomObjectInputStream(new ByteArrayInputStream(value), classLoader); - return in.readObject(); + + ByteBuf buf = Unpooled.wrappedBuffer(value); + try { + return decoder.decode(buf, null); + } finally { + buf.release(); + } } } diff --git a/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/AttributeUpdateMessage.java b/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/AttributeUpdateMessage.java index 7c64b19f0..85961eef1 100644 --- a/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/AttributeUpdateMessage.java +++ b/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/AttributeUpdateMessage.java @@ -17,6 +17,9 @@ package org.redisson.tomcat; import java.io.IOException; +import org.redisson.client.protocol.Decoder; +import org.redisson.client.protocol.Encoder; + /** * * @author Nikita Koksharov @@ -30,18 +33,18 @@ public class AttributeUpdateMessage extends AttributeMessage { public AttributeUpdateMessage() { } - public AttributeUpdateMessage(String nodeId, String sessionId, String name, Object value) throws IOException { + public AttributeUpdateMessage(String nodeId, String sessionId, String name, Object value, Encoder encoder) throws IOException { super(nodeId, sessionId); this.name = name; - this.value = toByteArray(value); + this.value = toByteArray(encoder, value); } public String getName() { return name; } - public Object getValue(ClassLoader classLoader) throws IOException, ClassNotFoundException { - return toObject(classLoader, value); + public Object getValue(Decoder decoder) throws IOException, ClassNotFoundException { + return toObject(decoder, value); } } diff --git a/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/AttributesPutAllMessage.java b/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/AttributesPutAllMessage.java index 0e097c8ad..4fe5c8cf5 100644 --- a/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/AttributesPutAllMessage.java +++ b/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/AttributesPutAllMessage.java @@ -20,6 +20,9 @@ import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; +import org.redisson.client.protocol.Decoder; +import org.redisson.client.protocol.Encoder; + /** * * @author Nikita Koksharov @@ -32,25 +35,25 @@ public class AttributesPutAllMessage extends AttributeMessage { public AttributesPutAllMessage() { } - public AttributesPutAllMessage(String nodeId, String sessionId, Map attrs) throws IOException { + public AttributesPutAllMessage(String nodeId, String sessionId, Map attrs, Encoder encoder) throws IOException { super(nodeId, sessionId); if (attrs != null) { this.attrs = new HashMap(); for (Entry entry: attrs.entrySet()) { - this.attrs.put(entry.getKey(), toByteArray(entry.getValue())); + this.attrs.put(entry.getKey(), toByteArray(encoder, entry.getValue())); } } else { this.attrs = null; } } - public Map getAttrs(ClassLoader classLoader) throws IOException, ClassNotFoundException { + public Map getAttrs(Decoder decoder) throws IOException, ClassNotFoundException { if (attrs == null) { return null; } Map result = new HashMap(); for (Entry entry: attrs.entrySet()) { - result.put(entry.getKey(), toObject(classLoader, entry.getValue())); + result.put(entry.getKey(), toObject(decoder, entry.getValue())); } return result; } diff --git a/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/RedissonSession.java b/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/RedissonSession.java index 3a783fdf0..6eeeff83c 100644 --- a/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/RedissonSession.java +++ b/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/RedissonSession.java @@ -187,7 +187,7 @@ public class RedissonSession extends StandardSession { map.put(entry.getKey(), entry.getValue()); } try { - return new AttributesPutAllMessage(redissonManager.getNodeId(), getId(), map); + return new AttributesPutAllMessage(redissonManager.getNodeId(), getId(), map, this.map.getCodec().getMapValueEncoder()); } catch (IOException e) { throw new IllegalStateException(e); } @@ -207,7 +207,7 @@ public class RedissonSession extends StandardSession { map.fastPut(name, value); if (readMode == ReadMode.MEMORY) { try { - topic.publish(new AttributeUpdateMessage(redissonManager.getNodeId(), getId(), name, value)); + topic.publish(new AttributeUpdateMessage(redissonManager.getNodeId(), getId(), name, value, this.map.getCodec().getMapValueEncoder())); } catch (IOException e) { throw new IllegalStateException(e); } diff --git a/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/RedissonSessionManager.java b/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/RedissonSessionManager.java index 4fa38c3bb..8b89f8e90 100644 --- a/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/RedissonSessionManager.java +++ b/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/RedissonSessionManager.java @@ -36,6 +36,7 @@ import org.redisson.api.RMap; import org.redisson.api.RTopic; import org.redisson.api.RedissonClient; import org.redisson.api.listener.MessageListener; +import org.redisson.client.codec.Codec; import org.redisson.client.codec.StringCodec; import org.redisson.codec.CompositeCodec; import org.redisson.config.Config; @@ -214,6 +215,16 @@ public class RedissonSessionManager extends ManagerBase { applicationClassLoader = getClass().getClassLoader(); } + Codec codec = redisson.getConfig().getCodec(); + Codec codecToUse; + try { + codecToUse = codec.getClass() + .getConstructor(ClassLoader.class, codec.getClass()) + .newInstance(applicationClassLoader, codec); + } catch (Exception e) { + throw new LifecycleException(e); + } + if (updateMode == UpdateMode.AFTER_REQUEST) { getEngine().getPipeline().addValve(new UpdateValve(this)); } @@ -238,14 +249,14 @@ public class RedissonSessionManager extends ManagerBase { if (msg instanceof AttributesPutAllMessage) { AttributesPutAllMessage m = (AttributesPutAllMessage) msg; - for (Entry entry : m.getAttrs(applicationClassLoader).entrySet()) { + for (Entry entry : m.getAttrs(codecToUse.getMapValueDecoder()).entrySet()) { session.superSetAttribute(entry.getKey(), entry.getValue(), true); } } if (msg instanceof AttributeUpdateMessage) { AttributeUpdateMessage m = (AttributeUpdateMessage)msg; - session.superSetAttribute(m.getName(), m.getValue(applicationClassLoader), true); + session.superSetAttribute(m.getName(), m.getValue(codecToUse.getMapValueDecoder()), true); } } } catch (Exception e) { diff --git a/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/AttributeMessage.java b/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/AttributeMessage.java index abd840557..d072fb6a5 100644 --- a/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/AttributeMessage.java +++ b/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/AttributeMessage.java @@ -15,13 +15,15 @@ */ 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; +import org.redisson.client.protocol.Decoder; +import org.redisson.client.protocol.Encoder; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufUtil; +import io.netty.buffer.Unpooled; /** * @@ -50,22 +52,29 @@ public class AttributeMessage implements Serializable { return nodeId; } - protected byte[] toByteArray(Object value) throws IOException { + protected byte[] toByteArray(Encoder encoder, 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(); + + ByteBuf buf = encoder.encode(value); + try { + return ByteBufUtil.getBytes(buf); + } finally { + buf.release(); + } } - protected Object toObject(ClassLoader classLoader, byte[] value) throws IOException, ClassNotFoundException { + protected Object toObject(Decoder decoder, byte[] value) throws IOException, ClassNotFoundException { if (value == null) { return null; } - CustomObjectInputStream in = new CustomObjectInputStream(new ByteArrayInputStream(value), classLoader); - return in.readObject(); + + ByteBuf buf = Unpooled.wrappedBuffer(value); + try { + return decoder.decode(buf, null); + } finally { + buf.release(); + } } } diff --git a/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/AttributeUpdateMessage.java b/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/AttributeUpdateMessage.java index 7c64b19f0..85961eef1 100644 --- a/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/AttributeUpdateMessage.java +++ b/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/AttributeUpdateMessage.java @@ -17,6 +17,9 @@ package org.redisson.tomcat; import java.io.IOException; +import org.redisson.client.protocol.Decoder; +import org.redisson.client.protocol.Encoder; + /** * * @author Nikita Koksharov @@ -30,18 +33,18 @@ public class AttributeUpdateMessage extends AttributeMessage { public AttributeUpdateMessage() { } - public AttributeUpdateMessage(String nodeId, String sessionId, String name, Object value) throws IOException { + public AttributeUpdateMessage(String nodeId, String sessionId, String name, Object value, Encoder encoder) throws IOException { super(nodeId, sessionId); this.name = name; - this.value = toByteArray(value); + this.value = toByteArray(encoder, value); } public String getName() { return name; } - public Object getValue(ClassLoader classLoader) throws IOException, ClassNotFoundException { - return toObject(classLoader, value); + public Object getValue(Decoder decoder) throws IOException, ClassNotFoundException { + return toObject(decoder, value); } } diff --git a/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/AttributesPutAllMessage.java b/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/AttributesPutAllMessage.java index 0e097c8ad..4fe5c8cf5 100644 --- a/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/AttributesPutAllMessage.java +++ b/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/AttributesPutAllMessage.java @@ -20,6 +20,9 @@ import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; +import org.redisson.client.protocol.Decoder; +import org.redisson.client.protocol.Encoder; + /** * * @author Nikita Koksharov @@ -32,25 +35,25 @@ public class AttributesPutAllMessage extends AttributeMessage { public AttributesPutAllMessage() { } - public AttributesPutAllMessage(String nodeId, String sessionId, Map attrs) throws IOException { + public AttributesPutAllMessage(String nodeId, String sessionId, Map attrs, Encoder encoder) throws IOException { super(nodeId, sessionId); if (attrs != null) { this.attrs = new HashMap(); for (Entry entry: attrs.entrySet()) { - this.attrs.put(entry.getKey(), toByteArray(entry.getValue())); + this.attrs.put(entry.getKey(), toByteArray(encoder, entry.getValue())); } } else { this.attrs = null; } } - public Map getAttrs(ClassLoader classLoader) throws IOException, ClassNotFoundException { + public Map getAttrs(Decoder decoder) throws IOException, ClassNotFoundException { if (attrs == null) { return null; } Map result = new HashMap(); for (Entry entry: attrs.entrySet()) { - result.put(entry.getKey(), toObject(classLoader, entry.getValue())); + result.put(entry.getKey(), toObject(decoder, entry.getValue())); } return result; } diff --git a/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/RedissonSession.java b/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/RedissonSession.java index a273225ad..760e35f33 100644 --- a/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/RedissonSession.java +++ b/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/RedissonSession.java @@ -187,7 +187,7 @@ public class RedissonSession extends StandardSession { map.put(entry.getKey(), entry.getValue()); } try { - return new AttributesPutAllMessage(redissonManager.getNodeId(), getId(), map); + return new AttributesPutAllMessage(redissonManager.getNodeId(), getId(), map, this.map.getCodec().getMapValueEncoder()); } catch (IOException e) { throw new IllegalStateException(e); } @@ -207,7 +207,7 @@ public class RedissonSession extends StandardSession { map.fastPut(name, value); if (readMode == ReadMode.MEMORY) { try { - topic.publish(new AttributeUpdateMessage(redissonManager.getNodeId(), getId(), name, value)); + topic.publish(new AttributeUpdateMessage(redissonManager.getNodeId(), getId(), name, value, this.map.getCodec().getMapValueEncoder())); } catch (IOException e) { throw new IllegalStateException(e); } diff --git a/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/RedissonSessionManager.java b/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/RedissonSessionManager.java index 8fc03d2bb..813bc6321 100644 --- a/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/RedissonSessionManager.java +++ b/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/RedissonSessionManager.java @@ -35,6 +35,7 @@ import org.redisson.api.RMap; import org.redisson.api.RTopic; import org.redisson.api.RedissonClient; import org.redisson.api.listener.MessageListener; +import org.redisson.client.codec.Codec; import org.redisson.client.codec.StringCodec; import org.redisson.codec.CompositeCodec; import org.redisson.config.Config; @@ -213,6 +214,16 @@ public class RedissonSessionManager extends ManagerBase { applicationClassLoader = getClass().getClassLoader(); } + Codec codec = redisson.getConfig().getCodec(); + Codec codecToUse; + try { + codecToUse = codec.getClass() + .getConstructor(ClassLoader.class, codec.getClass()) + .newInstance(applicationClassLoader, codec); + } catch (Exception e) { + throw new LifecycleException(e); + } + if (updateMode == UpdateMode.AFTER_REQUEST) { getEngine().getPipeline().addValve(new UpdateValve(this)); } @@ -237,14 +248,14 @@ public class RedissonSessionManager extends ManagerBase { if (msg instanceof AttributesPutAllMessage) { AttributesPutAllMessage m = (AttributesPutAllMessage) msg; - for (Entry entry : m.getAttrs(applicationClassLoader).entrySet()) { + for (Entry entry : m.getAttrs(codecToUse.getMapValueDecoder()).entrySet()) { session.superSetAttribute(entry.getKey(), entry.getValue(), true); } } if (msg instanceof AttributeUpdateMessage) { AttributeUpdateMessage m = (AttributeUpdateMessage)msg; - session.superSetAttribute(m.getName(), m.getValue(applicationClassLoader), true); + session.superSetAttribute(m.getName(), m.getValue(codecToUse.getMapValueDecoder()), true); } } } catch (Exception e) { diff --git a/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/AttributeMessage.java b/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/AttributeMessage.java index abd840557..d072fb6a5 100644 --- a/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/AttributeMessage.java +++ b/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/AttributeMessage.java @@ -15,13 +15,15 @@ */ 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; +import org.redisson.client.protocol.Decoder; +import org.redisson.client.protocol.Encoder; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufUtil; +import io.netty.buffer.Unpooled; /** * @@ -50,22 +52,29 @@ public class AttributeMessage implements Serializable { return nodeId; } - protected byte[] toByteArray(Object value) throws IOException { + protected byte[] toByteArray(Encoder encoder, 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(); + + ByteBuf buf = encoder.encode(value); + try { + return ByteBufUtil.getBytes(buf); + } finally { + buf.release(); + } } - protected Object toObject(ClassLoader classLoader, byte[] value) throws IOException, ClassNotFoundException { + protected Object toObject(Decoder decoder, byte[] value) throws IOException, ClassNotFoundException { if (value == null) { return null; } - CustomObjectInputStream in = new CustomObjectInputStream(new ByteArrayInputStream(value), classLoader); - return in.readObject(); + + ByteBuf buf = Unpooled.wrappedBuffer(value); + try { + return decoder.decode(buf, null); + } finally { + buf.release(); + } } } diff --git a/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/AttributeUpdateMessage.java b/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/AttributeUpdateMessage.java index 7c64b19f0..85961eef1 100644 --- a/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/AttributeUpdateMessage.java +++ b/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/AttributeUpdateMessage.java @@ -17,6 +17,9 @@ package org.redisson.tomcat; import java.io.IOException; +import org.redisson.client.protocol.Decoder; +import org.redisson.client.protocol.Encoder; + /** * * @author Nikita Koksharov @@ -30,18 +33,18 @@ public class AttributeUpdateMessage extends AttributeMessage { public AttributeUpdateMessage() { } - public AttributeUpdateMessage(String nodeId, String sessionId, String name, Object value) throws IOException { + public AttributeUpdateMessage(String nodeId, String sessionId, String name, Object value, Encoder encoder) throws IOException { super(nodeId, sessionId); this.name = name; - this.value = toByteArray(value); + this.value = toByteArray(encoder, value); } public String getName() { return name; } - public Object getValue(ClassLoader classLoader) throws IOException, ClassNotFoundException { - return toObject(classLoader, value); + public Object getValue(Decoder decoder) throws IOException, ClassNotFoundException { + return toObject(decoder, value); } } diff --git a/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/AttributesPutAllMessage.java b/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/AttributesPutAllMessage.java index 0e097c8ad..4fe5c8cf5 100644 --- a/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/AttributesPutAllMessage.java +++ b/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/AttributesPutAllMessage.java @@ -20,6 +20,9 @@ import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; +import org.redisson.client.protocol.Decoder; +import org.redisson.client.protocol.Encoder; + /** * * @author Nikita Koksharov @@ -32,25 +35,25 @@ public class AttributesPutAllMessage extends AttributeMessage { public AttributesPutAllMessage() { } - public AttributesPutAllMessage(String nodeId, String sessionId, Map attrs) throws IOException { + public AttributesPutAllMessage(String nodeId, String sessionId, Map attrs, Encoder encoder) throws IOException { super(nodeId, sessionId); if (attrs != null) { this.attrs = new HashMap(); for (Entry entry: attrs.entrySet()) { - this.attrs.put(entry.getKey(), toByteArray(entry.getValue())); + this.attrs.put(entry.getKey(), toByteArray(encoder, entry.getValue())); } } else { this.attrs = null; } } - public Map getAttrs(ClassLoader classLoader) throws IOException, ClassNotFoundException { + public Map getAttrs(Decoder decoder) throws IOException, ClassNotFoundException { if (attrs == null) { return null; } Map result = new HashMap(); for (Entry entry: attrs.entrySet()) { - result.put(entry.getKey(), toObject(classLoader, entry.getValue())); + result.put(entry.getKey(), toObject(decoder, entry.getValue())); } return result; } diff --git a/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/RedissonSession.java b/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/RedissonSession.java index b3a14dec0..d55c7f479 100644 --- a/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/RedissonSession.java +++ b/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/RedissonSession.java @@ -187,7 +187,7 @@ public class RedissonSession extends StandardSession { map.put(entry.getKey(), entry.getValue()); } try { - return new AttributesPutAllMessage(redissonManager.getNodeId(), getId(), map); + return new AttributesPutAllMessage(redissonManager.getNodeId(), getId(), map, this.map.getCodec().getMapValueEncoder()); } catch (IOException e) { throw new IllegalStateException(e); } @@ -207,7 +207,7 @@ public class RedissonSession extends StandardSession { map.fastPut(name, value); if (readMode == ReadMode.MEMORY) { try { - topic.publish(new AttributeUpdateMessage(redissonManager.getNodeId(), getId(), name, value)); + topic.publish(new AttributeUpdateMessage(redissonManager.getNodeId(), getId(), name, value, this.map.getCodec().getMapValueEncoder())); } catch (IOException e) { throw new IllegalStateException(e); } diff --git a/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/RedissonSessionManager.java b/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/RedissonSessionManager.java index 8fc03d2bb..813bc6321 100644 --- a/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/RedissonSessionManager.java +++ b/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/RedissonSessionManager.java @@ -35,6 +35,7 @@ import org.redisson.api.RMap; import org.redisson.api.RTopic; import org.redisson.api.RedissonClient; import org.redisson.api.listener.MessageListener; +import org.redisson.client.codec.Codec; import org.redisson.client.codec.StringCodec; import org.redisson.codec.CompositeCodec; import org.redisson.config.Config; @@ -213,6 +214,16 @@ public class RedissonSessionManager extends ManagerBase { applicationClassLoader = getClass().getClassLoader(); } + Codec codec = redisson.getConfig().getCodec(); + Codec codecToUse; + try { + codecToUse = codec.getClass() + .getConstructor(ClassLoader.class, codec.getClass()) + .newInstance(applicationClassLoader, codec); + } catch (Exception e) { + throw new LifecycleException(e); + } + if (updateMode == UpdateMode.AFTER_REQUEST) { getEngine().getPipeline().addValve(new UpdateValve(this)); } @@ -237,14 +248,14 @@ public class RedissonSessionManager extends ManagerBase { if (msg instanceof AttributesPutAllMessage) { AttributesPutAllMessage m = (AttributesPutAllMessage) msg; - for (Entry entry : m.getAttrs(applicationClassLoader).entrySet()) { + for (Entry entry : m.getAttrs(codecToUse.getMapValueDecoder()).entrySet()) { session.superSetAttribute(entry.getKey(), entry.getValue(), true); } } if (msg instanceof AttributeUpdateMessage) { AttributeUpdateMessage m = (AttributeUpdateMessage)msg; - session.superSetAttribute(m.getName(), m.getValue(applicationClassLoader), true); + session.superSetAttribute(m.getName(), m.getValue(codecToUse.getMapValueDecoder()), true); } } } catch (Exception e) {