openvidu-server: CommandExecutor return output as list

pull/553/head
pabloFuente 2020-11-02 14:13:04 +01:00
parent 79648c4006
commit e9426aebef
1 changed files with 22 additions and 8 deletions

View File

@ -21,7 +21,9 @@ import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -37,13 +39,20 @@ public class CommandExecutor {
public static String execCommand(long msTimeout, String... command) throws IOException, InterruptedException { public static String execCommand(long msTimeout, String... command) throws IOException, InterruptedException {
ProcessBuilder processBuilder = new ProcessBuilder(command); ProcessBuilder processBuilder = new ProcessBuilder(command);
processBuilder.redirectErrorStream(true); 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) public static String execCommandRedirectError(long msTimeout, File errorOutputFile, String... command)
throws IOException, InterruptedException { throws IOException, InterruptedException {
ProcessBuilder processBuilder = new ProcessBuilder(command).redirectError(errorOutputFile); 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, public static void execCommandRedirectStandardOutputAndError(long msTimeout, File standardOutputFile,
@ -59,11 +68,11 @@ public class CommandExecutor {
} }
} }
private static String commonExecCommand(long msTimeout, ProcessBuilder processBuilder) private static List<String> commonExecCommand(long msTimeout, ProcessBuilder processBuilder, boolean singleString)
throws IOException, InterruptedException { throws IOException, InterruptedException {
Process process = processBuilder.start(); Process process = processBuilder.start();
StringBuilder processOutput = new StringBuilder(); StringBuilder processOutput = new StringBuilder();
String output; List<String> outputList = new ArrayList<>();
InputStreamReader inputStreamReader = null; InputStreamReader inputStreamReader = null;
BufferedReader processOutputReader = null; BufferedReader processOutputReader = null;
try { try {
@ -71,9 +80,15 @@ public class CommandExecutor {
processOutputReader = new BufferedReader(inputStreamReader); processOutputReader = new BufferedReader(inputStreamReader);
String readLine; String readLine;
while ((readLine = processOutputReader.readLine()) != null) { 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)) { if (!process.waitFor(msTimeout, TimeUnit.MILLISECONDS)) {
log.error("Command {} did not receive a response in {} ms", log.error("Command {} did not receive a response in {} ms",
Arrays.toString(processBuilder.command().toArray()), msTimeout); Arrays.toString(processBuilder.command().toArray()), msTimeout);
@ -81,7 +96,6 @@ public class CommandExecutor {
log.error(errorMsg); log.error(errorMsg);
throw new IOException(errorMsg); throw new IOException(errorMsg);
} }
output = processOutput.toString().trim();
} finally { } finally {
if (inputStreamReader != null) { if (inputStreamReader != null) {
inputStreamReader.close(); inputStreamReader.close();
@ -90,7 +104,7 @@ public class CommandExecutor {
processOutputReader.close(); processOutputReader.close();
} }
} }
return output; return outputList;
} }
public static String gatherLinuxHostInformation() throws IOException, InterruptedException { public static String gatherLinuxHostInformation() throws IOException, InterruptedException {