Example usage for org.apache.commons.exec CommandLine parse

List of usage examples for org.apache.commons.exec CommandLine parse

Introduction

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

Prototype

public static CommandLine parse(final String line) 

Source Link

Document

Create a command line from a string.

Usage

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

@Override
/**//from  w  ww.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.jredrain.base.utils.CommandUtils.java

public static String executeShell(File shellFile, String... args) {
    String info = null;/*from  w  w  w . ja va 2 s. com*/
    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.k42b3.aletheia.filter.request.Process.java

public void exec(Request request) {
    String cmd = getConfig().getProperty("cmd");

    try {/*from w  w w  .  java2  s  . c om*/
        logger.info("Execute: " + cmd);

        CommandLine commandLine = CommandLine.parse(cmd);
        ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
        DefaultExecutor executor = new DefaultExecutor();

        this.baos = new ByteArrayOutputStream();
        this.baosErr = new ByteArrayOutputStream();
        this.bais = new ByteArrayInputStream(request.getContent().getBytes());

        executor.setStreamHandler(new PumpStreamHandler(this.baos, this.baosErr, this.bais));
        executor.setWatchdog(watchdog);
        executor.execute(commandLine);

        logger.info("Output: " + this.baos.toString());

        request.setContent(this.baos.toString());
    } catch (Exception e) {
        logger.warning(e.getMessage());
    }
}

From source file:cat.ogasoft.protocolizer.processor.CompilerPhase.java

public static void processCompiler(RoundEnvironment roundEnv) throws CompilerException {
    try {/*from w w  w  . j  av  a 2  s.c o  m*/
        String protocPath = "src" + File.separatorChar + "cat" + File.separatorChar + "ogasoft"
                + File.separatorChar + "protocolizer" + File.separatorChar + "protoc";

        DefaultExecutor de = new DefaultExecutor();
        for (Element element : roundEnv.getElementsAnnotatedWith(ProtoFileV2.Compiler.class)) {
            ProtoFileV2.Compiler compiler = element.getAnnotation(ProtoFileV2.Compiler.class);
            if (compiler.compile()) {
                String base = protocPath.replace('.', File.separatorChar);
                File dst = new File(base);
                if (!dst.exists() && !dst.mkdirs()) {
                    throw new Exception("Cann not be created directory " + dst.getAbsolutePath());
                }
                File rootDirectori = new File(base);
                //Compile all the files in compiler.protoFilePaths()
                FileFilter filter = new FileFilter() {
                    @Override
                    public boolean accept(File pathname) {
                        return pathname.getName().toLowerCase().endsWith(".proto");
                    }
                };
                for (File protoc : rootDirectori.listFiles(filter)) {
                    String target = base + File.separatorChar + protoc.getName();
                    LOG.info("\tCompiling " + target + "...");
                    CommandLine cmd = CommandLine.parse(compiler.command() + " --proto_path=" + protocPath + " "
                            + compiler.language().option + "=src " + target);
                    int result = de.execute(cmd);
                    if (result != 0) {
                        throw new CompilerException("HO ho... somthing went wrong, code: " + result);
                    }
                    LOG.info("\t" + target + " compiled");
                }
            }
        }
    } catch (Exception e) {
        //Any exception is a CompilerException.
        throw new CompilerException(e.getMessage());
    }
}

From source file:com.technofovea.packbsp.WindowsRegistryChecker.java

public WindowsRegistryChecker() {
    executor.setExitValue(SUCCESSFUL_EXIT);
    watchdog = new ExecuteWatchdog(WATCHDOG_TIME);
    executor.setWatchdog(watchdog);//from w w  w  . j  a  v  a 2s.c  o m
    environment = new HashMap<String, String>(System.getenv());
    cmd = CommandLine.parse("reg.exe");
    cmd.addArgument("query");
    cmd.addArgument("${" + KEY_PATH + "}");
    cmd.addArgument("/v");
    cmd.addArgument("${" + KEY_VALUE_NAME + "}");
}

From source file:com.zxy.commons.exec.CmdExecutor.java

/**
 * //from   w w  w.j  av a 2 s . c  om
 * 
 * @param workHome workHome
 * @param command command
 * @return ???
 * @throws InterruptedException InterruptedException
 * @throws IOException IOException
 */
public static ExecutorResult exec(String workHome, String command) throws InterruptedException, IOException {
    CommandLine cmdLine = CommandLine.parse(PRE_CMD + command);
    Executor executor = new DefaultExecutor();
    executor.setWorkingDirectory(new File(workHome));

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, errorStream);
    executor.setStreamHandler(streamHandler);

    int code = executor.execute(cmdLine, EnvironmentUtils.getProcEnvironment());
    String successMsg = outputStream.toString(ENCODING);
    String errorMsg = errorStream.toString(ENCODING);
    return new ExecutorResult(code, successMsg, errorMsg);
}

From source file:com.dangdang.ddframe.job.executor.type.ScriptJobExecutor.java

private void executeScript(final ShardingContext shardingContext, final String scriptCommandLine) {
    CommandLine commandLine = CommandLine.parse(scriptCommandLine);
    commandLine.addArgument(GsonFactory.getGson().toJson(shardingContext), false);
    try {//from w w w.  ja  va2  s.  co  m
        new DefaultExecutor().execute(commandLine);
    } catch (final IOException ex) {
        throw new JobConfigurationException("Execute script failure.", ex);
    }
}

From source file:com.stratio.explorer.shell.ShellInterpreter.java

@Override
public InterpreterResult interpret(String cmd) {
    logger.info("Run shell command '" + cmd + "'");
    long start = System.currentTimeMillis();
    CommandLine cmdLine = CommandLine.parse("bash");
    cmdLine.addArgument("-c", false);
    cmdLine.addArgument(cmd, false);/*from   ww w  .j av a2  s .  c o  m*/
    DefaultExecutor executor = new DefaultExecutor();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    executor.setStreamHandler(new PumpStreamHandler(outputStream));

    executor.setWatchdog(new ExecuteWatchdog(CMD_TIMEOUT));
    try {
        int exitValue = executor.execute(cmdLine);
        return new InterpreterResult(InterpreterResult.Code.SUCCESS, outputStream.toString());
    } catch (ExecuteException e) {
        logger.error("Can not run " + cmd, e);
        return new InterpreterResult(Code.ERROR, e.getMessage());
    } catch (IOException e) {
        logger.error("Can not run " + cmd, e);
        return new InterpreterResult(Code.ERROR, e.getMessage());
    }
}

From source file:com.k42b3.sacmis.ExecutorAbstract.java

public void run() {
    try {//  w w  w . j a v  a 2s . c om
        // clear text
        this.textArea.setText("");

        CommandLine commandLine = CommandLine.parse(this.getExecutable() + " " + this.cmd);
        ExecuteWatchdog watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT);

        // create executor
        DefaultExecutor executor = new DefaultExecutor();

        executor.setStreamHandler(new PumpStreamHandler(new TextAreaOutputStream(textArea)));
        executor.setWatchdog(watchdog);
        executor.execute(commandLine);
    } catch (FoundNoExecutableException e) {
        JOptionPane.showMessageDialog(null, e.getMessage(), "Information", JOptionPane.ERROR_MESSAGE);
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    }
}

From source file:com.github.rwhogg.git_vcr.review.ProcessBasedReviewTool.java

/**
 * Runs the command string specified by commandLine, then returns the output.
 * Note that any command-line arguments from the configuration are automatically added.
 * @param commands The rest of the command line, excluding any arguments
 * @return The output of the command/*from  w ww.ja va 2  s . c  o  m*/
 */
protected List<String> runProcess(String commands) throws ExecuteException, IOException {
    DefaultExecutor executor = new DefaultExecutor();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
    executor.setStreamHandler(streamHandler);
    executor.setExitValues(null);
    String arguments = configuration.getString("args", "");
    executor.execute(CommandLine.parse(getExecutableName() + " " + arguments + " " + commands));
    return Arrays.asList(outputStream.toString("UTF-8").split("\n"));
}