TimeoutProcessBuilder.java

  1. /*
  2.  * TimeoutProcessBuilder
  3.  *
  4.  * $Id$
  5.  * $URL$
  6.  */
  7. package gov.usgs.util;

  8. import java.io.File;
  9. import java.io.IOException;
  10. import java.util.List;
  11. import java.util.Map;
  12. import java.util.Timer;
  13. import java.util.TimerTask;

  14. /**
  15.  * The TimeoutProcessBuilder wraps a ProcessBuilder, adding support for a
  16.  * command time out.
  17.  *
  18.  * This class does not support a full command String complete with arguments.
  19.  * You can use the StringUtils.split method to get around this.
  20.  *
  21.  * @see java.lang.ProcessBuilder
  22.  * @see TimeoutProcess
  23.  */
  24. public class TimeoutProcessBuilder {

  25.     /** The wrapped process builder. */
  26.     private ProcessBuilder builder = null;
  27.     /** The timeout for this process. */
  28.     private long timeout = -1;

  29.     /**
  30.      * Create a new TimeoutProcessBuilder with a timeout and an array of
  31.      * strings.
  32.      *
  33.      * @param timeout
  34.      *            timeout in milliseconds for process, or <= 0 for no timeout.
  35.      * @param command
  36.      *            array of strings that represent command. The first element
  37.      *            must be the full path to the executable, without arguments.
  38.      */
  39.     public TimeoutProcessBuilder(long timeout, String... command) {
  40.         builder = new ProcessBuilder(command);
  41.         this.timeout = timeout;
  42.     }

  43.     /**
  44.      * Create a new TimeoutProcessBuilder with a timeout and an array of
  45.      * strings.
  46.      *
  47.      * @param timeout
  48.      *            timeout in milliseconds for process, or <= 0 for no timeout.
  49.      * @param command
  50.      *            list of strings that represent command.
  51.      */
  52.     public TimeoutProcessBuilder(long timeout, List<String> command) {
  53.         builder = new ProcessBuilder(command);
  54.         this.timeout = timeout;

  55.     }

  56.     /**
  57.      * This signature is preserved, but calls the alternate constructor with
  58.      * argument order swapped.
  59.      * @param command
  60.      *            list of strings that represent command.
  61.      * @param timeout
  62.      *            timeout in milliseconds for process, or &lt;= 0 for no timeout.
  63.      */
  64.     @Deprecated
  65.     public TimeoutProcessBuilder(List<String> command, long timeout) {
  66.         this(timeout, command);
  67.     }

  68.     /** @return list of builder commands */
  69.     public List<String> command() {
  70.         return builder.command();
  71.     }

  72.     /**
  73.      * @param command give builder a list of commands
  74.      * @return TimeoutProcessBuilder
  75.      */
  76.     public TimeoutProcessBuilder command(List<String> command) {
  77.         builder.command(command);
  78.         return this;
  79.     }

  80.     /**
  81.      * @param command give builder a single command
  82.      * @return TimeoutProcessBuilder
  83.      */
  84.     public TimeoutProcessBuilder command(String command) {
  85.         builder.command(command);
  86.         return this;
  87.     }

  88.     /** @return builder directory */
  89.     public File directory() {
  90.         return builder.directory();
  91.     }

  92.     /**
  93.      * @param directory to set
  94.      * @return TimeoutProcessBuilder
  95.      */
  96.     public TimeoutProcessBuilder directory(File directory) {
  97.         builder.directory(directory);
  98.         return this;
  99.     }

  100.     /** @return builder environment */
  101.     public Map<String, String> environment() {
  102.         return builder.environment();
  103.     }

  104.     /** @return boolean redirectErrorStream */
  105.     public boolean redirectErrorStream() {
  106.         return builder.redirectErrorStream();
  107.     }

  108.     /**
  109.      * @param redirectErrorStream to set
  110.      * @return TimeoutProcessBuilder
  111.      */
  112.     public TimeoutProcessBuilder redirectErrorStream(boolean redirectErrorStream) {
  113.         builder.redirectErrorStream(redirectErrorStream);
  114.         return this;
  115.     }

  116.     /**
  117.      * @return a TimeoutProcess
  118.      * @throws IOException if IO error occurs
  119.      */
  120.     public TimeoutProcess start() throws IOException {
  121.         final TimeoutProcess process = new TimeoutProcess(builder.start());

  122.         if (timeout > 0) {
  123.             // set up the timeout for this process
  124.             final Timer timer = new Timer();
  125.             timer.schedule(new TimerTask() {
  126.                 @Override
  127.                 public void run() {
  128.                     process.setTimeoutElapsed(true);
  129.                     process.destroy();
  130.                 }
  131.             }, timeout);
  132.             process.setTimer(timer);
  133.         }

  134.         return process;
  135.     }

  136.     /** @return timeout */
  137.     public long getTimeout() {
  138.         return this.timeout;
  139.     }

  140.     /** @param timeout to set */
  141.     public void setTimeout(final long timeout) {
  142.         this.timeout = timeout;
  143.     }

  144. }