Search in sources :

Example 1 with CustomObjectInputStream

use of org.redisson.codec.CustomObjectInputStream in project redisson by redisson.

the class TasksRunnerService method decode.

@SuppressWarnings("unchecked")
private <T> T decode(TaskParameters params) {
    ByteBuf classBodyBuf = Unpooled.wrappedBuffer(params.getClassBody());
    ByteBuf stateBuf = Unpooled.wrappedBuffer(params.getState());
    try {
        HashValue hash = new HashValue(Hash.hash128(classBodyBuf));
        Codec classLoaderCodec = CODECS.get(hash);
        if (classLoaderCodec == null) {
            RedissonClassLoader cl = new RedissonClassLoader(codec.getClassLoader());
            cl.loadClass(params.getClassName(), params.getClassBody());
            classLoaderCodec = this.codec.getClass().getConstructor(ClassLoader.class).newInstance(cl);
            CODECS.put(hash, classLoaderCodec);
        }
        T task;
        if (params.getLambdaBody() != null) {
            ByteArrayInputStream is = new ByteArrayInputStream(params.getLambdaBody());
            // 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);
        }
        Injector.inject(task, RedissonClient.class, redisson);
        Injector.inject(task, String.class, params.getRequestId());
        if (tasksInjector != null) {
            tasksInjector.inject(task);
        }
        return task;
    } catch (Exception e) {
        throw new IllegalStateException("Unable to initialize codec with ClassLoader parameter", e);
    } finally {
        classBodyBuf.release();
        stateBuf.release();
    }
}
Also used : Codec(org.redisson.client.codec.Codec) StringCodec(org.redisson.client.codec.StringCodec) LongCodec(org.redisson.client.codec.LongCodec) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(java.io.ObjectInput) ByteBuf(io.netty.buffer.ByteBuf) HashValue(org.redisson.misc.HashValue) CustomObjectInputStream(org.redisson.codec.CustomObjectInputStream) RedisException(org.redisson.client.RedisException) RedissonShutdownException(org.redisson.RedissonShutdownException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ObjectInput (java.io.ObjectInput)1 ExecutionException (java.util.concurrent.ExecutionException)1 RedissonShutdownException (org.redisson.RedissonShutdownException)1 RedisException (org.redisson.client.RedisException)1 Codec (org.redisson.client.codec.Codec)1 LongCodec (org.redisson.client.codec.LongCodec)1 StringCodec (org.redisson.client.codec.StringCodec)1 CustomObjectInputStream (org.redisson.codec.CustomObjectInputStream)1 HashValue (org.redisson.misc.HashValue)1