use of org.apache.commons.exec.PumpStreamHandler in project tycho by eclipse.
the class JdkLibraryInfoProvider method generateLibraryInfo.
private LibraryInfo generateLibraryInfo(String javaHome) {
String executable = javaHome + File.separator + "bin" + File.separator + "java";
if (File.separatorChar == '\\') {
executable = executable + ".exe";
}
if (!new File(executable).isFile()) {
getLog().warn(executable + " not found. Fallback to scan " + javaHome + "/lib/*.jar and " + javaHome + "/lib/ext/*.jar for bootclasspath");
return new LibraryInfo("unknown", scanLibFolders(javaHome), new String[0], new String[0]);
}
CommandLine cli = new CommandLine(executable);
cli.addArguments(new String[] { "-classpath", getLibDetectorJar().getAbsolutePath(), "org.eclipse.tycho.libdetector.LibraryDetector" }, false);
DefaultExecutor executor = new DefaultExecutor();
ExecuteWatchdog watchdog = new ExecuteWatchdog(30 * 1000L);
executor.setWatchdog(watchdog);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PumpStreamHandler handler = new PumpStreamHandler(outputStream);
executor.setStreamHandler(handler);
int exitValue = -1;
try {
exitValue = executor.execute(cli);
} catch (ExecuteException e) {
if (watchdog.killedProcess()) {
throw new RuntimeException("Timeout 30 s exceeded. Commandline " + cli.toString() + " was killed. Output: " + outputStream.toString());
}
} catch (IOException e) {
throw new RuntimeException(e);
}
if (exitValue == 0) {
return parseLibraryInfo(outputStream.toString(), javaHome);
} else {
throw new RuntimeException(cli.toString() + " process exit code was " + exitValue + ". Output: " + outputStream.toString());
}
}
use of org.apache.commons.exec.PumpStreamHandler in project frontend-maven-plugin by eirslett.
the class ProcessExecutor method execute.
private int execute(final Logger logger, final OutputStream stdout, final OutputStream stderr) throws ProcessExecutionException {
logger.debug("Executing command line {}", commandLine);
try {
ExecuteStreamHandler streamHandler = new PumpStreamHandler(stdout, stderr);
executor.setStreamHandler(streamHandler);
int exitValue = executor.execute(commandLine, environment);
logger.debug("Exit value {}", exitValue);
return exitValue;
} catch (ExecuteException e) {
if (executor.getWatchdog() != null && executor.getWatchdog().killedProcess()) {
throw new ProcessExecutionException("Process killed after timeout");
}
throw new ProcessExecutionException(e);
} catch (IOException e) {
throw new ProcessExecutionException(e);
}
}
use of org.apache.commons.exec.PumpStreamHandler in project Robot by fo0.
the class Commander method execute.
public String execute(boolean wait, boolean shell, String homedir, boolean quoting, List<String> cmds) {
if (cmds == null || cmds.isEmpty()) {
Logger.info("stopped cmd command is empty");
return null;
}
CommandLine cmdLine = null;
if (shell) {
switch(OSCheck.getOperatingSystemType()) {
case Windows:
cmdLine = new CommandLine("cmd");
if (shell) {
cmdLine.addArgument("/c");
}
break;
case Linux:
cmdLine = new CommandLine("/bin/bash");
if (shell) {
cmdLine.addArgument("-c");
}
break;
}
cmdLine.addArguments(cmds.stream().toArray(String[]::new), quoting);
} else {
cmdLine = new CommandLine(cmds.get(0));
cmdLine.addArguments(cmds.stream().skip(1).toArray(String[]::new), quoting);
}
Logger.debug("HomeDir: '" + Paths.get(homedir).toAbsolutePath() + "' => " + cmdLine.getExecutable() + ", " + StringUtils.join(cmdLine.getArguments(), ","));
try {
Executor executor = createDefaultExecutor();
executor.setStreamHandler(new PumpStreamHandler(new OutputStream() {
@Override
public void write(int b) throws IOException {
try {
String str = String.valueOf((char) b);
if (listener != null)
listener.event(str);
buffer.append(str);
} catch (Exception e) {
Logger.debug(e.getMessage());
}
}
}, new OutputStream() {
@Override
public void write(int b) throws IOException {
error = true;
try {
String str = String.valueOf((char) b);
if (listener != null)
listener.event(str);
buffer.append(str);
} catch (Exception e) {
Logger.debug(e.getMessage());
}
}
}));
// configure timeout
// executor.setWatchdog(new ExecuteWatchdog(-1));
executor.setWorkingDirectory(new File(homedir));
if (wait) {
executor.execute(cmdLine);
} else {
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
executor.execute(cmdLine, resultHandler);
}
} catch (Exception e) {
error = true;
Logger.error("failed commander in Cmd: " + cmdLine + " | " + e);
Logger.debug(e.getMessage());
}
return buffer.toString();
}
Aggregations