aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2024-01-11 13:01:16 +0100
committerHenning Baldersheim <balder@yahoo-inc.com>2024-01-11 13:01:16 +0100
commit3b91607f96017dc6a26ee6cfe7f55b78a19fc019 (patch)
tree2ca0560bb211465bf18694941b3424c60a639583 /vespajlib/src
parentf9db1e6e3eb5826733bfde3dea5e4677f1a18e56 (diff)
commons-exec is no longer needed.
Diffstat (limited to 'vespajlib/src')
-rw-r--r--vespajlib/src/main/java/com/yahoo/system/execution/ProcessExecutor.java99
-rw-r--r--vespajlib/src/main/java/com/yahoo/system/execution/ProcessResult.java19
-rw-r--r--vespajlib/src/main/java/com/yahoo/system/execution/package-info.java5
-rw-r--r--vespajlib/src/test/java/com/yahoo/system/execution/ProcessExecutorTest.java26
4 files changed, 0 insertions, 149 deletions
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<ProcessResult> 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<ProcessResult> 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<ProcessResult> result = executor.execute("echo " + message);
- assertTrue(result.isPresent());
- assertEquals(message, result.get().stdOut.trim());
- assertEquals("", result.get().stdErr);
- }
-}