Merge pull request #921 from rcordovano/exec_util_changes

Improve new ExecUtils API
This commit is contained in:
Richard Cordovano 2014-10-27 15:12:45 -04:00
commit 90d3586a97

View File

@ -32,10 +32,13 @@ import java.util.logging.Level;
*/ */
public final class ExecUtil { public final class ExecUtil {
private static final long DEFAULT_TIMEOUT = 1000;
private static final TimeUnit DEFAULT_TIMEOUT_UNITS = TimeUnit.MILLISECONDS;
/** /**
* The execute() methods do a wait with a timeout on the executing process and * The execute() methods do a wait with a timeout on the executing process
* query the terminator each time the timeout expires to determine whether * and query the terminator each time the timeout expires to determine
* or not to kill the process or allow it to continue. * whether or not to kill the process or allow it to continue.
*/ */
public interface ProcessTerminator { public interface ProcessTerminator {
@ -50,10 +53,17 @@ public final class ExecUtil {
} }
/** /**
* The default, do-nothing process terminator. * Runs a process without a timeout and terminator.
*
* @param processBuilder A process builder used to configure and construct
* the process to be run.
* @return the exit value of the process
* @throws SecurityException if a security manager exists and vetoes any
* aspect of running the process.
* @throws IOException if an I/O error occurs.
*/ */
private static class NullProcessTerminator implements ProcessTerminator { public static int execute(ProcessBuilder processBuilder) throws SecurityException, IOException {
return ExecUtil.execute(processBuilder, 30, TimeUnit.DAYS, new ProcessTerminator() {
/** /**
* @inheritDoc * @inheritDoc
*/ */
@ -61,22 +71,22 @@ public final class ExecUtil {
public boolean shouldTerminateProcess() { public boolean shouldTerminateProcess() {
return false; return false;
} }
});
} }
/** /**
* Runs a process using a default do-nothing terminator. * Runs a process using the default timeout and a custom terminator.
* *
* @param processBuilder A process builder used to configure and construct * @param processBuilder A process builder used to configure and construct
* the process to be run. * the process to be run.
* @param timeOut The duration of the timeout. * @param terminator The terminator.
* @param units The units for the timeout.
* @return the exit value of the process * @return the exit value of the process
* @throws SecurityException if a security manager exists and vetoes any * @throws SecurityException if a security manager exists and vetoes any
* aspect of running the process. * aspect of running the process.
* @throws IOException if an I/o error occurs. * @throws IOException if an I/O error occurs.
*/ */
public static int execute(ProcessBuilder processBuilder, long timeOut, TimeUnit units) throws SecurityException, IOException { public static int execute(ProcessBuilder processBuilder, ProcessTerminator terminator) throws SecurityException, IOException {
return ExecUtil.execute(processBuilder, timeOut, units, new ExecUtil.NullProcessTerminator()); return ExecUtil.execute(processBuilder, ExecUtil.DEFAULT_TIMEOUT, ExecUtil.DEFAULT_TIMEOUT_UNITS, terminator);
} }
/** /**