package droplauncher.util;

import adakite.debugging.Debugging;
import adakite.util.AdakiteUtils;
import java.io.IOException;
import java.nio.file.Path;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * Generic class for handling communication between the main program and
 * a process.
 */
public class ProcessPipe {

  private static final Logger LOGGER = LogManager.getLogger();

  private Process process;
  private String[] args;
  private Path cwd;
  private StreamGobbler gobblerStdout;
  private StreamGobbler gobblerStderr;

  public ProcessPipe() {
    this.process = null;
    this.args = null;
    this.cwd = null;
    this.gobblerStdout = null;
    this.gobblerStderr = null;
  }

  public String[] getArgs() {
    return this.args;
  }

  public void setArgs(String[] args) {
    this.args = args;
  }

  public Path getCWD() {
    return this.cwd;
  }

  public void setCWD(Path path) {
    this.cwd = path;
  }

  public void open(String[] args) throws IOException {
    if (args == null) {
      throw new IllegalArgumentException(Debugging.nullObject("args"));
    }

    this.args = args;

    ProcessBuilder pb = new ProcessBuilder(this.args);
    if (this.cwd != null && AdakiteUtils.directoryExists(this.cwd)) {
      /* Set current working directory for the new process. */
      pb.directory(this.cwd.toFile());
    }

    this.process = pb.start();

    this.gobblerStdout = new StreamGobbler(this.process.getInputStream());
    this.gobblerStderr = new StreamGobbler(this.process.getErrorStream());
    this.gobblerStdout.start();
    this.gobblerStderr.start();
  }

  public void close() {
    this.gobblerStdout.interrupt();
    this.gobblerStdout.interrupt();
    this.process.destroy();
    if (this.process.isAlive()) {
      LOGGER.warn("process is still alive after attempting to destroy");
    }
  }

}

@@ -242,15 +242,17 @@
 %0A * 
-Ge
+Contai
 ner
-ic
  cla
@@ -262,64 +262,35 @@
 for 
-handling communication between the main program and%0A * a
+starting and stopping a new
  pro
@@ -708,31 +708,98 @@
 %0A%0A  
-public String%5B%5D getArgs
+/**%0A   * Returns the current working directory for the process.%0A   */%0A  public Path getCWD
 () %7B
@@ -815,20 +815,19 @@
 rn this.
-args
+cwd
 ;%0A  %7D%0A%0A 
@@ -831,182 +831,354 @@
 %0A%0A  
-public void setArgs(String%5B%5D args) %7B%0A    this.args = args;%0A  %7D%0A%0A  public Path getCWD() %7B%0A    return this.cwd;%0A  %7D%0A%0A  public void setCWD(Path path) %7B%0A    this.cwd = path;%0A  %7D%0A
+/**%0A   * Sets the current working directory for the executable.%0A   *%0A   * @param path specified current working directory%0A   */%0A  public void setCWD(Path path) %7B%0A    this.cwd = path;%0A  %7D%0A%0A  /**%0A   * Creates and opens the pipe to the specified executable.%0A   *%0A   * @param args specified command and arguments to run%0A   * @throws IOException%0A   */
 %0A  p
@@ -1840,16 +1840,61 @@
 );%0A  %7D%0A%0A
+  /**%0A   * Attempts to close the pipe.%0A   */%0A
   public
@@ -2095,29 +2095,23 @@
 ter 
-attempting to destroy
+destroy attempt
 %22);%0A

Update ProcessPipe.java