use of org.gradle.internal.io.StreamByteBuffer 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);
encoder.writeString(gradleUserHomeDir.getAbsolutePath());
new MultiChoiceAddressSerializer().write(encoder, (MultiChoiceAddress) serverAddress);
// Serialize the worker, this is consumed by SystemApplicationClassLoaderWorker
ActionExecutionWorker worker = new ActionExecutionWorker(processBuilder.getWorker(), workerId, displayName, gradleUserHomeDir);
byte[] serializedWorker = GUtil.serialize(worker);
encoder.writeBinary(serializedWorker);
encoder.flush();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
execSpec.setStandardInput(buffer.getInputStream());
}
use of org.gradle.internal.io.StreamByteBuffer in project gradle by gradle.
the class ToolingApiGradleExecutor method run.
public GradleExecutionResult run(GradleExecutionParameters parameters) {
final StreamByteBuffer outputBuffer = new StreamByteBuffer();
final OutputStream syncOutput = new SynchronizedOutputStream(outputBuffer.getOutputStream());
final List<BuildTask> tasks = new ArrayList<BuildTask>();
maybeRegisterCleanup();
GradleConnector gradleConnector = buildConnector(parameters.getGradleUserHome(), parameters.getProjectDir(), parameters.isEmbedded(), parameters.getGradleProvider());
ProjectConnection connection = null;
GradleVersion targetGradleVersion = null;
try {
connection = gradleConnector.connect();
targetGradleVersion = determineTargetGradleVersion(connection);
if (targetGradleVersion.compareTo(TestKitFeature.RUN_BUILDS.getSince()) < 0) {
throw new UnsupportedFeatureException(String.format("The version of Gradle you are using (%s) is not supported by TestKit. TestKit supports all Gradle versions 1.2 and later.", targetGradleVersion.getVersion()));
}
DefaultBuildLauncher launcher = (DefaultBuildLauncher) connection.newBuild();
launcher.setStandardOutput(new NoCloseOutputStream(teeOutput(syncOutput, parameters.getStandardOutput())));
launcher.setStandardError(new NoCloseOutputStream(teeOutput(syncOutput, parameters.getStandardError())));
if (parameters.getStandardInput() != null) {
launcher.setStandardInput(parameters.getStandardInput());
}
launcher.addProgressListener(new TaskExecutionProgressListener(tasks), OperationType.TASK);
launcher.withArguments(parameters.getBuildArgs().toArray(new String[0]));
launcher.setJvmArguments(parameters.getJvmArgs().toArray(new String[0]));
if (!parameters.getInjectedClassPath().isEmpty()) {
if (targetGradleVersion.compareTo(TestKitFeature.PLUGIN_CLASSPATH_INJECTION.getSince()) < 0) {
throw new UnsupportedFeatureException("support plugin classpath injection", targetGradleVersion, TestKitFeature.PLUGIN_CLASSPATH_INJECTION.getSince());
}
launcher.withInjectedClassPath(parameters.getInjectedClassPath());
}
launcher.run();
} catch (UnsupportedVersionException e) {
throw new InvalidRunnerConfigurationException("The build could not be executed due to a feature not being supported by the target Gradle version", e);
} catch (BuildException t) {
return new GradleExecutionResult(new BuildOperationParameters(targetGradleVersion, parameters.isEmbedded()), outputBuffer.readAsString(), tasks, t);
} catch (GradleConnectionException t) {
StringBuilder message = new StringBuilder("An error occurred executing build with ");
if (parameters.getBuildArgs().isEmpty()) {
message.append("no args");
} else {
message.append("args '");
message.append(CollectionUtils.join(" ", parameters.getBuildArgs()));
message.append("'");
}
message.append(" in directory '").append(parameters.getProjectDir().getAbsolutePath()).append("'");
String capturedOutput = outputBuffer.readAsString();
if (!capturedOutput.isEmpty()) {
message.append(". Output before error:").append(SystemProperties.getInstance().getLineSeparator()).append(capturedOutput);
}
throw new IllegalStateException(message.toString(), t);
} finally {
if (connection != null) {
connection.close();
}
}
return new GradleExecutionResult(new BuildOperationParameters(targetGradleVersion, parameters.isEmbedded()), outputBuffer.readAsString(), tasks);
}
use of org.gradle.internal.io.StreamByteBuffer in project gradle by gradle.
the class MapBasedBuildCacheService method store.
@Override
public void store(BuildCacheKey key, BuildCacheEntryWriter output) throws BuildCacheException {
StreamByteBuffer buffer = new StreamByteBuffer();
try {
output.writeTo(buffer.getOutputStream());
} catch (IOException e) {
throw new BuildCacheException("storing " + key, e);
}
delegate.put(key.getHashCode(), buffer.readAsByteArray());
}
use of org.gradle.internal.io.StreamByteBuffer in project gradle by gradle.
the class DefaultDaemonStarter method startDaemon.
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 = ClassPath.EMPTY;
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.writeBoolean(singleUse);
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);
}
use of org.gradle.internal.io.StreamByteBuffer in project gradle by gradle.
the class CommandLineToolVersionLocator method getVswhereOutput.
private String getVswhereOutput(File vswhereBinary, List<String> args) {
ExecAction exec = execActionFactory.newExecAction();
exec.args(args);
exec.executable(vswhereBinary.getAbsolutePath());
exec.setWorkingDir(vswhereBinary.getParentFile());
StreamByteBuffer buffer = new StreamByteBuffer();
exec.setStandardOutput(buffer.getOutputStream());
exec.setErrorOutput(NullOutputStream.INSTANCE);
exec.setIgnoreExitValue(true);
ExecResult result = exec.execute();
int exitValue = result.getExitValue();
if (exitValue == 0) {
return buffer.readAsString("UTF-8");
} else {
LOGGER.debug("vswhere.exe returned a non-zero exit value ({}) - ignoring", result.getExitValue());
return null;
}
}
Aggregations