Search in sources :

Example 1 with OutputStreamBackedEncoder

use of org.gradle.internal.serialize.OutputStreamBackedEncoder in project gradle by gradle.

the class InMemoryIndexedCache method put.

@Override
public void put(K key, V value) {
    ByteArrayOutputStream outstr = new ByteArrayOutputStream();
    OutputStreamBackedEncoder encoder = new OutputStreamBackedEncoder(outstr);
    try {
        valueSerializer.write(encoder, value);
        encoder.flush();
    } catch (Exception e) {
        throw UncheckedException.throwAsUncheckedException(e);
    }
    entries.put(key, outstr.toByteArray());
}
Also used : OutputStreamBackedEncoder(org.gradle.internal.serialize.OutputStreamBackedEncoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) UncheckedException(org.gradle.internal.UncheckedException)

Example 2 with OutputStreamBackedEncoder

use of org.gradle.internal.serialize.OutputStreamBackedEncoder in project gradle by gradle.

the class SimpleStateCache method serialize.

private void serialize(T newValue) {
    try {
        if (!cacheFile.isFile()) {
            cacheFile.createNewFile();
        }
        // read-write-execute for user only
        chmod.chmod(cacheFile.getParentFile(), 0700);
        // read-write for user only
        chmod.chmod(cacheFile, 0600);
        OutputStreamBackedEncoder encoder = new OutputStreamBackedEncoder(new BufferedOutputStream(new FileOutputStream(cacheFile)));
        try {
            serializer.write(encoder, newValue);
        } finally {
            encoder.close();
        }
    } catch (Exception e) {
        throw new GradleException(String.format("Could not write cache value to '%s'.", cacheFile), e);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) GradleException(org.gradle.api.GradleException) OutputStreamBackedEncoder(org.gradle.internal.serialize.OutputStreamBackedEncoder) BufferedOutputStream(java.io.BufferedOutputStream) GradleException(org.gradle.api.GradleException)

Example 3 with OutputStreamBackedEncoder

use of org.gradle.internal.serialize.OutputStreamBackedEncoder in project gradle by gradle.

the class ApplicationClassesInSystemClassLoaderWorkerImplementationFactory method prepareJavaCommand.

@Override
public void prepareJavaCommand(Object workerId, String displayName, DefaultWorkerProcessBuilder processBuilder, List<URL> implementationClassPath, Address serverAddress, JavaExecHandleBuilder execSpec, boolean publishProcessInfo) {
    Collection<File> applicationClasspath = processBuilder.getApplicationClasspath();
    LogLevel logLevel = processBuilder.getLogLevel();
    Set<String> sharedPackages = processBuilder.getSharedPackages();
    Object requestedSecurityManager = execSpec.getSystemProperties().get("java.security.manager");
    ClassPath workerMainClassPath = classPathRegistry.getClassPath("WORKER_MAIN");
    execSpec.setMain("worker." + GradleWorkerMain.class.getName());
    boolean useOptionsFile = shouldUseOptionsFile(execSpec);
    if (useOptionsFile) {
        // Use an options file to pass across application classpath
        File optionsFile = temporaryFileProvider.createTemporaryFile("gradle-worker-classpath", "txt");
        List<String> jvmArgs = writeOptionsFile(workerMainClassPath.getAsFiles(), applicationClasspath, optionsFile);
        execSpec.jvmArgs(jvmArgs);
    } else {
        // Use a dummy security manager, which hacks the application classpath into the system ClassLoader
        execSpec.classpath(workerMainClassPath.getAsFiles());
        execSpec.systemProperty("java.security.manager", "worker." + BootstrapSecurityManager.class.getName());
    }
    // Serialize configuration for the worker process to it stdin
    StreamByteBuffer buffer = new StreamByteBuffer();
    try {
        DataOutputStream outstr = new DataOutputStream(new EncodedStream.EncodedOutput(buffer.getOutputStream()));
        if (!useOptionsFile) {
            // Serialize the application classpath, this is consumed by BootstrapSecurityManager
            outstr.writeInt(applicationClasspath.size());
            for (File file : applicationClasspath) {
                outstr.writeUTF(file.getAbsolutePath());
            }
            // Serialize the actual security manager type, this is consumed by BootstrapSecurityManager
            outstr.writeUTF(requestedSecurityManager == null ? "" : requestedSecurityManager.toString());
        }
        // Serialize the shared packages, this is consumed by GradleWorkerMain
        outstr.writeInt(sharedPackages.size());
        for (String str : sharedPackages) {
            outstr.writeUTF(str);
        }
        // Serialize the worker implementation classpath, this is consumed by GradleWorkerMain
        outstr.writeInt(implementationClassPath.size());
        for (URL entry : implementationClassPath) {
            outstr.writeUTF(entry.toString());
        }
        // Serialize the worker config, this is consumed by SystemApplicationClassLoaderWorker
        OutputStreamBackedEncoder encoder = new OutputStreamBackedEncoder(outstr);
        encoder.writeSmallInt(logLevel.ordinal());
        encoder.writeBoolean(publishProcessInfo);
        new MultiChoiceAddressSerializer().write(encoder, (MultiChoiceAddress) serverAddress);
        // Serialize the worker, this is consumed by SystemApplicationClassLoaderWorker
        ActionExecutionWorker worker = new ActionExecutionWorker(processBuilder.getWorker(), workerId, displayName, processBuilder.getGradleUserHomeDir());
        byte[] serializedWorker = GUtil.serialize(worker);
        encoder.writeBinary(serializedWorker);
        encoder.flush();
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    execSpec.setStandardInput(buffer.getInputStream());
}
Also used : ClassPath(org.gradle.internal.classpath.ClassPath) EncodedStream(org.gradle.process.internal.streams.EncodedStream) DataOutputStream(java.io.DataOutputStream) StreamByteBuffer(org.gradle.internal.io.StreamByteBuffer) UncheckedIOException(org.gradle.api.UncheckedIOException) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) LogLevel(org.gradle.api.logging.LogLevel) URL(java.net.URL) OutputStreamBackedEncoder(org.gradle.internal.serialize.OutputStreamBackedEncoder) File(java.io.File) MultiChoiceAddressSerializer(org.gradle.internal.remote.internal.inet.MultiChoiceAddressSerializer)

Example 4 with OutputStreamBackedEncoder

use of org.gradle.internal.serialize.OutputStreamBackedEncoder in project gradle by gradle.

the class DaemonStartupCommunication method printDaemonStarted.

public void printDaemonStarted(PrintStream target, Long pid, String uid, Address address, File daemonLog) {
    target.print(daemonGreeting());
    // Encode as ascii
    try {
        OutputStream outputStream = new EncodedStream.EncodedOutput(target);
        FlushableEncoder encoder = new OutputStreamBackedEncoder(outputStream);
        encoder.writeNullableString(pid == null ? null : pid.toString());
        encoder.writeString(uid);
        MultiChoiceAddress multiChoiceAddress = (MultiChoiceAddress) address;
        new MultiChoiceAddressSerializer().write(encoder, multiChoiceAddress);
        encoder.writeString(daemonLog.getPath());
        encoder.flush();
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    target.println();
    //ibm vm 1.6 + windows XP gotchas:
    //we need to print something else to the stream after we print the daemon greeting.
    //without it, the parent hangs without receiving the message above (flushing does not help).
    LOGGER.debug("Completed writing the daemon greeting. Closing streams...");
//btw. the ibm vm+winXP also has some issues detecting closed streams by the child but we handle this problem differently.
}
Also used : FlushableEncoder(org.gradle.internal.serialize.FlushableEncoder) MultiChoiceAddress(org.gradle.internal.remote.internal.inet.MultiChoiceAddress) OutputStreamBackedEncoder(org.gradle.internal.serialize.OutputStreamBackedEncoder) UncheckedIOException(org.gradle.api.UncheckedIOException) UncheckedIOException(org.gradle.api.UncheckedIOException) MultiChoiceAddressSerializer(org.gradle.internal.remote.internal.inet.MultiChoiceAddressSerializer)

Aggregations

OutputStreamBackedEncoder (org.gradle.internal.serialize.OutputStreamBackedEncoder)4 UncheckedIOException (org.gradle.api.UncheckedIOException)2 MultiChoiceAddressSerializer (org.gradle.internal.remote.internal.inet.MultiChoiceAddressSerializer)2 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 URL (java.net.URL)1 GradleException (org.gradle.api.GradleException)1 LogLevel (org.gradle.api.logging.LogLevel)1 UncheckedException (org.gradle.internal.UncheckedException)1 ClassPath (org.gradle.internal.classpath.ClassPath)1 StreamByteBuffer (org.gradle.internal.io.StreamByteBuffer)1 MultiChoiceAddress (org.gradle.internal.remote.internal.inet.MultiChoiceAddress)1 FlushableEncoder (org.gradle.internal.serialize.FlushableEncoder)1 EncodedStream (org.gradle.process.internal.streams.EncodedStream)1