本文整理汇总了Java中org.apache.commons.exec.PumpStreamHandler类的典型用法代码示例。如果您正苦于以下问题:Java PumpStreamHandler类的具体用法?Java PumpStreamHandler怎么用?Java PumpStreamHandler使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
PumpStreamHandler类属于org.apache.commons.exec包,在下文中一共展示了PumpStreamHandler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: call
import org.apache.commons.exec.PumpStreamHandler; //导入依赖的package包/类
@Override
public Long call() throws Exception {
Executor executor = new DefaultExecutor();
executor.setProcessDestroyer(new ShutdownHookProcessDestroyer());
ExecuteWatchdog watchDog = new ExecuteWatchdog(watchdogTimeout);
executor.setWatchdog(watchDog);
executor.setStreamHandler(new PumpStreamHandler(new MyLogOutputStream(handler, true), new MyLogOutputStream(handler, false)));
Long exitValue;
try {
exitValue = new Long(executor.execute(commandline));
} catch (ExecuteException e) {
exitValue = new Long(e.getExitValue());
}
if (watchDog.killedProcess()) {
exitValue = WATCHDOG_EXIST_VALUE;
}
return exitValue;
}
示例2: run
import org.apache.commons.exec.PumpStreamHandler; //导入依赖的package包/类
public void run() {
try {
executor = new DaemonExecutor();
resultHandler = new DefaultExecuteResultHandler();
String javaHome = System.getProperty("java.home");
String userDir = System.getProperty("user.dir");
executor.setStreamHandler(new PumpStreamHandler(System.out));
watchdog = new ExecuteWatchdog(15000);
executor.setWatchdog(watchdog);
executor.execute(new CommandLine(javaHome + SystemUtils.FILE_SEPARATOR
+ "bin"+ SystemUtils.FILE_SEPARATOR+"java.exe").addArgument("-version"));
executor.execute(new CommandLine(javaHome + SystemUtils.FILE_SEPARATOR
+ "bin"+ SystemUtils.FILE_SEPARATOR+"java.exe")
.addArgument("-jar")
.addArgument(userDir + "/../moneta-springboot/target/moneta-springboot-" + ContractTestSuite.getProjectVersion() + ".jar"));
}
catch (Exception e) {
e.printStackTrace();
}
}
示例3: runMavenCommand
import org.apache.commons.exec.PumpStreamHandler; //导入依赖的package包/类
@When("I run maven with args: (.*)")
public void runMavenCommand(List<String> mvnArgs) throws IOException {
this.mvnArgs.addAll(mvnArgs);
System.out.println("Launching Maven with args <" + Joiner.on(" ").join(mvnArgs) + ">");
CommandLine cmdLine = new CommandLine(getCommandLine());
for (String mvnArg : mvnArgs) {
cmdLine.addArgument(mvnArg);
}
if (confdForCucumberLocation != null) {
cmdLine.addArgument("-Dcucumber.confd.binary.path=" + confdForCucumberLocation);
}
DefaultExecutor executor = new DefaultExecutor();
if (projectRootAsFile != null) {
executor.setWorkingDirectory(projectRootAsFile);
}
executor.setExitValue(expectedExitCode);
executor.setStreamHandler(new PumpStreamHandler(new LogOutputStream() {
@Override
protected void processLine(String line, int level) {
System.out.println(line);
executorOutput.add(line);
}
}));
exitCode = executor.execute(cmdLine, environment);
fullOutput = Joiner.on(LINE_SEPARATOR).join(executorOutput);
}
示例4: execToFile
import org.apache.commons.exec.PumpStreamHandler; //导入依赖的package包/类
/**
* 日志文件输出方式
*
* 优点:支持将目标数据实时输出到指定日志文件中去
* 缺点:
* 标准输出和错误输出优先级固定,可能和脚本中顺序不一致
* Java无法实时获取
*
* @param command
* @param scriptFile
* @param logFile
* @param params
* @return
* @throws IOException
*/
public static int execToFile(String command, String scriptFile, String logFile, String... params) throws IOException {
// 标准输出:print (null if watchdog timeout)
// 错误输出:logging + 异常 (still exists if watchdog timeout)
// 标准输入
FileOutputStream fileOutputStream = new FileOutputStream(logFile, true);
PumpStreamHandler streamHandler = new PumpStreamHandler(fileOutputStream, fileOutputStream, null);
// command
CommandLine commandline = new CommandLine(command);
commandline.addArgument(scriptFile);
if (params!=null && params.length>0) {
commandline.addArguments(params);
}
// exec
DefaultExecutor exec = new DefaultExecutor();
exec.setExitValues(null);
exec.setStreamHandler(streamHandler);
int exitValue = exec.execute(commandline); // exit code: 0=success, 1=error
return exitValue;
}
示例5: startJekyllCI
import org.apache.commons.exec.PumpStreamHandler; //导入依赖的package包/类
/**
* Starts the jekyll build process (jekyll build --incremental)
* @return true, if jekyll build was successful
*/
public boolean startJekyllCI() {
int exitValue = -1;
String line = JEKYLL_PATH;
ByteArrayOutputStream jekyllBuildOutput = new ByteArrayOutputStream();
CommandLine cmdLine = CommandLine.parse(line);
cmdLine.addArgument(JEKYLL_OPTION_BUILD);
cmdLine.addArgument(JEKYLL_OPTION_INCR);
DefaultExecutor executor = new DefaultExecutor();
executor.setWorkingDirectory(new File(LOCAL_REPO_PATH));
PumpStreamHandler streamHandler = new PumpStreamHandler(jekyllBuildOutput);
executor.setStreamHandler(streamHandler);
try {
LOGGER.info("Starting jekyll build");
exitValue = executor.execute(cmdLine);
LOGGER.info("Jekyll build command executed");
} catch (IOException e) {
LOGGER.error("Error while executing jekyll build. Error message: {}", e.getMessage());
e.printStackTrace();
return false;
}
printJekyllStatus(exitValue, jekyllBuildOutput.toString());
return true;
}
示例6: main
import org.apache.commons.exec.PumpStreamHandler; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
String cmd = "/tmp/test.sh";
CommandLine cmdLine = CommandLine.parse("/bin/bash " + cmd);
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
ByteArrayOutputStream stderr = new ByteArrayOutputStream();
PumpStreamHandler psh = new PumpStreamHandler(stdout, stderr);
psh.setStopTimeout(TIMEOUT_FIVE_MINUTES);
ExecuteWatchdog watchdog = new ExecuteWatchdog(TIMEOUT_TEN_MINUTES); // timeout in milliseconds
Executor executor = new DefaultExecutor();
executor.setExitValue(0);
executor.setStreamHandler(psh);
executor.setWatchdog(watchdog);
int exitValue = executor.execute(cmdLine, Collections.emptyMap());
System.out.println(exitValue);
}
示例7: runH2Spec
import org.apache.commons.exec.PumpStreamHandler; //导入依赖的package包/类
public static List<Failure> runH2Spec(File targetDirectory, int port, final int timeout, final int maxHeaderLength, final Set<String> excludeSpecs) throws IOException {
File reportsDirectory = new File(targetDirectory, "reports");
if (!reportsDirectory.exists()) {
reportsDirectory.mkdir();
}
File junitFile = new File(reportsDirectory, "TEST-h2spec.xml");
File h2spec = getH2SpecFile(targetDirectory);
Executor exec = new DefaultExecutor();
PumpStreamHandler psh = new PumpStreamHandler(System.out, System.err, System.in);
exec.setStreamHandler(psh);
exec.setExitValues(new int[]{0, 1});
psh.start();
if (exec.execute(buildCommandLine(h2spec, port, junitFile, timeout, maxHeaderLength)) != 0) {
return parseReports(reportsDirectory, excludeSpecs);
}
psh.stop();
return Collections.emptyList();
}
示例8: compile
import org.apache.commons.exec.PumpStreamHandler; //导入依赖的package包/类
public void compile(String cwd, Submission submission) throws RuntimeException {
ByteArrayOutputStream stderr = new ByteArrayOutputStream();
ExecuteWatchdog watchdog = new ExecuteWatchdog(watchdogTimeout);
DefaultExecutor executor = new DefaultExecutor();
executor.setWorkingDirectory(new File(cwd));
executor.setStreamHandler(new PumpStreamHandler(null, stderr, null));
executor.setWatchdog(watchdog);
CommandLine cmd = new CommandLine(javac);
cmd.addArgument("-J-Duser.language=en"); // force using English
cmd.addArgument("-classpath");
cmd.addArgument(cwd);
cmd.addArgument(fileName + ".java");
logger.info("Compiler cmd:\t" + cmd.toString());
try {
executor.execute(cmd);
logger.info("Compile OK");
} catch (IOException e) {
if (watchdog.killedProcess()) {
submission.setStatus(Submission.STATUS_CE);
submission.setError("Compile Time Exceeded");
logger.warn("Compile Time Exceeded:\t" + e.getMessage());
} else {
submission.setStatus(Submission.STATUS_CE);
submission.setError("Compile error");
logger.warn("Compile error:\t" + e.getMessage());
}
logger.warn(stderr.toString());
throw new RuntimeException("Compile Aborted.");
}
}
示例9: compile
import org.apache.commons.exec.PumpStreamHandler; //导入依赖的package包/类
public void compile(String cwd, Submission submission) throws RuntimeException {
CommandLine cmd = new CommandLine(compiler);
cmd.addArgument("-basedir=" + cwd);
cmd.addArgument("-compiler=" + realCompiler);
cmd.addArgument("-filename=" + fileName + "." + suffix);
cmd.addArgument("-timeout=" + watchdogTimeout);
cmd.addArgument("-std=" + std);
logger.info(cmd.toString());
ByteArrayOutputStream stderr = new ByteArrayOutputStream();
DefaultExecutor executor = new DefaultExecutor();
executor.setStreamHandler(new PumpStreamHandler(null, stderr, null));
try {
executor.execute(cmd);
if (stderr.toString().length() > 0) {
submission.setStatus(Submission.STATUS_CE);
submission.setError("Compile error");
logger.warn(stderr.toString());
throw new RuntimeException("Sandbox Aborted.");
}
logger.info("Compile OK");
} catch (IOException e) {
logger.warn("Compile error:\t" + e.getMessage());
throw new RuntimeException("An Error Occurred.");
}
}
示例10: exec
import org.apache.commons.exec.PumpStreamHandler; //导入依赖的package包/类
/**
* Esegue un comando di shell
* @param command comando
* @param args lista di argomenti (ogni elemento puo' contenere spazi), puo' essere null
* @param outputStream ByteArrayOutputStream che conterrà l'output del comando
* @return the error message (will be empty for a return code >0), or null if there was no error
*/
public String exec(String command, List<String> args, ByteArrayOutputStream outputStream) {
int exitValue=1;
try {
CommandLine commandLine = new CommandLine(command);
if (args!=null) {
for (String arg : args) {
commandLine.addArgument(arg, false); // Don't handle quoting
}
}
DefaultExecutor executor = new DefaultExecutor();
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
executor.setStreamHandler(streamHandler);
log.debug("Executing shell command: {}", commandLine);
exitValue = executor.execute(commandLine);
} catch (Exception e) {
log.error("Failed to execute shell command: " + command + " " + args, e);
return e.getMessage();
}
return exitValue>0?"":null;
}
示例11: handle
import org.apache.commons.exec.PumpStreamHandler; //导入依赖的package包/类
@Override
public Map<String,Object> handle (Task aTask) throws Exception {
CommandLine cmd = new CommandLine ("mediainfo");
cmd.addArgument(aTask.getRequiredString("input"));
log.debug("{}",cmd);
DefaultExecutor exec = new DefaultExecutor();
File tempFile = File.createTempFile("log", null);
try (PrintStream stream = new PrintStream(tempFile);) {
exec.setStreamHandler(new PumpStreamHandler(stream));
exec.execute(cmd);
return parse(FileUtils.readFileToString(tempFile));
}
catch (ExecuteException e) {
throw new ExecuteException(e.getMessage(),e.getExitValue(), new RuntimeException(FileUtils.readFileToString(tempFile)));
}
finally {
FileUtils.deleteQuietly(tempFile);
}
}
示例12: handle
import org.apache.commons.exec.PumpStreamHandler; //导入依赖的package包/类
@Override
public Object handle(Task aTask) throws Exception {
List<String> options = aTask.getList("options", String.class);
CommandLine cmd = new CommandLine ("ffmpeg");
options.forEach(o->cmd.addArgument(o));
log.debug("{}",cmd);
DefaultExecutor exec = new DefaultExecutor();
File tempFile = File.createTempFile("log", null);
try (PrintStream stream = new PrintStream(tempFile);) {
exec.setStreamHandler(new PumpStreamHandler(stream));
int exitValue = exec.execute(cmd);
return exitValue!=0?FileUtils.readFileToString(tempFile):cmd.toString();
}
catch (ExecuteException e) {
throw new ExecuteException(e.getMessage(),e.getExitValue(), new RuntimeException(FileUtils.readFileToString(tempFile)));
}
finally {
FileUtils.deleteQuietly(tempFile);
}
}
示例13: handle
import org.apache.commons.exec.PumpStreamHandler; //导入依赖的package包/类
@Override
public Map<String,Object> handle(Task aTask) throws Exception {
CommandLine cmd = new CommandLine ("ffprobe");
cmd.addArgument("-v")
.addArgument("quiet")
.addArgument("-print_format")
.addArgument("json")
.addArgument("-show_error")
.addArgument("-show_format")
.addArgument("-show_streams")
.addArgument(aTask.getRequiredString("input"));
log.debug("{}",cmd);
DefaultExecutor exec = new DefaultExecutor();
File tempFile = File.createTempFile("log", null);
try (PrintStream stream = new PrintStream(tempFile);) {
exec.setStreamHandler(new PumpStreamHandler(stream));
exec.execute(cmd);
return parse(FileUtils.readFileToString(tempFile));
}
catch (ExecuteException e) {
throw new ExecuteException(e.getMessage(),e.getExitValue(), new RuntimeException(FileUtils.readFileToString(tempFile)));
}
finally {
FileUtils.deleteQuietly(tempFile);
}
}
示例14: setupSudoCredentials
import org.apache.commons.exec.PumpStreamHandler; //导入依赖的package包/类
public static ExecResult setupSudoCredentials(String password) {
ExecCommand execCommand = new ExecCommand("sudo");
execCommand.addArgument("--validate", false);
execCommand.addArgument("--stdin", false);
Executor executor = new DefaultExecutor();
executor.setWorkingDirectory(execCommand.getWorkingDirectory());
executor.setExitValues(execCommand.getExitValues());
ExecOutputStream execOutputStream = new ExecOutputStream();
executor.setStreamHandler(new PumpStreamHandler(execOutputStream, execOutputStream, new ExecInputStream(password)));
try {
executor.execute(execCommand);
} catch (IOException e) {
return new ExecResult(execOutputStream.getOutput(), e);
}
return new ExecResult(execOutputStream.getOutput());
}
示例15: execCommand
import org.apache.commons.exec.PumpStreamHandler; //导入依赖的package包/类
@Deprecated
public static ExecResult execCommand(CommandLine commandLine, File workDir, int[] exitValues, boolean routeToCapture, boolean routeToStdout) {
Executor executor = new DefaultExecutor();
if (workDir != null) {
executor.setWorkingDirectory(workDir);
}
if (exitValues != null) {
executor.setExitValues(exitValues);
}
ExecOutputStream execOutputStream = new ExecOutputStream(routeToCapture, routeToStdout);
PumpStreamHandler streamHandler = new PumpStreamHandler(execOutputStream);
executor.setStreamHandler(streamHandler);
try {
executor.execute(commandLine);
} catch (IOException e) {
return new ExecResult(false, execOutputStream.getOutput(), e);
}
return new ExecResult(true, execOutputStream.getOutput(), null);
}