Skip to content
Snippets Groups Projects
Commit cc894a5f authored by Georg Seibt's avatar Georg Seibt :nerd:
Browse files

doc

parent aaf1d04d
No related branches found
No related tags found
No related merge requests found
...@@ -8,18 +8,34 @@ import java.util.logging.Logger; ...@@ -8,18 +8,34 @@ import java.util.logging.Logger;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
/**
* Contains static methods for executing commands and gathering their output and exit code.
*/
public class ProcessExecutor { public class ProcessExecutor {
private static final Logger LOG = Logger.getLogger(ProcessExecutor.class.getCanonicalName()); private static final Logger LOG = Logger.getLogger(ProcessExecutor.class.getCanonicalName());
/** /**
* The result of a command line execution. * The result of a process execution.
*/ */
public static class ExecRes { public static class ExecRes {
public int exitCode; public int exitCode;
public String output; public String output;
} }
/**
* Executes the given <code>cmd</code> with the supplied <code>parameters</code>.
*
* @param cmd
* the command to execute
* @param workingDir
* the working directory for the execution
* @param redirectError
* whether to merge the standard output and error streams
* @param parameters
* the parameters for the command (may be null or none)
* @return the result of the execution or an empty {@link Optional} if there is an exception executing the command
*/
public static Optional<ExecRes> exec(String cmd, File workingDir, boolean redirectError, String... parameters) { public static Optional<ExecRes> exec(String cmd, File workingDir, boolean redirectError, String... parameters) {
if (parameters == null) { if (parameters == null) {
...@@ -34,6 +50,18 @@ public class ProcessExecutor { ...@@ -34,6 +50,18 @@ public class ProcessExecutor {
return exec(workingDir, redirectError, cmdArray); return exec(workingDir, redirectError, cmdArray);
} }
/**
* Executes the given <code>command</code>. If <code>command</code> is <code>null</code> no var-args are given
* and empty {@link Optional} will be returned.
*
* @param workingDir
* the working directory for the execution
* @param redirectError
* whether to merge the standard output and error streams
* @param command
* the command to be executed
* @return the result of the execution or an empty {@link Optional} if there is an exception executing the command
*/
public static Optional<ExecRes> exec(File workingDir, boolean redirectError, String... command) { public static Optional<ExecRes> exec(File workingDir, boolean redirectError, String... command) {
if (command == null) { if (command == null) {
...@@ -49,6 +77,13 @@ public class ProcessExecutor { ...@@ -49,6 +77,13 @@ public class ProcessExecutor {
return exec(builder); return exec(builder);
} }
/**
* Executes the command represented by the given <code>builder</code>.
*
* @param builder
* the {@link ProcessBuilder} to be executed
* @return the result of the execution or an empty {@link Optional} if there is an exception executing the command
*/
public static Optional<ExecRes> exec(ProcessBuilder builder) { public static Optional<ExecRes> exec(ProcessBuilder builder) {
String cmd = String.join(" ", builder.command()); String cmd = String.join(" ", builder.command());
LOG.fine(() -> String.format("Executing '%s'.", cmd)); LOG.fine(() -> String.format("Executing '%s'.", cmd));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment