Skip to content

Commit

Permalink
openvidu-server: CommandExecutor return output as list
Browse files Browse the repository at this point in the history
  • Loading branch information
pabloFuente committed Nov 2, 2020
1 parent 79648c4 commit e9426ae
Showing 1 changed file with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
Expand All @@ -37,13 +39,20 @@ public class CommandExecutor {
public static String execCommand(long msTimeout, String... command) throws IOException, InterruptedException {
ProcessBuilder processBuilder = new ProcessBuilder(command);
processBuilder.redirectErrorStream(true);
return commonExecCommand(msTimeout, processBuilder);
return commonExecCommand(msTimeout, processBuilder, true).get(0);
}

public static List<String> execCommandReturnList(long msTimeout, String... command)
throws IOException, InterruptedException {
ProcessBuilder processBuilder = new ProcessBuilder(command);
processBuilder.redirectErrorStream(true);
return commonExecCommand(msTimeout, processBuilder, false);
}

public static String execCommandRedirectError(long msTimeout, File errorOutputFile, String... command)
throws IOException, InterruptedException {
ProcessBuilder processBuilder = new ProcessBuilder(command).redirectError(errorOutputFile);
return commonExecCommand(msTimeout, processBuilder);
return commonExecCommand(msTimeout, processBuilder, true).get(0);
}

public static void execCommandRedirectStandardOutputAndError(long msTimeout, File standardOutputFile,
Expand All @@ -59,29 +68,34 @@ public static void execCommandRedirectStandardOutputAndError(long msTimeout, Fil
}
}

private static String commonExecCommand(long msTimeout, ProcessBuilder processBuilder)
private static List<String> commonExecCommand(long msTimeout, ProcessBuilder processBuilder, boolean singleString)
throws IOException, InterruptedException {
Process process = processBuilder.start();
StringBuilder processOutput = new StringBuilder();
String output;
List<String> outputList = new ArrayList<>();
InputStreamReader inputStreamReader = null;
BufferedReader processOutputReader = null;
try {
inputStreamReader = new InputStreamReader(process.getInputStream());
processOutputReader = new BufferedReader(inputStreamReader);
String readLine;
while ((readLine = processOutputReader.readLine()) != null) {
processOutput.append(readLine + System.lineSeparator());
if (singleString) {
processOutput.append(readLine + System.lineSeparator());
} else {
outputList.add(readLine);
}
}
if (singleString) {
outputList = Arrays.asList(processOutput.toString().trim());
}

if (!process.waitFor(msTimeout, TimeUnit.MILLISECONDS)) {
log.error("Command {} did not receive a response in {} ms",
Arrays.toString(processBuilder.command().toArray()), msTimeout);
String errorMsg = "Current process status of host:\n" + gatherLinuxHostInformation();
log.error(errorMsg);
throw new IOException(errorMsg);
}
output = processOutput.toString().trim();
} finally {
if (inputStreamReader != null) {
inputStreamReader.close();
Expand All @@ -90,7 +104,7 @@ private static String commonExecCommand(long msTimeout, ProcessBuilder processBu
processOutputReader.close();
}
}
return output;
return outputList;
}

public static String gatherLinuxHostInformation() throws IOException, InterruptedException {
Expand Down

0 comments on commit e9426ae

Please sign in to comment.