本文整理匯總了Java中org.apache.commons.exec.Executor.setStreamHandler方法的典型用法代碼示例。如果您正苦於以下問題:Java Executor.setStreamHandler方法的具體用法?Java Executor.setStreamHandler怎麽用?Java Executor.setStreamHandler使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.commons.exec.Executor
的用法示例。
在下文中一共展示了Executor.setStreamHandler方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: call
import org.apache.commons.exec.Executor; //導入方法依賴的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: main
import org.apache.commons.exec.Executor; //導入方法依賴的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);
}
示例3: runH2Spec
import org.apache.commons.exec.Executor; //導入方法依賴的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();
}
示例4: dockerAsync
import org.apache.commons.exec.Executor; //導入方法依賴的package包/類
/**
* Runs docker command asynchronously.
*
* @param arguments command line arguments
* @param out stdout will be written in to this file
*
* @return a handle used to stop the command process
*
* @throws IOException when command has error
*/
public ExecuteWatchdog dockerAsync(final List<Object> arguments, OutputStream out) throws IOException {
CommandLine cmdLine = new CommandLine(DOCKER);
arguments.forEach((arg) -> {
cmdLine.addArgument(arg + "");
});
LOG.debug("[{} {}]", cmdLine.getExecutable(), StringUtils.join(cmdLine.getArguments(), " "));
List<String> output = new ArrayList<>();
ExecuteWatchdog watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT);
Executor executor = new DefaultExecutor();
executor.setWatchdog(watchdog);
PrintWriter writer = new PrintWriter(out);
ESH esh = new ESH(writer);
executor.setStreamHandler(esh);
executor.execute(cmdLine, new DefaultExecuteResultHandler());
return watchdog;
}
示例5: setupSudoCredentials
import org.apache.commons.exec.Executor; //導入方法依賴的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());
}
示例6: execCommand
import org.apache.commons.exec.Executor; //導入方法依賴的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);
}
示例7: executeSynch
import org.apache.commons.exec.Executor; //導入方法依賴的package包/類
/**
* Run a short living engine command where we expect a process failure as the engine invokes <code>System.exit(-1)</code>.
* @param statusCmd
* @return the output of the engine command.
*/
private String executeSynch(CommandLine statusCmd)
{
String engineOutput = null;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, System.err);
Executor executor = createEngineExecutor();
executor.setStreamHandler(streamHandler);
executor.setExitValue(-1);
try
{
executor.execute(statusCmd);
}
catch (IOException ex)
{ // expected!
}
finally
{
engineOutput = outputStream.toString();
IOUtils.closeQuietly(outputStream);
}
return engineOutput;
}
示例8: getVersion
import org.apache.commons.exec.Executor; //導入方法依賴的package包/類
public static String getVersion() {
try {
URL scriptURL = PythonCLIProbe.class.getResource(versionScriptFile);
File sf = new File(scriptURL.toURI());
String scriptPath = sf.getAbsolutePath();
CommandLine commandLine = CommandLine.parse(PythonCLIProcessor.pythonExecutable + " " + scriptPath);
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
Executor executor = new DefaultExecutor();
// put a watchdog with a timeout
ExecuteWatchdog watchdog = new ExecuteWatchdog(new Long(timeout_seconds) * 1000);
executor.setWatchdog(watchdog);
ByteArrayOutputStream os = new ByteArrayOutputStream();
PumpStreamHandler psh = new PumpStreamHandler(os);
executor.setStreamHandler(psh);
executor.execute(commandLine, resultHandler);
resultHandler.waitFor();
int exitVal = resultHandler.getExitValue();
if (exitVal != 0) {
return null;
}
return (os.toString());
}
catch (Exception e) {
return null;
}
}
示例9: runAsync
import org.apache.commons.exec.Executor; //導入方法依賴的package包/類
private CmdResultHandler runAsync(String command, CmdResultHandler resultHandler, boolean shouldClone) throws IOException, IOException, IOException, IOException {
CmdResultHandler handler = shouldClone ? resultHandler.clone() : resultHandler;
PumpStreamHandler streamHandler = handler.getStreamHandler(this);
Executor executor = new DefaultExecutor();
executor.setStreamHandler(streamHandler);
// String[] arguments = CommandLine.parse(command).toStrings();
// CommandLine cmdLine = new CommandLine(arguments[0]);
// for (int i = 1; i < arguments.length; i++) {
// cmdLine.addArgument(command, true);
// }
CommandLine cmdLine = CommandLine.parse(command);
executor.execute(cmdLine, getEnvironment(), handler.delegate);
return handler;
}
示例10: call
import org.apache.commons.exec.Executor; //導入方法依賴的package包/類
public Long call() throws Exception {
logger.debug("Started");
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 {
logger.debug("Successfully completed");
exitValue = new Long(executor.execute(commandline));
} catch (ExecuteException e) {
logger.debug("Execute exception");
exitValue = new Long(e.getExitValue());
}
if(watchDog.killedProcess()){
logger.debug("Killed by watchdog");
exitValue =WATCHDOG_EXIT_VALUE;
}
return exitValue;
}
示例11: unload
import org.apache.commons.exec.Executor; //導入方法依賴的package包/類
public int unload(String loadedInputPath) throws Exception {
String unloaderDir = platformConfig.getUnloaderPath();
commandLine = new CommandLine(Paths.get(unloaderDir).toFile());
commandLine.addArgument("--graph-name");
commandLine.addArgument(formattedGraph.getName());
commandLine.addArgument("--output-path");
commandLine.addArgument(loadedInputPath);
String commandString = StringUtils.toString(commandLine.toStrings(), " ");
LOG.info(String.format("Execute graph unloader with command-line: [%s]", commandString));
Executor executor = new DefaultExecutor();
executor.setStreamHandler(new PumpStreamHandler(System.out, System.err));
executor.setExitValue(0);
return executor.execute(commandLine);
}
示例12: getAvailableBears
import org.apache.commons.exec.Executor; //導入方法依賴的package包/類
/**
* Get details of all bears that are available on the user's system.
*
* @return JSONArray containing details of bears.
*/
public static JSONArray getAvailableBears() throws ExecuteException, IOException {
CommandLine cmdLine = new CommandLine("coala");
cmdLine.addArgument("--json");
cmdLine.addArgument("-B");
final ByteArrayOutputStream stdout = new ByteArrayOutputStream();
PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(stdout, null);
Executor executor = new DefaultExecutor();
executor.setStreamHandler(pumpStreamHandler);
executor.execute(cmdLine);
JSONArray bearList = new JSONObject(stdout.toString()).getJSONArray("bears");
return bearList;
}
示例13: docker
import org.apache.commons.exec.Executor; //導入方法依賴的package包/類
/**
* Runs docker command synchronously.
*
* @param arguments command line arguments
*
* @return command stdout
*
* @throws IOException when command has error
*/
public List<String> docker(final List<Object> arguments) throws IOException {
CommandLine cmdLine = new CommandLine(DOCKER);
arguments.forEach((arg) -> {
cmdLine.addArgument(arg + "");
});
LOG.debug("[{} {}]", cmdLine.getExecutable(), StringUtils.join(cmdLine.getArguments(), " "));
List<String> output = new ArrayList<>();
Executor executor = new DefaultExecutor();
executor.setStreamHandler(new ESH(output));
if (executor.execute(cmdLine) != 0) {
throw new IOException(cmdLine + " failed");
}
return output;
}
示例14: executeCommand
import org.apache.commons.exec.Executor; //導入方法依賴的package包/類
public static ExecResult executeCommand(ExecCommand execCommand, ExecOutputStream execOutputStream) {
Executor executor = new DefaultExecutor();
executor.setWorkingDirectory(execCommand.getWorkingDirectory());
executor.setExitValues(execCommand.getExitValues());
executor.setStreamHandler(new PumpStreamHandler(execOutputStream));
try {
executor.execute(execCommand);
} catch (IOException e) {
return new ExecResult(execOutputStream.getOutput(), e);
}
return new ExecResult(execOutputStream.getOutput());
}
示例15: executeCommandWithPipe
import org.apache.commons.exec.Executor; //導入方法依賴的package包/類
public static ExecResult executeCommandWithPipe(ExecCommand execCommand, ExecOutputStream execOutputStream, String pipeValue) {
Executor executor = new DefaultExecutor();
executor.setWorkingDirectory(execCommand.getWorkingDirectory());
executor.setExitValues(execCommand.getExitValues());
executor.setStreamHandler(new PumpStreamHandler(execOutputStream, execOutputStream, new ExecInputStream(pipeValue)));
try {
executor.execute(execCommand);
} catch (IOException e) {
return new ExecResult(execOutputStream.getOutput(), e);
}
return new ExecResult(execOutputStream.getOutput());
}