/**
 * Copyright (c) 2013, Groupon, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * Neither the name of GROUPON nor the names of its contributors may be
 * used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 * Created with IntelliJ IDEA.
 * User: Dima Kovalenko (@dimacus) && Darko Marinov
 * Date: 5/10/13
 * Time: 4:06 PM
 */

package com.groupon;

import org.json.simple.JSONValue;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;


class ExecuteCommand {
  public static String execRuntime(String cmd) {
    return execRuntime(cmd, true);
  }

  public static String execRuntime(String cmd, boolean waitToFinish) {
    System.out.println("Starting to execute - " + cmd);
    Process process;

    try {
      process = Runtime.getRuntime().exec(cmd);
    } catch (IOException e) {
      return formatResult(1, "", "Problems in running " + cmd + "\n" + e.toString());
    }

    int exitCode;
    if (waitToFinish) {
      try {
        System.out.println("Waiting to finish");
        exitCode = process.waitFor();
        System.out.println("Command Finished");
      } catch (InterruptedException e) {
        return formatResult(1, "", "Interrupted running " + cmd + "\n" + e.toString());
      }
    } else {
      System.out.println("Not waiting for finish");
      return formatResult(0, "Background process started", "");
    }

    try {
      String output = inputStreamToString(process.getInputStream());
      String error = inputStreamToString(process.getErrorStream());
      String returnResults = formatResult(exitCode, output, error);
      return returnResults;
    } catch (IOException e) {
      return formatResult(1, "", "Problems reading stdout and stderr from " + cmd + "\n" + e.toString());
    } finally {
      process.destroy();
    }
  }

  public static String formatResult(int result, String output, String error) {

    Map resultsHash = new HashMap();
    resultsHash.put("exit_code", result);
    resultsHash.put("standard_out", output);
    resultsHash.put("standard_error", error);

    return JSONValue.toJSONString(resultsHash);
  }

  public static String inputStreamToString(InputStream is) throws IOException {
    StringBuilder result = new StringBuilder();
    int in;
    while ((in = is.read()) != -1) {
      result.append((char) in);
    }
    is.close();
    return result.toString();
  }
}

@@ -1686,16 +1686,85 @@
 oupon;%0A%0A
+import org.json.simple.JSONArray;%0Aimport org.json.simple.JSONObject;%0A
 import o
@@ -3315,19 +3315,26 @@
  %7B%0A%0A    
-Map
+JSONObject
  results
@@ -3348,18 +3348,359 @@
 new 
-HashMap();
+JSONObject();%0A    JSONArray standardOut = new JSONArray();%0A    JSONArray standardError = new JSONArray();%0A%0A    String stdOutLines%5B%5D = output.split(%22%5Cn%22);%0A    for(String line: stdOutLines) %7B%0A      standardOut.add(line);%0A    %7D%0A%0A    String stdErrorLines%5B%5D = error.split(%22%5Cn%22);%0A    for(String line: stdErrorLines) %7B%0A      standardError.add(line);%0A    %7D%0A%0A%0A
 %0A   
@@ -3774,20 +3774,25 @@
 d_out%22, 
-outp
+standardO
 ut);%0A   
@@ -3826,17 +3826,25 @@
 error%22, 
-e
+standardE
 rror);%0A%0A
@@ -3858,31 +3858,8 @@
 urn 
-JSONValue.toJSONString(
 resu
@@ -3865,16 +3865,26 @@
 ultsHash
+.toString(
 );%0A  %7D%0A%0A

Make the standard error and out return an array with each item on new line