Search in sources :

Example 1 with ClassLoaderObjectInputStream

use of org.gradle.internal.io.ClassLoaderObjectInputStream in project gradle by gradle.

the class SerializationFixture method serializationRoundtrip.

public static <T> T serializationRoundtrip(T obj) {
    try {
        StreamByteBuffer buffer = new StreamByteBuffer();
        ObjectOutputStream out = new ObjectOutputStream(buffer.getOutputStream());
        out.writeObject(obj);
        out.close();
        ClassLoaderObjectInputStream input = new ClassLoaderObjectInputStream(buffer.getInputStream(), obj.getClass().getClassLoader());
        return (T) input.readObject();
    } catch (Exception e) {
        throw UncheckedException.throwAsUncheckedException(e);
    }
}
Also used : StreamByteBuffer(org.gradle.internal.io.StreamByteBuffer) ClassLoaderObjectInputStream(org.gradle.internal.io.ClassLoaderObjectInputStream) ObjectOutputStream(java.io.ObjectOutputStream) UncheckedException(org.gradle.internal.UncheckedException)

Example 2 with ClassLoaderObjectInputStream

use of org.gradle.internal.io.ClassLoaderObjectInputStream in project gradle by gradle.

the class InProcessWorkerFactory method transferWorkerIntoWorkerClassloader.

private <T extends WorkSpec> Callable<?> transferWorkerIntoWorkerClassloader(WorkerAction<T> action, T spec, ClassLoader workerClassLoader) throws IOException, ClassNotFoundException {
    byte[] serializedWorker = GUtil.serialize(new WorkerCallable<T>(action, spec));
    ObjectInputStream ois = new ClassLoaderObjectInputStream(new ByteArrayInputStream(serializedWorker), workerClassLoader);
    return (Callable<?>) ois.readObject();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ClassLoaderObjectInputStream(org.gradle.internal.io.ClassLoaderObjectInputStream) Callable(java.util.concurrent.Callable) ObjectInputStream(java.io.ObjectInputStream) ClassLoaderObjectInputStream(org.gradle.internal.io.ClassLoaderObjectInputStream) ExceptionReplacingObjectInputStream(org.gradle.internal.serialize.ExceptionReplacingObjectInputStream)

Example 3 with ClassLoaderObjectInputStream

use of org.gradle.internal.io.ClassLoaderObjectInputStream in project gradle by gradle.

the class SerializedValueSnapshot method snapshot.

@Override
public ValueSnapshot snapshot(Object value, ValueSnapshotter snapshotter) {
    ValueSnapshot snapshot = snapshotter.snapshot(value);
    if (snapshot instanceof SerializedValueSnapshot) {
        SerializedValueSnapshot newSnapshot = (SerializedValueSnapshot) snapshot;
        if (!Objects.equal(implementationHash, newSnapshot.implementationHash)) {
            // Different implementation - assume value has changed
            return newSnapshot;
        }
        if (Arrays.equals(serializedValue, newSnapshot.serializedValue)) {
            // Same serialized content - value has not changed
            return this;
        }
        // Deserialize the old value and use the equals() implementation. This will be removed at some point
        Object oldValue;
        try {
            oldValue = new ClassLoaderObjectInputStream(new ByteArrayInputStream(serializedValue), value.getClass().getClassLoader()).readObject();
        } catch (Exception e) {
            throw new UncheckedIOException(e);
        }
        if (oldValue.equals(value)) {
            // Same value
            return this;
        }
    }
    return snapshot;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) UncheckedIOException(org.gradle.api.UncheckedIOException) ClassLoaderObjectInputStream(org.gradle.internal.io.ClassLoaderObjectInputStream) UncheckedIOException(org.gradle.api.UncheckedIOException)

Example 4 with ClassLoaderObjectInputStream

use of org.gradle.internal.io.ClassLoaderObjectInputStream in project gradle by gradle.

the class SystemApplicationClassLoaderWorker method deserializeWorker.

private Action<WorkerProcessContext> deserializeWorker(byte[] serializedWorker) {
    Action<WorkerProcessContext> action;
    try {
        ObjectInputStream instr = new ClassLoaderObjectInputStream(new ByteArrayInputStream(serializedWorker), getClass().getClassLoader());
        @SuppressWarnings("unchecked") Action<WorkerProcessContext> deserializedAction = (Action<WorkerProcessContext>) instr.readObject();
        action = deserializedAction;
    } catch (Exception e) {
        throw UncheckedException.throwAsUncheckedException(e);
    }
    return action;
}
Also used : Action(org.gradle.api.Action) ByteArrayInputStream(java.io.ByteArrayInputStream) WorkerProcessContext(org.gradle.process.internal.worker.WorkerProcessContext) ClassLoaderObjectInputStream(org.gradle.internal.io.ClassLoaderObjectInputStream) UncheckedException(org.gradle.internal.UncheckedException) FileNotFoundException(java.io.FileNotFoundException) ObjectInputStream(java.io.ObjectInputStream) ClassLoaderObjectInputStream(org.gradle.internal.io.ClassLoaderObjectInputStream)

Example 5 with ClassLoaderObjectInputStream

use of org.gradle.internal.io.ClassLoaderObjectInputStream in project gradle by gradle.

the class IsolatedClassloaderWorkerFactory method transferWorkerIntoWorkerClassloader.

private Callable<?> transferWorkerIntoWorkerClassloader(ActionExecutionSpec spec, ClassLoader workerClassLoader) throws IOException, ClassNotFoundException {
    byte[] serializedWorker = GUtil.serialize(new WorkerCallable(spec));
    ObjectInputStream ois = new ClassLoaderObjectInputStream(new ByteArrayInputStream(serializedWorker), workerClassLoader);
    return (Callable<?>) ois.readObject();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ClassLoaderObjectInputStream(org.gradle.internal.io.ClassLoaderObjectInputStream) Callable(java.util.concurrent.Callable) ObjectInputStream(java.io.ObjectInputStream) ClassLoaderObjectInputStream(org.gradle.internal.io.ClassLoaderObjectInputStream) ExceptionReplacingObjectInputStream(org.gradle.internal.serialize.ExceptionReplacingObjectInputStream)

Aggregations

ClassLoaderObjectInputStream (org.gradle.internal.io.ClassLoaderObjectInputStream)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ObjectInputStream (java.io.ObjectInputStream)3 Callable (java.util.concurrent.Callable)2 UncheckedException (org.gradle.internal.UncheckedException)2 ExceptionReplacingObjectInputStream (org.gradle.internal.serialize.ExceptionReplacingObjectInputStream)2 FileNotFoundException (java.io.FileNotFoundException)1 ObjectOutputStream (java.io.ObjectOutputStream)1 Action (org.gradle.api.Action)1 UncheckedIOException (org.gradle.api.UncheckedIOException)1 StreamByteBuffer (org.gradle.internal.io.StreamByteBuffer)1 WorkerProcessContext (org.gradle.process.internal.worker.WorkerProcessContext)1