From 3b91607f96017dc6a26ee6cfe7f55b78a19fc019 Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Thu, 11 Jan 2024 13:01:16 +0100 Subject: commons-exec is no longer needed. --- vespajlib/pom.xml | 4 - .../yahoo/system/execution/ProcessExecutor.java | 99 ---------------------- .../com/yahoo/system/execution/ProcessResult.java | 19 ----- .../com/yahoo/system/execution/package-info.java | 5 -- .../system/execution/ProcessExecutorTest.java | 26 ------ 5 files changed, 153 deletions(-) delete mode 100644 vespajlib/src/main/java/com/yahoo/system/execution/ProcessExecutor.java delete mode 100644 vespajlib/src/main/java/com/yahoo/system/execution/ProcessResult.java delete mode 100644 vespajlib/src/main/java/com/yahoo/system/execution/package-info.java delete mode 100644 vespajlib/src/test/java/com/yahoo/system/execution/ProcessExecutorTest.java (limited to 'vespajlib') diff --git a/vespajlib/pom.xml b/vespajlib/pom.xml index 16ae251aa8b..9d785faeae4 100644 --- a/vespajlib/pom.xml +++ b/vespajlib/pom.xml @@ -23,10 +23,6 @@ org.lz4 lz4-java - - org.apache.commons - commons-exec - net.java.dev.jna jna diff --git a/vespajlib/src/main/java/com/yahoo/system/execution/ProcessExecutor.java b/vespajlib/src/main/java/com/yahoo/system/execution/ProcessExecutor.java deleted file mode 100644 index fe3bca07845..00000000000 --- a/vespajlib/src/main/java/com/yahoo/system/execution/ProcessExecutor.java +++ /dev/null @@ -1,99 +0,0 @@ -// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.system.execution; - -import org.apache.commons.exec.CommandLine; -import org.apache.commons.exec.DefaultExecutor; -import org.apache.commons.exec.ExecuteException; -import org.apache.commons.exec.ExecuteWatchdog; -import org.apache.commons.exec.PumpStreamHandler; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; -import java.time.Duration; -import java.util.Optional; -import java.util.concurrent.TimeUnit; - -/** - * Configurable system command executor that captures stdout and stderr. - * - * @author gjoranv - * @author bjorncs - */ -public class ProcessExecutor { - - public static class Builder { - private final Duration timeout; - private int[] successExitCodes; - - public Builder(Duration timeout) { - this.timeout = timeout; - } - - public Builder successExitCodes(int... successExitCodes) { - this.successExitCodes = successExitCodes; - return this; - } - - public ProcessExecutor build() { - return new ProcessExecutor(timeout, successExitCodes); - } - } - - private ProcessExecutor(Duration timeout, int[] successExitCodes) { - this.timeout = timeout; - this.successExitCodes = successExitCodes; - } - - private final Duration timeout; - private final int[] successExitCodes; - - /** - * Convenience method to execute a process with no input data. See {@link #execute(String, String)} for details. - */ - public Optional execute(String command) throws IOException { - return execute(command, null); - } - - /** - * Executes the given command synchronously. - * - * @param command The command to execute. - * @param processInput Input provided to the process. - * @return The result of the execution, or empty if the process does not terminate within the timeout set for this executor. - * @throws IOException if the process execution failed. - */ - public Optional execute(String command, String processInput) throws IOException { - ByteArrayOutputStream processErr = new ByteArrayOutputStream(); - ByteArrayOutputStream processOut = new ByteArrayOutputStream(); - - DefaultExecutor executor = DefaultExecutor.builder().get(); - executor.setStreamHandler(createStreamHandler(processOut, processErr, processInput)); - ExecuteWatchdog watchDog = ExecuteWatchdog.builder().setTimeout(timeout).get(); - executor.setWatchdog(watchDog); - executor.setExitValues(successExitCodes); - - int exitCode; - try { - exitCode = executor.execute(CommandLine.parse(command)); - } catch (ExecuteException e) { - exitCode = e.getExitValue(); - } - return (watchDog.killedProcess()) ? - Optional.empty() : Optional.of(new ProcessResult(exitCode, processOut.toString(), processErr.toString())); - } - - private static PumpStreamHandler createStreamHandler(ByteArrayOutputStream processOut, - ByteArrayOutputStream processErr, - String input) { - if (input != null) { - InputStream processInput = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8)); - return new PumpStreamHandler(processOut, processErr, processInput); - } else { - return new PumpStreamHandler(processOut, processErr); - } - } - -} diff --git a/vespajlib/src/main/java/com/yahoo/system/execution/ProcessResult.java b/vespajlib/src/main/java/com/yahoo/system/execution/ProcessResult.java deleted file mode 100644 index 03ded02848f..00000000000 --- a/vespajlib/src/main/java/com/yahoo/system/execution/ProcessResult.java +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.system.execution; - -/** - * @author bjorncs - * @author gjoranv - */ -public class ProcessResult { - public final String stdOut; - public final String stdErr; - public final int exitCode; - - public ProcessResult(int exitCode, String stdOut, String stdErr) { - this.exitCode = exitCode; - this.stdOut = stdOut; - this.stdErr = stdErr; - } - -} diff --git a/vespajlib/src/main/java/com/yahoo/system/execution/package-info.java b/vespajlib/src/main/java/com/yahoo/system/execution/package-info.java deleted file mode 100644 index a2fce8038fe..00000000000 --- a/vespajlib/src/main/java/com/yahoo/system/execution/package-info.java +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -@ExportPackage -package com.yahoo.system.execution; - -import com.yahoo.osgi.annotation.ExportPackage; diff --git a/vespajlib/src/test/java/com/yahoo/system/execution/ProcessExecutorTest.java b/vespajlib/src/test/java/com/yahoo/system/execution/ProcessExecutorTest.java deleted file mode 100644 index 607a223b00b..00000000000 --- a/vespajlib/src/test/java/com/yahoo/system/execution/ProcessExecutorTest.java +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.system.execution; - -import org.junit.Test; - -import java.time.Duration; -import java.util.Optional; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -/** - * @author gjoranv - */ -public class ProcessExecutorTest { - - @Test - public void echo_can_be_executed() throws Exception { - final String message = "Hello from executor!"; - ProcessExecutor executor = new ProcessExecutor.Builder(Duration.ofSeconds(10)).build(); - Optional result = executor.execute("echo " + message); - assertTrue(result.isPresent()); - assertEquals(message, result.get().stdOut.trim()); - assertEquals("", result.get().stdErr); - } -} -- cgit v1.2.3