package com.heroku.sdk.maven;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Opens the Heroku Dashboard for an application
 *
 * @goal run-war
 * @execute phase="package"
 * @requiresDependencyResolution
 */
public class RunWarMojo extends HerokuWarMojo {

  @Override
  public void execute() throws MojoExecutionException, MojoFailureException {
    try {
      prepareWarFile();

      String javaCommand = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java";

      String javaOpts = configVars.get("JAVA_OPTS");
      String webappRunnerOpts = configVars.get("WEBAPP_RUNNER_OPTS");

      List<String> fullCommand = new ArrayList<String>();
      fullCommand.add(javaCommand);
      if (null != javaOpts) fullCommand.add(javaOpts);
      fullCommand.add("-jar");
      fullCommand.add("target" + File.separator + "dependency" + File.separator + "webapp-runner.jar");
      if (null != webappRunnerOpts) fullCommand.add(webappRunnerOpts);
      fullCommand.add(warFile.getAbsolutePath());

      ProcessBuilder pb = new ProcessBuilder().command(fullCommand.toArray(new String[fullCommand.size()]));

      String fullCommandLine = javaCommand;
      for (String s : fullCommand) {
        fullCommandLine += " " + s;
      }
      getLog().debug("Executing: " + fullCommandLine);

      Process p = pb.start();

      StreamGobbler inputGobbler = new StreamGobbler(p.getInputStream());
      inputGobbler.start();

      StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream());
      errorGobbler.start();

      p.waitFor();
    } catch (Exception e) {
      throw new MojoFailureException("Failed to deploy application", e);
    }
  }

  private class StreamGobbler extends Thread {
    InputStream is;

    StreamGobbler(InputStream is) {
      this.is = is;
    }

    public void run() {
      try {
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line;
        while ((line = br.readLine()) != null)
          getLog().info(line);
      } catch (IOException e) {
        getLog().error(e.getMessage());
        e.printStackTrace();
        throw new RuntimeException(e);
      }
    }
  }
}

@@ -179,16 +179,41 @@
 ayList;%0A
+import java.util.Arrays;%0A
 import j
@@ -461,32 +461,8 @@
 rows
- MojoExecutionException,
  Moj
@@ -869,24 +869,113 @@
 != javaOpts)
+ %7B%0A        for (String javaOpt : javaOpts.split(%22 %22)) %7B%0A          if (!javaOpt.isEmpty())
  fullCommand
@@ -986,20 +986,38 @@
 (javaOpt
-s
 );%0A
+        %7D%0A      %7D%0A%0A
       fu
@@ -1178,16 +1178,129 @@
 nerOpts)
+ %7B%0A        for (String webappRunnerOpt : webappRunnerOpts.split(%22 %22)) %7B%0A          if (!webappRunnerOpt.isEmpty())
  fullCom
@@ -1323,20 +1323,38 @@
 unnerOpt
-s
 );%0A
+        %7D%0A      %7D%0A%0A
       fu

Split JAVA_OPTS and WEBAPP_RUNNER_OPTS on whitespace. Fixes #38