From f62f028e0822ca7113f7c80342915d1b2e11abfc Mon Sep 17 00:00:00 2001 From: Zhelyazko Chobantonov Date: Wed, 29 May 2019 14:41:15 -0400 Subject: [PATCH] resolves issue: ClassNotFoundException thrown during SeriazliationCodec.decode #2136 --- .../redisson/codec/SerializationCodec.java | 22 +++++++++++++------ .../redisson/executor/TasksRunnerService.java | 15 ++++++++++--- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/redisson/src/main/java/org/redisson/codec/SerializationCodec.java b/redisson/src/main/java/org/redisson/codec/SerializationCodec.java index 66e0c677a..335dc327d 100644 --- a/redisson/src/main/java/org/redisson/codec/SerializationCodec.java +++ b/redisson/src/main/java/org/redisson/codec/SerializationCodec.java @@ -40,14 +40,22 @@ public class SerializationCodec extends BaseCodec { @Override public Object decode(ByteBuf buf, State state) throws IOException { try { - ByteBufInputStream in = new ByteBufInputStream(buf); - ObjectInputStream inputStream; - if (classLoader != null) { - inputStream = new CustomObjectInputStream(classLoader, in); - } else { - inputStream = new ObjectInputStream(in); + //set thread context class loader to be the classLoader variable as there could be reflection + //done while reading from input stream which reflection will use thread class loader to load classes on demand + ClassLoader currentThreadClassLoader = Thread.currentThread().getContextClassLoader(); + try { + ByteBufInputStream in = new ByteBufInputStream(buf); + ObjectInputStream inputStream; + if (classLoader != null) { + Thread.currentThread().setContextClassLoader(classLoader); + inputStream = new CustomObjectInputStream(classLoader, in); + } else { + inputStream = new ObjectInputStream(in); + } + return inputStream.readObject(); + } finally { + Thread.currentThread().setContextClassLoader(currentThreadClassLoader); } - return inputStream.readObject(); } catch (IOException e) { throw e; } catch (Exception e) { diff --git a/redisson/src/main/java/org/redisson/executor/TasksRunnerService.java b/redisson/src/main/java/org/redisson/executor/TasksRunnerService.java index d0cd44d49..a47877715 100644 --- a/redisson/src/main/java/org/redisson/executor/TasksRunnerService.java +++ b/redisson/src/main/java/org/redisson/executor/TasksRunnerService.java @@ -275,9 +275,18 @@ public class TasksRunnerService implements RemoteExecutorService { T task; if (params.getLambdaBody() != null) { ByteArrayInputStream is = new ByteArrayInputStream(params.getLambdaBody()); - ObjectInput oo = new CustomObjectInputStream(classLoaderCodec.getClassLoader(), is); - task = (T) oo.readObject(); - oo.close(); + + //set thread context class loader to be the classLoaderCodec.getClassLoader() variable as there could be reflection + //done while reading from input stream which reflection will use thread class loader to load classes on demand + ClassLoader currentThreadClassLoader = Thread.currentThread().getContextClassLoader(); + try { + Thread.currentThread().setContextClassLoader(classLoaderCodec.getClassLoader()); + ObjectInput oo = new CustomObjectInputStream(classLoaderCodec.getClassLoader(), is); + task = (T) oo.readObject(); + oo.close(); + } finally { + Thread.currentThread().setContextClassLoader(currentThreadClassLoader); + } } else { task = (T) classLoaderCodec.getValueDecoder().decode(stateBuf, null); }