Skip to content

Commit

Permalink
Move away of depracated commons-exec apis
Browse files Browse the repository at this point in the history
  • Loading branch information
akurtakov committed May 24, 2024
1 parent 26eaabd commit 54b04e0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008, 2019 Sonatype Inc. and others.
* Copyright (c) 2008, 2024 Sonatype Inc. and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -14,12 +14,14 @@

import java.io.File;
import java.io.IOException;
import java.time.Duration;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.DefaultExecutor.Builder;
import org.apache.commons.exec.ExecuteException;
import org.apache.commons.exec.ExecuteWatchdog;
import org.apache.commons.exec.ShutdownHookProcessDestroyer;
Expand Down Expand Up @@ -57,20 +59,21 @@ public int execute(LaunchConfiguration configuration, int forkedProcessTimeoutIn

cli.addArguments(configuration.getProgramArguments(), handleQuotes);

DefaultExecutor executor = new DefaultExecutor();
Builder<?> executorBuilder = DefaultExecutor.builder();
executorBuilder.setWorkingDirectory(configuration.getWorkingDirectory());
DefaultExecutor executor = executorBuilder.get();
ExecuteWatchdog watchdog = null;
if (forkedProcessTimeoutInSeconds > 0) {
long timeoutInMilliseconds = forkedProcessTimeoutInSeconds * 1000L;
cli.addArguments(new String[] { "-timeout ", String.valueOf(timeoutInMilliseconds) });
watchdog = new ExecuteWatchdog(timeoutInMilliseconds);
watchdog = ExecuteWatchdog.builder().setTimeout(Duration.ofMillis(timeoutInMilliseconds)).get();
executor.setWatchdog(watchdog);
}

log.info("Command line: " + Arrays.stream(cli.toStrings()).collect(Collectors.joining(" ")));

// best effort to avoid orphaned child process
executor.setProcessDestroyer(new ShutdownHookProcessDestroyer());
executor.setWorkingDirectory(configuration.getWorkingDirectory());
try {
return executor.execute(cli, getMergedEnvironment(configuration));
} catch (ExecuteException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2018 SAP SE and others.
* Copyright (c) 2018, 2024 SAP SE and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -16,6 +16,7 @@
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -82,8 +83,8 @@ private LibraryInfo generateLibraryInfo(String javaHome) throws ArtifactResoluti
CommandLine cli = new CommandLine(executable);
cli.addArguments(new String[] { "-classpath", getLibDetectorJar().getAbsolutePath(),
"org.eclipse.tycho.libdetector.LibraryDetector" }, false);
DefaultExecutor executor = new DefaultExecutor();
ExecuteWatchdog watchdog = new ExecuteWatchdog(30 * 1000L);
DefaultExecutor executor = DefaultExecutor.builder().get();
ExecuteWatchdog watchdog = ExecuteWatchdog.builder().setTimeout(Duration.ofMillis(30 * 1000L)).get();
executor.setWatchdog(watchdog);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PumpStreamHandler handler = new PumpStreamHandler(outputStream);
Expand Down

0 comments on commit 54b04e0

Please sign in to comment.