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:org.zanata.sync.jobs.utils.ProcessUtils.java

public static List<String> runNativeCommand(Path workingDir, long timeoutInMilli, String... commands) {
    Preconditions.checkArgument(commands != null && commands.length > 0, "You must provide commands to run");

    CommandLine commandLine = CommandLine.parse(commands[0]);
    ImmutableList<String> args = ImmutableList.copyOf(commands).subList(1, commands.length);
    for (String arg : args) {
        commandLine.addArgument(arg);/*from   w ww . j  av  a2s.  c  o  m*/
    }

    Executor executor = new DefaultExecutor();

    ImmutableList.Builder<String> output = ImmutableList.builder();
    executor.setStreamHandler(new PumpStreamHandler(new LogOutputStream() {
        @Override
        protected void processLine(String line, int logLevel) {
            log.info(line);
            output.add(line);
        }
    }));
    ExecuteWatchdog watchDog = new ExecuteWatchdog(timeoutInMilli);
    executor.setWatchdog(watchDog);
    executor.setWorkingDirectory(workingDir.toFile());
    executor.setProcessDestroyer(PROCESS_DESTROYER);

    try {
        int exitCode = executor.execute(commandLine);
        if (Execute.isFailure(exitCode) && watchDog.killedProcess()) {
            // it was killed on purpose by the watchdog
            log.error("process {} taking too long to run and killed by watchdog", commandLine);
        }
    } catch (IOException e) {
        log.error("error running:{}", commandLine);
        throw Throwables.propagate(e);
    }

    return output.build();
}

From source file:processing.app.linux.Platform.java

@Override
public Map<String, Object> resolveDeviceAttachedTo(String serial, Map<String, TargetPackage> packages,
        String devicesListOutput) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Executor executor = new ExternalProcessExecutor(baos);

    try {//from w w w . j  a  va  2  s.c o  m
        CommandLine toDevicePath = CommandLine.parse("udevadm info -q path -n " + serial);
        executor.execute(toDevicePath);
        String devicePath = new String(baos.toByteArray());
        baos.reset();
        CommandLine commandLine = CommandLine.parse("udevadm info --query=property -p " + devicePath);
        executor.execute(commandLine);
        String vidPid = new UDevAdmParser().extractVIDAndPID(new String(baos.toByteArray()));

        if (vidPid == null) {
            return super.resolveDeviceAttachedTo(serial, packages, devicesListOutput);
        }

        return super.resolveDeviceByVendorIdProductId(packages, vidPid);
    } catch (IOException e) {
        return super.resolveDeviceAttachedTo(serial, packages, devicesListOutput);
    }
}

From source file:processing.app.macosx.Platform.java

private void discoverRealOsArch() throws IOException {
    CommandLine uname = CommandLine.parse("uname -m");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Executor executor = new DefaultExecutor();
    executor.setStreamHandler(new PumpStreamHandler(baos, null));
    executor.execute(uname);/*w  w  w .j  av a 2s .c  om*/
    osArch = StringUtils.trim(new String(baos.toByteArray()));
}

From source file:processing.app.windows.Platform.java

@Override
public String preListAllCandidateDevices() {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Executor executor = new ExternalProcessExecutor(baos);

    try {/*from www.java2 s  .c  o m*/
        String listComPorts = new File(System.getProperty("user.dir"), "hardware/tools/listComPorts.exe")
                .getCanonicalPath();

        CommandLine toDevicePath = CommandLine.parse(listComPorts);
        executor.execute(toDevicePath);
        return new String(baos.toByteArray());
    } catch (Throwable e) {
        return super.preListAllCandidateDevices();
    }
}

From source file:ro.cosu.vampires.client.executors.fork.ForkExecutor.java

private CommandLine getCommandLine(String command) {
    String newCommand = command;/*  w ww .ja  v  a  2  s  .c  om*/
    if (isNumaEnabled()) {
        final String cpus = Joiner.on(",").join(cpuSet.getCpuSet());
        newCommand = "numactl --physcpubind=" + cpus + " " + command;
    }

    LOG.info("executing {} with timeout {} minutes", newCommand, TIMEOUT_IN_MILIS / 1000 / 60);
    return CommandLine.parse(newCommand);
}

From source file:ro.cosu.vampires.client.executors.fork.ForkExecutor.java

private boolean isNumaEnabled() {
    int exitCode;
    try {/* ww  w  .  ja v  a 2 s .  co  m*/
        executor.setStreamHandler(new PumpStreamHandler(new CollectingLogOutputStream()));
        exitCode = executor.execute(CommandLine.parse("numactl --hardware"));

    } catch (IOException e) {
        exitCode = -1;
    }
    return (exitCode == 0);

}

From source file:util.Utility.java

/**
 * command line execution//w  w w .j a  va  2  s.  co  m
 * 
 * @param line command line
 * @return output string of running this command line
 */
public static String exec(String line) {

    //String line = "wc -l " + "/home/hathitrust/solr/ToVM_Solr_related/test/apache-tomcat-6.0.35/bin/proxy_logs/logfile";

    CommandLine command = CommandLine.parse(line);

    DefaultExecutor executor = new DefaultExecutor();

    //int exitValue = executor.execute(command);

    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    PumpStreamHandler pump_stream_handler = new PumpStreamHandler(stdout);

    executor.setStreamHandler(pump_stream_handler);

    int exitValue = 0;
    try {
        exitValue = executor.execute(command);
    } catch (ExecuteException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    // System.out.println(exitValue);

    // System.out.println(stdout.toString());

    return stdout.toString();

}

From source file:wf.frk.tilde.sblauncher.SauerbratenExecutor.java

public void startThread(String executable, String[] params, String working_dir)
        throws ExecuteException, IOException {

    setDaemon(true);/*from   w  w w  .jav a 2s .co  m*/
    startThread();

    CommandLine cmdLine = CommandLine.parse(executable);
    for (String p : params) {
        System.out.println(p);

        cmdLine.addArgument(p);
    }

    DefaultExecutor executor = new DefaultExecutor();

    PipedOutputStream output = new PipedOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(output, System.err);
    SAUER_OUTPUT = new Scanner(new PipedInputStream(output));

    executor.setStreamHandler(streamHandler);
    executor.setWorkingDirectory(new File(working_dir));
    executor.execute(cmdLine);

}