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

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

Introduction

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

Prototype

public CommandLine addArgument(final String argument) 

Source Link

Document

Add a single argument.

Usage

From source file:com.tupilabs.pbs.PBS.java

/**
 * PBS qnodes command.//from w  ww  .  j  a  v  a2  s  . c om
 * <p>
 * Get information about the cluster nodes.
 *
 * @param name node name
 * @return list of nodes
 * @throws PBSException if an error communicating with the PBS occurs
 */
public static List<Node> qnodes(String name) {
    final List<Node> nodes;

    final CommandLine cmdLine = new CommandLine(COMMAND_QNODES);
    cmdLine.addArgument(PARAMETER_XML);
    if (StringUtils.isNotBlank(name)) {
        cmdLine.addArgument(name);
    }

    final OutputStream out = new ByteArrayOutputStream();
    final OutputStream err = new ByteArrayOutputStream();

    DefaultExecuteResultHandler resultHandler;
    try {
        resultHandler = execute(cmdLine, null, out, err);
        resultHandler.waitFor(DEFAULT_TIMEOUT);
    } catch (ExecuteException e) {
        throw new PBSException("Failed to execute qnodes command: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new PBSException("Failed to execute qnodes command: " + e.getMessage(), e);
    } catch (InterruptedException e) {
        throw new PBSException("Failed to execute qnodes command: " + e.getMessage(), e);
    }

    final int exitValue = resultHandler.getExitValue();
    LOGGER.info("qnodes exit value: " + exitValue);

    try {
        nodes = NODE_XML_PARSER.parse(out.toString());
    } catch (ParseException pe) {
        throw new PBSException("Failed to parse node XML: " + pe.getMessage(), pe);
    }

    return nodes;
}

From source file:com.tupilabs.pbs.PBS.java

/**
 * PBS qsub command.//from w w w.j  a v a 2 s.com
 * <p>
 * Equivalent to qsub [param]
 *
 * @param inputs job input file
 * @param environment environment variables
 * @return job id
 */
public static String qsub(String[] inputs, Map<String, String> environment) {
    final CommandLine cmdLine = new CommandLine(COMMAND_QSUB);
    for (int i = 0; i < inputs.length; ++i) {
        cmdLine.addArgument(inputs[i]);
    }

    final OutputStream out = new ByteArrayOutputStream();
    final OutputStream err = new ByteArrayOutputStream();

    DefaultExecuteResultHandler resultHandler;
    try {
        resultHandler = execute(cmdLine, environment, out, err);
        resultHandler.waitFor(DEFAULT_TIMEOUT);
    } catch (ExecuteException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    } catch (InterruptedException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    }

    final int exitValue = resultHandler.getExitValue();
    LOGGER.info("qsub exit value: " + exitValue);
    LOGGER.fine("qsub output: " + out.toString());

    if (exitValue != 0) {
        throw new PBSException("Failed to submit job script with command line '" + cmdLine.toString()
                + "'. Error output: " + err.toString());
    }

    String jobId = out.toString();
    return jobId.trim();
}

From source file:com.cip.crane.agent.exec.TaurusExecutorTest.java

public void testExecute() throws IOException {
    //        Executor exec = new TaurusExecutor();
    //        exec.execute("test", System.out, System.err, "C:/1.bat");
    String cmd = "sudo -u hadoop bash -c \"kinit\";  ";
    CommandLine cmd1 = new CommandLine(cmd);
    CommandLine cmdLine = new CommandLine("bash");
    cmdLine.addArgument("-c");
    cmdLine.addArgument(cmd);//  ww w .j av  a 2s .  c o m
    System.out.println(cmd1);
    System.out.println(cmdLine.toString());
}

From source file:com.dp.bigdata.taurus.agent.exec.TaurusExecutorTest.java

@Test
public void testExecute() throws IOException {
    //        Executor exec = new TaurusExecutor();
    //        exec.execute("test", System.out, System.err, "C:/1.bat");
    String cmd = "sudo -u hadoop bash -c \"kinit\";  ";
    CommandLine cmd1 = new CommandLine(cmd);
    CommandLine cmdLine = new CommandLine("bash");
    cmdLine.addArgument("-c");
    cmdLine.addArgument(cmd);//  w w  w  . j av  a  2s  .c  o m
    System.out.println(cmd1);
    System.out.println(cmdLine.toString());
}

From source file:de.akquinet.innovation.play.maven.Play2CompilationMojo.java

public void execute() throws MojoExecutionException {

    String line = getPlay2().getAbsolutePath();

    CommandLine cmdLine = CommandLine.parse(line);
    cmdLine.addArgument("compile");
    DefaultExecutor executor = new DefaultExecutor();

    if (timeout > 0) {
        ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
        executor.setWatchdog(watchdog);/*from w  ww . ja  v  a 2s. com*/
    }

    executor.setExitValue(0);
    executor.setWorkingDirectory(project.getBasedir());
    try {
        executor.execute(cmdLine, getEnvironment());
    } catch (IOException e) {
        throw new MojoExecutionException("Error during compilation", e);
    }
}

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

@Override
public Map<String, Object> handle(Task aTask) throws Exception {
    CommandLine cmd = new CommandLine("mediainfo");
    cmd.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);//from ww w.j  ava 2s  . c om
        return parse(FileUtils.readFileToString(tempFile));
    } catch (ExecuteException e) {
        throw new ExecuteException(e.getMessage(), e.getExitValue(),
                new RuntimeException(FileUtils.readFileToString(tempFile)));
    } finally {
        FileUtils.deleteQuietly(tempFile);
    }
}

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

@Override
public Object handle(Task aTask) throws Exception {
    List<String> options = aTask.getList("options", String.class);
    CommandLine cmd = new CommandLine("ffmpeg");
    options.forEach(o -> cmd.addArgument(o));
    log.debug("{}", cmd);
    DefaultExecutor exec = new DefaultExecutor();
    File tempFile = File.createTempFile("log", null);
    try (PrintStream stream = new PrintStream(tempFile);) {
        exec.setStreamHandler(new PumpStreamHandler(stream));
        int exitValue = exec.execute(cmd);
        return exitValue != 0 ? FileUtils.readFileToString(tempFile) : cmd.toString();
    } catch (ExecuteException e) {
        throw new ExecuteException(e.getMessage(), e.getExitValue(),
                new RuntimeException(FileUtils.readFileToString(tempFile)));
    } finally {//  w  w w. j  a va 2  s.c om
        FileUtils.deleteQuietly(tempFile);
    }
}

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);/*  ww  w. ja va2  s  . com*/
        return parse(FileUtils.readFileToString(tempFile));
    } catch (ExecuteException e) {
        throw new ExecuteException(e.getMessage(), e.getExitValue(),
                new RuntimeException(FileUtils.readFileToString(tempFile)));
    } finally {
        FileUtils.deleteQuietly(tempFile);
    }
}

From source file:name.martingeisse.webide.process.NodejsCompanionProcess.java

@Override
protected CommandLine buildCommandLine() {
    CommandLine commandLine = new CommandLine(Configuration.getBashPath());
    commandLine.addArgument("--login");
    commandLine.addArgument("-c");
    commandLine.addArgument("node " + mainFile.getName() + " " + getCompanionId(), false);
    return commandLine;
}

From source file:com.abiquo.nodecollector.service.impl.StonithServiceImpl.java

@Override
public boolean shootTheOtherNodeInTheHead(final String host, final Integer port, final String user,
        final String password) {
    CommandLine command = buildCommand(host, port, user, password);
    command.addArgument("power").addArgument("off");

    return executeCommand(command);
}