本文整理汇总了Java中org.apache.commons.exec.ExecuteResultHandler类的典型用法代码示例。如果您正苦于以下问题:Java ExecuteResultHandler类的具体用法?Java ExecuteResultHandler怎么用?Java ExecuteResultHandler使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ExecuteResultHandler类属于org.apache.commons.exec包,在下文中一共展示了ExecuteResultHandler类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: asynchExecutionHandler
import org.apache.commons.exec.ExecuteResultHandler; //导入依赖的package包/类
private ExecuteResultHandler asynchExecutionHandler()
{
return new ExecuteResultHandler()
{
@Override
public void onProcessFailed(ExecuteException ex)
{
throw new RuntimeException("Engine operation failed.", ex);
}
@Override
public void onProcessComplete(int exitValue)
{
context.log.info("Engine process stopped.");
}
};
}
示例2: execAsync
import org.apache.commons.exec.ExecuteResultHandler; //导入依赖的package包/类
@SuppressWarnings("rawtypes")
public static void execAsync(String display, CommandLine commandline)
throws ShellCommandException {
log.debug("executing async command: " + commandline);
DefaultExecutor exec = new DefaultExecutor();
ExecuteResultHandler handler = new DefaultExecuteResultHandler();
PumpStreamHandler streamHandler = new PumpStreamHandler(
new PritingLogOutputStream());
exec.setStreamHandler(streamHandler);
try {
if (display == null || display.isEmpty()) {
exec.execute(commandline, handler);
} else {
Map env = EnvironmentUtils.getProcEnvironment();
EnvironmentUtils.addVariableToEnvironment(env, "DISPLAY=:"
+ display);
exec.execute(commandline, env, handler);
}
} catch (Exception e) {
throw new ShellCommandException(
"An error occured while executing shell command: "
+ commandline, e);
}
}
示例3: launchContrailServer
import org.apache.commons.exec.ExecuteResultHandler; //导入依赖的package包/类
public static void launchContrailServer(int port) throws Exception {
try {
DefaultExecutor exec = new DefaultExecutor();
int exitValues[] = {1};
exec.setExitValues(exitValues);
String workingDir = System.getProperty("user.dir");
String path = workingDir + "/../../config/api-server/tests/";
File f = new File(path);
exec.setWorkingDirectory(f);
exec.setStreamHandler(new PumpStreamHandler(new ByteArrayOutputStream()));
CommandLine cmd = buildServerLaunchCmd(port);
ExecuteResultHandler handler = null;
exec.execute(cmd, handler);
/* sleep 5 seconds for server to get started */
Thread.sleep(5000);
} catch (Exception e) {
s_logger.debug(e);
String cause = e.getMessage();
if (cause.equals("python: not found"))
System.out.println("No python interpreter found.");
throw e;
}
}
示例4: runCallsExecuteWithCorrectCommand
import org.apache.commons.exec.ExecuteResultHandler; //导入依赖的package包/类
@Test
public void runCallsExecuteWithCorrectCommand() throws Exception {
// Arrange
Executor mockExecutor = mock(Executor.class);
File executable = testFolder.newFile("file1");
String[] executionArguments = new String[]{"arg1", "arg2", "${arg3}"};
File script = testFolder.newFile("file2");
HashMap<String, File> fileArguments = new HashMap<>();
fileArguments.put("arg3", script);
ProcessRunner target = new CommonsExecProcessRunner(mockExecutor, testFolder.getRoot(), executable,
executionArguments, fileArguments, 10);
String expectation = "[" + executable + ", arg1, arg2, " + script + "]";
// Act
target.run(mock(ProcessHandler.class));
// Assert
ArgumentCaptor<CommandLine> commandLineCaptor = captorForClass(CommandLine.class);
verify(mockExecutor).execute(commandLineCaptor.capture(), any(ExecuteResultHandler.class));
assertThat(commandLineCaptor.getValue().toString()).isEqualTo(expectation);
}
示例5: runWrapsInnerExceptionInErrorCase
import org.apache.commons.exec.ExecuteResultHandler; //导入依赖的package包/类
@Test
public void runWrapsInnerExceptionInErrorCase() throws Exception {
// Arrange
Executor mockExecutor = mock(Executor.class);
ExecuteException expectedCause = new ExecuteException("foo", -1);
doThrow(expectedCause).when(mockExecutor).execute(any(CommandLine.class), any(ExecuteResultHandler.class));
ProcessRunner target = new CommonsExecProcessRunner(mockExecutor, testFolder.getRoot(), new File("exe"),
new String[0], new HashMap<String, File>(), 10);
// Act
catchException(target).run(mock(ProcessHandler.class));
Exception result = caughtException();
// Assert
assertThat(result).isInstanceOf(ProcessException.class);
assertThat(result.getCause()).isEqualTo(expectedCause);
}
示例6: execASync
import org.apache.commons.exec.ExecuteResultHandler; //导入依赖的package包/类
public static ExecuteWatchdog execASync(String line, File workDir, Map<String, String> environment, OutputStream output, ExecuteResultHandler erh,
long timeout) throws IOException {
if (environment != null) {
Map<String, String> sysenv = System.getenv();
for (String key : sysenv.keySet()) {
boolean contains = false;
for (String k : environment.keySet()) {
if (k.equalsIgnoreCase(key)) {
contains = true;
break;
}
}
if (!contains)
environment.put(key, sysenv.get(key));
}
}
DefaultExecutor executor = new DefaultExecutor();
if (workDir != null)
executor.setWorkingDirectory(workDir);
PumpStreamHandler sh = new PumpStreamHandler(output, output, null);
executor.setStreamHandler(sh);
ExecuteWatchdog watchdog = new ProcessTreeWatchDog(timeout);
executor.setWatchdog(watchdog);
executor.execute(CommandLine.parse(line), environment, erh);
return watchdog;
}
示例7: main
import org.apache.commons.exec.ExecuteResultHandler; //导入依赖的package包/类
public static void main(String[] args) throws ExecuteException, IOException, InterruptedException
{
CommandLine command = new CommandLine("/bin/bash");
command.addArgument("-c", false);
command.addArgument("iperf3 -t 30 -c iperf.scottlinux.com >> output.txt", false);
//Process process = Runtime.getRuntime().exec(new String[]{"bash", "-c", "iperf3 -t 60 -c localhost"});
// System.out.println(new Mirror().on(process).get().field("pid"));
//process.waitFor();
// System.out.println(process.exitValue());
// ManagementFactory.getRuntimeMXBean().getName();
// System.out.println(IOUtils.readLines(process.getInputStream()));
//String command = "iperf3 -t 30 -c iperf.scottlinux.com";
ExecuteWatchdog watchdog = new ExecuteWatchdog(10);
final DefaultExecutor executor = new DefaultExecutor();
executor.setStreamHandler(new PumpStreamHandler());
executor.setExitValue(1);
executor.execute(command, new ExecuteResultHandler()
{
@Override
public void onProcessFailed(ExecuteException e)
{
e.printStackTrace();
}
@Override
public void onProcessComplete(int exitValue)
{
System.out.println(exitValue);
}
});
}
示例8: execute
import org.apache.commons.exec.ExecuteResultHandler; //导入依赖的package包/类
@Override
public void execute(CommandLine command) throws ShellException, IOException
{
// ProcessBuilder pb = new ProcessBuilder("bash", "c", instruction);
// Process start = pb.start();
//
// try
// {
// int exitValue = start.waitFor();
// }
// catch (InterruptedException exception)
// {
// throw new ShellException();
// }
final DefaultExecutor executor = new DefaultExecutor();
executor.execute(command, new ExecuteResultHandler()
{
@Override
public void onProcessFailed(ExecuteException e)
{
}
@Override
public void onProcessComplete(int exitValue)
{
}
});
}
示例9: execute
import org.apache.commons.exec.ExecuteResultHandler; //导入依赖的package包/类
public void execute(final Application application, ExecuteResultHandler executeResultHandler, ExecuteStreamHandler streamHandler) throws ExecuteException, IOException
{
final String commandLine = application.getCommandLine();
DefaultExecutor executor = new DefaultExecutor();
CommandLine command = new CommandLine("/bin/sh");
command.addArgument("-c", false);
command.addArgument(commandLine, false);
executor.setStreamHandler(streamHandler);
executor.execute(command, System.getenv(), executeResultHandler);
LOG.debug("Launched the execution of task: [{}], uuid: [{}]", commandLine, application.getId());
}
示例10: launchProcess
import org.apache.commons.exec.ExecuteResultHandler; //导入依赖的package包/类
/**
* If it is backend, please set resultHandler, such as DefaultExecuteResultHandler
* If it is frontend, ByteArrayOutputStream.toString will return the calling result
* <p>
* This function will ignore whether the command is successfully executed or not
*
* @param command command to be executed
* @param environment env vars
* @param workDir working directory
* @param resultHandler exec result handler
* @return output stream
* @throws IOException
*/
@Deprecated
public static ByteArrayOutputStream launchProcess(
String command, final Map environment, final String workDir,
ExecuteResultHandler resultHandler) throws IOException {
String[] cmdlist = command.split(" ");
CommandLine cmd = new CommandLine(cmdlist[0]);
for (String cmdItem : cmdlist) {
if (!StringUtils.isBlank(cmdItem)) {
cmd.addArgument(cmdItem);
}
}
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(0);
if (!StringUtils.isBlank(workDir)) {
executor.setWorkingDirectory(new File(workDir));
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
PumpStreamHandler streamHandler = new PumpStreamHandler(out, out);
executor.setStreamHandler(streamHandler);
try {
if (resultHandler == null) {
executor.execute(cmd, environment);
} else {
executor.execute(cmd, environment, resultHandler);
}
} catch (ExecuteException ignored) {
}
return out;
}
示例11: launchProcess
import org.apache.commons.exec.ExecuteResultHandler; //导入依赖的package包/类
/**
* If it is backend, please set resultHandler, such as DefaultExecuteResultHandler
* If it is frontend, ByteArrayOutputStream.toString get the result
*
* This function don't care whether the command is successfully or not
*
* @param command
* @param environment
* @param workDir
* @param resultHandler
* @return
* @throws IOException
*/
public static ByteArrayOutputStream launchProcess(String command, final Map environment,
final String workDir, ExecuteResultHandler resultHandler)
throws IOException {
String[] cmdlist = command.split(" ");
CommandLine cmd = new CommandLine(cmdlist[0]);
for (String cmdItem : cmdlist) {
if (StringUtils.isBlank(cmdItem) == false) {
cmd.addArgument(cmdItem);
}
}
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(0);
if (StringUtils.isBlank(workDir) == false) {
executor.setWorkingDirectory(new File(workDir));
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
PumpStreamHandler streamHandler = new PumpStreamHandler(out, out);
if (streamHandler != null) {
executor.setStreamHandler(streamHandler);
}
try {
if (resultHandler == null) {
executor.execute(cmd, environment);
} else {
executor.execute(cmd, environment, resultHandler);
}
}catch(ExecuteException e) {
// @@@@
// failed to run command
}
return out;
}
示例12: launchProcess
import org.apache.commons.exec.ExecuteResultHandler; //导入依赖的package包/类
/**
* If it is backend, please set resultHandler, such as DefaultExecuteResultHandler If it is frontend, ByteArrayOutputStream.toString get the result
* <p/>
* This function don't care whether the command is successfully or not
*
* @param command
* @param environment
* @param workDir
* @param resultHandler
* @return
* @throws IOException
*/
public static ByteArrayOutputStream launchProcess(String command, final Map environment, final String workDir, ExecuteResultHandler resultHandler)
throws IOException {
String[] cmdlist = command.split(" ");
CommandLine cmd = new CommandLine(cmdlist[0]);
for (String cmdItem : cmdlist) {
if (StringUtils.isBlank(cmdItem) == false) {
cmd.addArgument(cmdItem);
}
}
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(0);
if (StringUtils.isBlank(workDir) == false) {
executor.setWorkingDirectory(new File(workDir));
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
PumpStreamHandler streamHandler = new PumpStreamHandler(out, out);
if (streamHandler != null) {
executor.setStreamHandler(streamHandler);
}
try {
if (resultHandler == null) {
executor.execute(cmd, environment);
} else {
executor.execute(cmd, environment, resultHandler);
}
} catch (ExecuteException e) {
// @@@@
// failed to run command
}
return out;
}