Search in sources :

Example 1 with FlushableEncoder

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

the class DefaultDaemonStarter method startDaemon.

public DaemonStartupInfo startDaemon() {
    String daemonUid = UUID.randomUUID().toString();
    GradleInstallation gradleInstallation = CurrentGradleInstallation.get();
    ModuleRegistry registry = new DefaultModuleRegistry(gradleInstallation);
    ClassPath classpath;
    List<File> searchClassPath;
    if (gradleInstallation == null) {
        // When not running from a Gradle distro, need runtime impl for launcher plus the search path to look for other modules
        classpath = new DefaultClassPath();
        for (Module module : registry.getModule("gradle-launcher").getAllRequiredModules()) {
            classpath = classpath.plus(module.getClasspath());
        }
        searchClassPath = registry.getAdditionalClassPath().getAsFiles();
    } else {
        // When running from a Gradle distro, only need launcher jar. The daemon can find everything from there.
        classpath = registry.getModule("gradle-launcher").getImplementationClasspath();
        searchClassPath = Collections.emptyList();
    }
    if (classpath.isEmpty()) {
        throw new IllegalStateException("Unable to construct a bootstrap classpath when starting the daemon");
    }
    versionValidator.validate(daemonParameters);
    List<String> daemonArgs = new ArrayList<String>();
    daemonArgs.add(daemonParameters.getEffectiveJvm().getJavaExecutable().getAbsolutePath());
    List<String> daemonOpts = daemonParameters.getEffectiveJvmArgs();
    daemonArgs.addAll(daemonOpts);
    daemonArgs.add("-cp");
    daemonArgs.add(CollectionUtils.join(File.pathSeparator, classpath.getAsFiles()));
    if (Boolean.getBoolean("org.gradle.daemon.debug")) {
        daemonArgs.add("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005");
    }
    LOGGER.debug("Using daemon args: {}", daemonArgs);
    daemonArgs.add(GradleDaemon.class.getName());
    // Version isn't used, except by a human looking at the output of jps.
    daemonArgs.add(GradleVersion.current().getVersion());
    // Serialize configuration to daemon via the process' stdin
    StreamByteBuffer buffer = new StreamByteBuffer();
    FlushableEncoder encoder = new KryoBackedEncoder(new EncodedStream.EncodedOutput(buffer.getOutputStream()));
    try {
        encoder.writeString(daemonParameters.getGradleUserHomeDir().getAbsolutePath());
        encoder.writeString(daemonDir.getBaseDir().getAbsolutePath());
        encoder.writeSmallInt(daemonParameters.getIdleTimeout());
        encoder.writeSmallInt(daemonParameters.getPeriodicCheckInterval());
        encoder.writeString(daemonUid);
        encoder.writeSmallInt(daemonOpts.size());
        for (String daemonOpt : daemonOpts) {
            encoder.writeString(daemonOpt);
        }
        encoder.writeSmallInt(searchClassPath.size());
        for (File file : searchClassPath) {
            encoder.writeString(file.getAbsolutePath());
        }
        encoder.flush();
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    InputStream stdInput = buffer.getInputStream();
    return startProcess(daemonArgs, daemonDir.getVersionedDir(), stdInput);
}
Also used : DefaultClassPath(org.gradle.internal.classpath.DefaultClassPath) ClassPath(org.gradle.internal.classpath.ClassPath) FlushableEncoder(org.gradle.internal.serialize.FlushableEncoder) EncodedStream(org.gradle.process.internal.streams.EncodedStream) InputStream(java.io.InputStream) ModuleRegistry(org.gradle.api.internal.classpath.ModuleRegistry) DefaultModuleRegistry(org.gradle.api.internal.classpath.DefaultModuleRegistry) ArrayList(java.util.ArrayList) StreamByteBuffer(org.gradle.internal.io.StreamByteBuffer) UncheckedIOException(org.gradle.api.UncheckedIOException) KryoBackedEncoder(org.gradle.internal.serialize.kryo.KryoBackedEncoder) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) CurrentGradleInstallation(org.gradle.internal.installation.CurrentGradleInstallation) GradleInstallation(org.gradle.internal.installation.GradleInstallation) DefaultModuleRegistry(org.gradle.api.internal.classpath.DefaultModuleRegistry) GradleDaemon(org.gradle.launcher.daemon.bootstrap.GradleDaemon) Module(org.gradle.api.internal.classpath.Module) File(java.io.File) DefaultClassPath(org.gradle.internal.classpath.DefaultClassPath)

Example 2 with FlushableEncoder

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

the class TestResultSerializer method write.

public void write(Collection<TestClassResult> results) {
    try {
        OutputStream outputStream = new FileOutputStream(resultsFile);
        try {
            if (!results.isEmpty()) {
                // only write if we have results, otherwise truncate
                FlushableEncoder encoder = new KryoBackedEncoder(outputStream);
                encoder.writeSmallInt(RESULT_VERSION);
                write(results, encoder);
                encoder.flush();
            }
        } finally {
            outputStream.close();
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : FlushableEncoder(org.gradle.internal.serialize.FlushableEncoder) UncheckedIOException(org.gradle.api.UncheckedIOException) KryoBackedEncoder(org.gradle.internal.serialize.kryo.KryoBackedEncoder) UncheckedIOException(org.gradle.api.UncheckedIOException)

Example 3 with FlushableEncoder

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

the class DaemonStartupCommunication method printDaemonStarted.

public void printDaemonStarted(PrintStream target, Long pid, String uid, Address address, File daemonLog) {
    // Encode as ascii
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    try {
        byteArrayOutputStream.write(daemonGreeting().getBytes());
        OutputStream outputStream = new EncodedStream.EncodedOutput(byteArrayOutputStream);
        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());
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    target.println(byteArrayOutputStream.toString());
    // 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) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MultiChoiceAddress(org.gradle.internal.remote.internal.inet.MultiChoiceAddress) OutputStreamBackedEncoder(org.gradle.internal.serialize.OutputStreamBackedEncoder) UncheckedIOException(org.gradle.api.UncheckedIOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) UncheckedIOException(org.gradle.api.UncheckedIOException) MultiChoiceAddressSerializer(org.gradle.internal.remote.internal.inet.MultiChoiceAddressSerializer)

Example 4 with FlushableEncoder

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

the class DefaultDaemonStarter method startDaemon.

@Override
public DaemonStartupInfo startDaemon(boolean singleUse) {
    String daemonUid = UUID.randomUUID().toString();
    GradleInstallation gradleInstallation = CurrentGradleInstallation.get();
    ModuleRegistry registry = new DefaultModuleRegistry(gradleInstallation);
    ClassPath classpath;
    List<File> searchClassPath;
    if (gradleInstallation == null) {
        // When not running from a Gradle distro, need runtime impl for launcher plus the search path to look for other modules
        classpath = registry.getModule("gradle-launcher").getAllRequiredModulesClasspath();
        searchClassPath = registry.getAdditionalClassPath().getAsFiles();
    } else {
        // When running from a Gradle distro, only need launcher jar. The daemon can find everything from there.
        classpath = registry.getModule("gradle-launcher").getImplementationClasspath();
        searchClassPath = Collections.emptyList();
    }
    if (classpath.isEmpty()) {
        throw new IllegalStateException("Unable to construct a bootstrap classpath when starting the daemon");
    }
    versionValidator.validate(daemonParameters);
    List<String> daemonArgs = new ArrayList<String>();
    daemonArgs.addAll(getPriorityArgs(daemonParameters.getPriority()));
    daemonArgs.add(daemonParameters.getEffectiveJvm().getJavaExecutable().getAbsolutePath());
    List<String> daemonOpts = daemonParameters.getEffectiveJvmArgs();
    daemonArgs.addAll(daemonOpts);
    daemonArgs.add("-cp");
    daemonArgs.add(CollectionUtils.join(File.pathSeparator, classpath.getAsFiles()));
    if (Boolean.getBoolean("org.gradle.daemon.debug")) {
        daemonArgs.add("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005");
    }
    LOGGER.debug("Using daemon args: {}", daemonArgs);
    daemonArgs.add(GradleDaemon.class.getName());
    // Version isn't used, except by a human looking at the output of jps.
    daemonArgs.add(GradleVersion.current().getVersion());
    // Serialize configuration to daemon via the process' stdin
    StreamByteBuffer buffer = new StreamByteBuffer();
    FlushableEncoder encoder = new KryoBackedEncoder(new EncodedStream.EncodedOutput(buffer.getOutputStream()));
    try {
        encoder.writeString(daemonParameters.getGradleUserHomeDir().getAbsolutePath());
        encoder.writeString(daemonDir.getBaseDir().getAbsolutePath());
        encoder.writeSmallInt(daemonParameters.getIdleTimeout());
        encoder.writeSmallInt(daemonParameters.getPeriodicCheckInterval());
        encoder.writeBoolean(singleUse);
        encoder.writeString(daemonUid);
        encoder.writeSmallInt(daemonParameters.getPriority().ordinal());
        encoder.writeSmallInt(daemonOpts.size());
        for (String daemonOpt : daemonOpts) {
            encoder.writeString(daemonOpt);
        }
        encoder.writeSmallInt(searchClassPath.size());
        for (File file : searchClassPath) {
            encoder.writeString(file.getAbsolutePath());
        }
        encoder.flush();
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    InputStream stdInput = buffer.getInputStream();
    return startProcess(daemonArgs, daemonDir.getVersionedDir(), daemonParameters.getGradleUserHomeDir().getAbsoluteFile(), stdInput);
}
Also used : ClassPath(org.gradle.internal.classpath.ClassPath) FlushableEncoder(org.gradle.internal.serialize.FlushableEncoder) EncodedStream(org.gradle.internal.stream.EncodedStream) InputStream(java.io.InputStream) ModuleRegistry(org.gradle.api.internal.classpath.ModuleRegistry) DefaultModuleRegistry(org.gradle.api.internal.classpath.DefaultModuleRegistry) ArrayList(java.util.ArrayList) StreamByteBuffer(org.gradle.internal.io.StreamByteBuffer) UncheckedIOException(org.gradle.api.UncheckedIOException) KryoBackedEncoder(org.gradle.internal.serialize.kryo.KryoBackedEncoder) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) CurrentGradleInstallation(org.gradle.internal.installation.CurrentGradleInstallation) GradleInstallation(org.gradle.internal.installation.GradleInstallation) DefaultModuleRegistry(org.gradle.api.internal.classpath.DefaultModuleRegistry) GradleDaemon(org.gradle.launcher.daemon.bootstrap.GradleDaemon) File(java.io.File)

Aggregations

UncheckedIOException (org.gradle.api.UncheckedIOException)4 FlushableEncoder (org.gradle.internal.serialize.FlushableEncoder)4 IOException (java.io.IOException)3 KryoBackedEncoder (org.gradle.internal.serialize.kryo.KryoBackedEncoder)3 File (java.io.File)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 DefaultModuleRegistry (org.gradle.api.internal.classpath.DefaultModuleRegistry)2 ModuleRegistry (org.gradle.api.internal.classpath.ModuleRegistry)2 ClassPath (org.gradle.internal.classpath.ClassPath)2 CurrentGradleInstallation (org.gradle.internal.installation.CurrentGradleInstallation)2 GradleInstallation (org.gradle.internal.installation.GradleInstallation)2 StreamByteBuffer (org.gradle.internal.io.StreamByteBuffer)2 GradleDaemon (org.gradle.launcher.daemon.bootstrap.GradleDaemon)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1 Module (org.gradle.api.internal.classpath.Module)1 DefaultClassPath (org.gradle.internal.classpath.DefaultClassPath)1 MultiChoiceAddress (org.gradle.internal.remote.internal.inet.MultiChoiceAddress)1 MultiChoiceAddressSerializer (org.gradle.internal.remote.internal.inet.MultiChoiceAddressSerializer)1