8: Create ProcessExecutor

This commit is contained in:
Bartłomiej Pluta
2019-04-02 20:04:09 +02:00
parent 60b141ac1a
commit ced7b1017d

View File

@@ -0,0 +1,31 @@
package com.bartek.esa.decompiler.process;
import com.bartek.esa.error.EsaException;
import io.vavr.control.Try;
import javax.inject.Inject;
public class ProcessExecutor {
@Inject
public ProcessExecutor() {
}
public void execute(String[] command) {
Process process = Try.of(() -> Runtime.getRuntime().exec(command))
.getOrElseThrow(EsaException::new);
waitForProcess(process);
checkExitValue(process, command[0]);
}
private void waitForProcess(Process process) {
Try.run(process::waitFor).getOrElseThrow(EsaException::new);
}
private void checkExitValue(Process process, String commandName) {
if(process.exitValue() != 0) {
throw new EsaException("'" + commandName + "' process has finished with non-zero code. Interrupting...");
}
}
}