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:net.ishchenko.idea.nginx.platform.NginxCompileParametersExtractor.java

/**
 * Runs file with -V argument and matches the output against OUTPUT_PATTERN.
 * @param from executable to be run with -V command line argument
 * @return Parameters parsed out from process output
 * @throws PlatformDependentTools.ThisIsNotNginxExecutableException if file could not be run, or
 * output would not match against expected pattern
 *//*from   w  ww.ja v  a2 s  .  c  o m*/
public static NginxCompileParameters extract(VirtualFile from)
        throws PlatformDependentTools.ThisIsNotNginxExecutableException {

    NginxCompileParameters result = new NginxCompileParameters();

    Executor executor = new DefaultExecutor();
    ByteArrayOutputStream os = new ByteArrayOutputStream();

    try {
        executor.setStreamHandler(new PumpStreamHandler(os, os));
        executor.execute(CommandLine.parse(from.getPath() + " -V"));
    } catch (IOException e) {
        throw new PlatformDependentTools.ThisIsNotNginxExecutableException(e);
    }

    String output = os.toString();
    Matcher versionMatcher = Pattern.compile("nginx version: nginx/([\\d\\.]+)").matcher(output);
    Matcher configureArgumentsMatcher = Pattern.compile("configure arguments: (.*)").matcher(output);

    if (versionMatcher.find() && configureArgumentsMatcher.find()) {

        String version = versionMatcher.group(1);
        String params = configureArgumentsMatcher.group(1);

        result.setVersion(version);

        Iterable<String> namevalues = StringUtil.split(params, " ");
        for (String namevalue : namevalues) {
            int eqPosition = namevalue.indexOf('=');
            if (eqPosition == -1) {
                handleFlag(result, namevalue);
            } else {
                handleNameValue(result, namevalue.substring(0, eqPosition),
                        namevalue.substring(eqPosition + 1));
            }
        }

    } else {
        throw new PlatformDependentTools.ThisIsNotNginxExecutableException(
                NginxBundle.message("run.configuration.outputwontmatch"));
    }

    return result;

}

From source file:com.taobao.ad.es.common.job.executor.ShellHttpJobExecutor.java

@Override
public JobResult execute(JobData jobData) throws IOException {
    JobResult jobResult = JobResult.succcessResult();
    CommandLine cmdLine = CommandLine.parse(jobData.getData().get(JobData.JOBDATA_DATA_JOBCOMMAND));
    Executor executor = new DefaultExecutor();
    ExecuteWatchdog watchdog = new ExecuteWatchdog(1200000);
    executor.setExitValue(0);/*from  w ww .j  ava2s. c  o m*/
    executor.setWatchdog(watchdog);
    int exitValue = -1;
    try {
        exitValue = executor.execute(cmdLine);
    } catch (ExecuteException e) {
        exitValue = e.getExitValue();
    }
    if (exitValue != 0) {
        jobResult = JobResult.errorResult(JobResult.RESULTCODE_OTHER_ERR,
                "Shell?");
        jobResult.setResultCode(exitValue);
        return jobResult;
    }
    jobResult.setResultCode(exitValue);
    return jobResult;
}

From source file:io.manasobi.utils.CmdUtils.java

/**
 *  ?  ?? ?  ? ?//from ww  w. java2 s . c  om
 * 
 * @param line        
 * @param argument  ?
 * @return int ? 
 */
public static int execute(String line, String argument) {

    CommandLine commandLine = CommandLine.parse(line);

    return execute(commandLine, argument);
}

From source file:net.gageot.test.utils.Shell.java

/**
 * Execute an external command.//from w ww . j  a  v a2 s.com
 *
 * @return exitCode or -1 if an exception occured
 */
public int execute(String command, Object... arguments) {
    DefaultExecutor executor = new DefaultExecutor();

    CommandLine commandLine = CommandLine.parse(format(command, arguments));

    try {
        return executor.execute(commandLine);
    } catch (IOException e) {
        return -1;
    }
}

From source file:io.github.binout.wordpress2html.writer.Html2AsciidocConverter.java

public File convert(File html) throws IOException {
    String baseName = FilenameUtils.getBaseName(html.getName());
    File asciidoc = new File(html.getParentFile(), baseName + ".adoc");
    CommandLine cmdLine = CommandLine.parse("pandoc --no-wrap -f html -t asciidoc " + html.getAbsolutePath()
            + " -o " + asciidoc.getAbsolutePath());
    execute(cmdLine);//from w ww  . java  2  s  .c  om
    return asciidoc;
}

From source file:com.stratio.ingestion.utils.IngestionUtils.java

public static DefaultExecuteResultHandler executeBash(String command) throws IOException {
    CommandLine cmdLine = CommandLine.parse("bash");
    cmdLine.addArgument("-c", false);
    cmdLine.addArgument(command, false);

    DefaultExecutor executor = new DefaultExecutor();
    ByteArrayOutputStream outputStreamAgentStart = new ByteArrayOutputStream();
    executor.setStreamHandler(new PumpStreamHandler(outputStreamAgentStart));
    DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler();
    executor.execute(cmdLine, handler);//w w w  .  j  ava 2s. c  o  m

    return handler;
}

From source file:com.codeabovelab.dm.common.utils.ProcessUtils.java

public static int executeCommand(String command, ExecuteWatchdog watchdog, OutputStream outputStream,
        OutputStream errorStream, InputStream inputStream, Map<String, String> env) {
    CommandLine cmdLine = CommandLine.parse(command);
    DefaultExecutor executor = new DefaultExecutor();
    if (outputStream == null) {
        outputStream = new LogOutputStream() {
            @Override//from  w  w  w  .  ja v  a 2  s  .  c o m
            protected void processLine(String s, int i) {
                log.error(s);
            }
        };
    }
    if (errorStream == null) {
        errorStream = new LogOutputStream() {
            @Override
            protected void processLine(String s, int i) {
                log.error(s);
            }
        };
    }
    executor.setStreamHandler(new PumpStreamHandler(outputStream, errorStream, inputStream));
    executor.setExitValues(new int[] { 0, 1 });
    if (watchdog != null) {
        executor.setWatchdog(watchdog);
    }
    int exitValue;
    try {
        exitValue = executor.execute(cmdLine, env);
    } catch (IOException e) {
        exitValue = 1;
        LOGGER.error("error executing command", e);
    }
    return exitValue;
}

From source file:com.technofovea.packbsp.packaging.BspZipController.java

public BspZipController(File exe) {
    super();//www. ja  va 2s  .  c  o  m
    executor.setExitValues(new int[] { 0, 1 });
    // Quotes are necessary or it complains when it hits spaces
    cmd = CommandLine.parse("\"" + exe.getAbsolutePath() + "\"");

    // Does -game even work with bspzip? Try VProject env variable instead.
    /*
    cmd.addArgument("-game");
    cmd.addArgument("${" + KEY_GAME_DIR +"}");
     */
    cmd.addArgument("-addlist");
    cmd.addArgument("${" + KEY_SRC_BSP + "}");
    cmd.addArgument("${" + KEY_FILELIST + "}");
    cmd.addArgument("${" + KEY_DST_BSP + "}");

}

From source file:com.dangdang.ddframe.job.plugin.job.type.integrated.ScriptElasticJob.java

@Override
protected void executeJob(final JobExecutionMultipleShardingContext shardingContext) {
    String scriptCommandLine = getJobFacade().getScriptCommandLine();
    Preconditions.checkArgument(!Strings.isNullOrEmpty(scriptCommandLine), "Cannot find script command line.");
    CommandLine cmdLine = CommandLine.parse(scriptCommandLine);
    cmdLine.addArgument(shardingContext.toScriptArguments(), false);
    DefaultExecutor executor = new DefaultExecutor();
    try {//from   w ww .j ava  2s  . c  om
        executor.execute(cmdLine);
    } catch (final IOException ex) {
        throw new JobException(ex);
    }
}

From source file:io.rhiot.utils.process.ExecProcessManager.java

@Override
public List<String> executeAndJoinOutput(String... command) {

    CommandLine cmdLine = CommandLine.parse(String.join(" ", command));
    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(0);/*from   w w w. jav a2s . c  o m*/
    ExecResultHandler resultHandler = null;

    if (getTimeout() > 0) {
        ExecuteWatchdog watchdog = new ExecuteWatchdog(getTimeout());
        executor.setWatchdog(watchdog);
        resultHandler = new ExecResultHandler(watchdog);
    }
    try {
        CollectingLogOutputStream outAndErr = new CollectingLogOutputStream();
        executor.setStreamHandler(new PumpStreamHandler(outAndErr));
        if (resultHandler != null) {
            executor.execute(cmdLine, resultHandler);
        } else {
            executor.execute(cmdLine);
        }
        resultHandler.waitFor();
        return outAndErr.getLines();

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}