Example usage for org.apache.commons.exec DefaultExecutor DefaultExecutor

List of usage examples for org.apache.commons.exec DefaultExecutor DefaultExecutor

Introduction

In this page you can find the example usage for org.apache.commons.exec DefaultExecutor DefaultExecutor.

Prototype

public DefaultExecutor() 

Source Link

Document

Default constructor creating a default PumpStreamHandler and sets the working directory of the subprocess to the current working directory.

Usage

From source file:hu.bme.mit.trainbenchmark.sql.process.MySqlProcess.java

public static void runScript(final String scriptFile) throws ExecuteException, IOException {
    final Executor executor = new DefaultExecutor();
    final CommandLine commandLine = new CommandLine("/bin/bash");
    commandLine.addArgument(scriptFile, false);
    executor.execute(commandLine);//from ww w.  j a  va 2s  . c  o  m
}

From source file:com.rest4j.generator.PythonGeneratorTest.java

@Test
public void testPythonClient() throws Exception {
    gen.setStylesheet("com/rest4j/client/python.xslt");
    new File("target/python").mkdir();
    gen.setApiXmlUrl(getClass().getResource("doc-generator-graph.xml"));
    gen.setOutputDir("target/python");
    gen.addParam(new TemplateParam("common-params", "access-token"));
    gen.addParam(new TemplateParam("additional-client-code", "\t# ADDITIONAL CODE"));
    gen.generate();//from w  ww.ja  va 2 s .c  o  m

    // let's try compiling the damn thing
    CommandLine cmdLine = CommandLine.parse("python apiclient.py");
    DefaultExecutor executor = new DefaultExecutor();
    executor.setWorkingDirectory(new File("target/python"));
    ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
    executor.setWatchdog(watchdog);
    int exitValue = executor.execute(cmdLine);
    assertFalse(executor.isFailure(exitValue));

    String a = IOUtils.toString(new File("target/python/apiclient.py").toURI());

    // check doc comments
    assertTrue(a, a.contains("Some additional client info"));
    assertTrue(a, a.contains("Some additional python client info"));

    // check existence of Parameter Object class
    assertTrue(a, a.contains("class PatchBRequest"));

    // check additional-client-code
    assertTrue(a, a.contains("\t# ADDITIONAL CODE"));
}

From source file:io.werval.maven.MavenDevShellSPI.java

@Override
protected void doRebuild() throws DevShellRebuildException {
    System.out.println("Reload!");
    DefaultExecutor executor = new DefaultExecutor();
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    executor.setStreamHandler(new PumpStreamHandler(output));
    try {/*from ww  w.ja v a  2s  .  c  om*/
        int exitValue = executor.execute(cmdLine);
        if (exitValue != 0) {
            throw new DevShellRebuildException(
                    new MavenExecutionException("Maven exited with a non-zero status: " + exitValue, pom));
        }
    } catch (Exception ex) {
        throw new DevShellRebuildException(ex, output.toString());
    }
}

From source file:it.unisi.bdm.crawler.PhantomjsBrowser.java

@Override
/**/*from w w  w. j  a  v a 2  s  . c  om*/
 * Invokes PhantomJS in order to download the page associated to `url`.
 * Please note that since the browser sometimes hangs, it's execution is
 * monitored by a wathdog that kills it after this.timeout ms.
 * 
 * @param url
 * @return Page
 * @throws BrowserTimeoutException If PhantomJS hangs.
 */
public Page getPage(String url) throws BrowserTimeoutException {
    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    PumpStreamHandler psh = new PumpStreamHandler(stdout);

    String line = "library/phantomjs library/crawler.js " + url;
    CommandLine cmdLine = CommandLine.parse(line);
    DefaultExecutor executor = new DefaultExecutor();
    ExecuteWatchdog watchdog = new ExecuteWatchdog(this.timeout);
    executor.setWatchdog(watchdog);
    executor.setStreamHandler(psh);

    try {
        executor.execute(cmdLine);
    } catch (Exception e) {
        throw new BrowserTimeoutException();
    }

    String json = stdout.toString();
    return new Gson().fromJson(json, Page.class);
}

From source file:com.dubture.composer.core.launch.DefaultExecutableLauncher.java

public DefaultExecutableLauncher() {

    outputStream = new ByteArrayOutputStream();
    errStream = new ByteArrayOutputStream();
    streamHandler = new PumpStreamHandler(outputStream, errStream);
    executor = new DefaultExecutor();
    executor.setStreamHandler(streamHandler);
    watchdog = new ExecuteWatchdog(TIMEOUT);
    executor.setWatchdog(watchdog);//  w w  w  .j  av a2  s.co m

}

From source file:adb4j.executor.CmdExecutor.java

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);
    //        }/*from   w ww  .j a v  a2 s.co  m*/
    CommandLine cmdLine = CommandLine.parse(command);
    executor.execute(cmdLine, getEnvironment(), handler.delegate);

    return handler;
}

From source file:com.jredrain.base.utils.CommandUtils.java

public static String executeShell(File shellFile, String... args) {
    String info = null;/*from   w w  w  .  ja  v a2s. co m*/
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {

        String params = " ";
        if (CommonUtils.notEmpty(args)) {
            for (String p : args) {
                params += p + " ";
            }
        }

        CommandLine commandLine = CommandLine.parse("/bin/bash +x " + shellFile.getAbsolutePath() + params);
        DefaultExecutor exec = new DefaultExecutor();
        exec.setExitValues(null);
        PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, outputStream);
        exec.setStreamHandler(streamHandler);

        exec.execute(commandLine);
        info = outputStream.toString().trim();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return info;
    }
}

From source file:com.nts.alphamale.shell.AdbShellExecutor.java

/**
 * @param cmd adb /* www  . j  a va  2 s  . c o m*/
 * @param synch   ? ? true: synchronous, false: asynchronous
 * @param  (ms)
 * @return Executor Standard Output? Map
 */
public Map<String, Object> execute(CommandLine cmd, boolean synch, long timeoutMilliseconds) {
    Map<String, Object> executorMap = new HashMap<String, Object>();
    DefaultExecutor executor = new DefaultExecutor();
    PipedOutputStream pos = new PipedOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(pos, System.err);
    streamHandler.setStopTimeout(timeoutMilliseconds);
    DataInputStream dis = null;
    try {
        dis = new DataInputStream(new PipedInputStream(pos));
    } catch (IOException e) {
        //log.error(e.getCause() + " : " + e.getMessage());
    }
    executor.setStreamHandler(streamHandler);
    ExecuteWatchdog watchDog = new ExecuteWatchdog(timeoutMilliseconds);
    executor.setWatchdog(watchDog);
    try {
        if (synch)
            executor.execute(cmd);
        else
            executor.execute(cmd, new DefaultExecuteResultHandler());

        log.debug(cmd.toString());
        LineIterator li = IOUtils.lineIterator(dis, "UTF-8");
        if (li != null) {
            executorMap.put("executor", executor);
            executorMap.put("stdOut", li);
        }
    } catch (Exception e) {
        log.error(e.getCause() + ":" + e.getMessage() + "[" + cmd + "]");
    }
    return executorMap;
}

From source file:io.selendroid.standalone.io.ShellCommand.java

public static void execAsync(String display, CommandLine commandline) throws ShellCommandException {
    log.info("executing async command: " + commandline);
    DefaultExecutor exec = new DefaultExecutor();

    ExecuteResultHandler handler = new DefaultExecuteResultHandler();
    PumpStreamHandler streamHandler = new PumpStreamHandler(new PrintingLogOutputStream());
    exec.setStreamHandler(streamHandler);
    try {// www . j a  v  a  2  s.co m
        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) {
        log.log(Level.SEVERE, "Error executing command: " + commandline, e);
        throw new ShellCommandException("Error executing shell command: " + commandline, e);
    }
}

From source file:com.creactiviti.piper.plugin.ffmpeg.Ffprobe.java

@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);//www. ja  v a  2  s . c  o m
        return parse(FileUtils.readFileToString(tempFile));
    } catch (ExecuteException e) {
        throw new ExecuteException(e.getMessage(), e.getExitValue(),
                new RuntimeException(FileUtils.readFileToString(tempFile)));
    } finally {
        FileUtils.deleteQuietly(tempFile);
    }
}