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:fitnesse.maven.io.CommandShell.java

public String execute(File workingDir, String... commands) {
    CommandLine commandLine = CommandLine.parse(commands[0]);
    for (int i = 1; i < commands.length; i++) {
        commandLine.addArgument(commands[i]);
    }/*  w w  w  . jav a 2s.  c o  m*/
    DefaultExecutor executor = new DefaultExecutor();
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        executor.setStreamHandler(new PumpStreamHandler(baos));
        executor.setWorkingDirectory(workingDir);
        executor.execute(commandLine);
        return new String(baos.toByteArray());
    } catch (ExecuteException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:edu.buffalo.fusim.ReadSimulator.java

public void run(String artBinPath, File fusionFile, String outputPrefix, int readLength, int meanFragSize,
        int readCoverage, boolean pairedEnd) {
    // Example art call:
    //   art_illumina -i fusion.txt -o testsim -l 75 -f 10 -p -m 400 -s 10 
    CommandLine cmdLine = new CommandLine(artBinPath);

    // the filename of input DNA reference
    cmdLine.addArgument("-i");
    cmdLine.addArgument("${file}");
    // the prefix of output files
    cmdLine.addArgument("-o");
    cmdLine.addArgument("${outputPrefix}");
    // the length of reads to be simulated
    cmdLine.addArgument("-l");
    cmdLine.addArgument("" + readLength);
    // the fold of read coverage to be simulated
    cmdLine.addArgument("-f");
    cmdLine.addArgument("" + readCoverage);
    if (pairedEnd) {
        // indicate a paired-end read simulation
        cmdLine.addArgument("-p");
        // the mean size of DNA fragments for paired-end simulations
        cmdLine.addArgument("-m");
        cmdLine.addArgument("" + meanFragSize);
        // the standard deviation of DNA fragment size for paired-end simulations.
        cmdLine.addArgument("-s");
        cmdLine.addArgument("10");
    }//from   www .j av a 2 s .  c  om
    // quite - turn off end of run summary
    cmdLine.addArgument("-q");

    Map map = new HashMap();
    map.put("file", fusionFile);
    map.put("outputPrefix", outputPrefix);
    cmdLine.setSubstitutionMap(map);

    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(0);
    // Timeout after 5 minutes
    ExecuteWatchdog watchdog = new ExecuteWatchdog(300000);
    executor.setWatchdog(watchdog);

    try {
        int exitValue = executor.execute(cmdLine);
    } catch (Exception e) {
        logger.fatal("Failed to execute ART for simulating Illumina reads: " + e.getMessage());
    }
}

From source file:com.adaptris.hpcc.ListLogicalFiles.java

@Override
public AdaptrisMessage request(AdaptrisMessage msg, ProduceDestination destination, long timeoutMs)
        throws ProduceException {
    try {//from ww  w.j a  v  a  2  s . c  om
        String dest = destination.getDestination(msg);
        CommandLine commandLine = retrieveConnection(DfuplusConnection.class).createCommand();
        commandLine.addArgument("action=list");
        commandLine.addArgument(String.format("name=%s", dest));
        log.trace("Executing {}", commandLine);
        try (PrintWriter out = new PrintWriter(msg.getWriter())) {
            // parser is closed as part of the execution
            ListLogicalFilesOutput parser = new ListLogicalFilesOutput(dest, out); // lgtm [java/output-resource-leak]
            executeInternal(commandLine, parser);
        }
    } catch (Exception e) {
        throw ExceptionHelper.wrapProduceException(e);
    }
    return msg;
}

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

public void execute() throws MojoExecutionException {

    String line = getPlay2().getAbsolutePath();

    CommandLine cmdLine = CommandLine.parse(line);
    cmdLine.addArguments(getPlay2SystemPropertiesArguments(), false);
    cmdLine.addArgument("run");
    DefaultExecutor executor = new DefaultExecutor();

    // As where not linked to a project, we can't set the working directory.
    // So it will use the directory where mvn was launched.

    executor.setExitValue(0);/*from   w w w.  j av a 2s. com*/
    try {
        executor.execute(cmdLine, getEnvironment());
    } catch (IOException e) {
        // Ignore.
    }
}

From source file:com.adaptris.hpcc.SprayToThorImpl.java

protected CommandLine createSprayCommand(AdaptrisMessage msg) throws PasswordException, IOException {
    CommandLine cmdLine = retrieveConnection(DfuplusConnection.class).createCommand();
    cmdLine.addArgument("action=spray");
    cmdLine.addArgument(String.format("dstcluster=%s", msg.resolve(getCluster())));
    cmdLine.addArgument(String.format("overwrite=%d", overwrite() ? 1 : 0));
    cmdLine.addArgument("nowait=1");
    return cmdLine;
}

From source file:com.adaptris.hpcc.DeleteFromThor.java

@Override
public AdaptrisMessage request(AdaptrisMessage msg, ProduceDestination destination, long timeoutMs)
        throws ProduceException {
    try {/*from  ww  w. ja v  a  2  s. com*/
        String dest = destination.getDestination(msg);
        CommandLine commandLine = retrieveConnection(DfuplusConnection.class).createCommand();
        commandLine.addArgument("action=remove");
        commandLine.addArgument(String.format("name=%s", dest));
        log.trace("Executing {}", commandLine);
        DeleteOutputParser p = new DeleteOutputParser();
        executeInternal(commandLine, p);
        log.info("Delete on [{}]: {}", dest, p.actualState);
    } catch (AbortJobException e) {
        throw ExceptionHelper.wrapProduceException(generateExceptionMessage(e), e);
    } catch (Exception e) {
        throw ExceptionHelper.wrapProduceException(e);
    }
    return msg;
}

From source file:edu.buffalo.fusim.BowtieAlignment.java

public void run(String bowtiePath, String index, File read1, File read2, File outFile, int threads) {
    // Example bowtie call:
    //   bowtie -t -v 2 -p 12 -m 10 -S
    CommandLine cmdLine = new CommandLine(bowtiePath);

    // print wall-clock time taken by search phases
    cmdLine.addArgument("-t");
    // eport end-to-end hits w/ <=v mismatches; ignore qualities
    cmdLine.addArgument("-v");
    cmdLine.addArgument("2");
    // number of alignment threads to launch
    cmdLine.addArgument("-p");
    cmdLine.addArgument("" + threads);
    // suppress all alignments if > <int> exist
    cmdLine.addArgument("-m");
    cmdLine.addArgument("10");
    // write hits in SAM format
    cmdLine.addArgument("-S");
    // bowtie index
    cmdLine.addArgument("${index}");
    if (read2 != null) {
        // fastq1
        cmdLine.addArgument("-1");
        cmdLine.addArgument("${read1}");
        // fastq2
        cmdLine.addArgument("-2");
        cmdLine.addArgument("${read2}");
    } else {//www .  j a v a  2  s .  c o m
        cmdLine.addArgument("${read1}");
    }
    // output SAM file
    cmdLine.addArgument("${outFile}");

    Map map = new HashMap();
    map.put("index", index);
    map.put("read1", read1);
    if (read2 != null) {
        map.put("read2", read2);
    }
    map.put("outFile", outFile);
    cmdLine.setSubstitutionMap(map);

    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(0);
    // Never timeout 
    ExecuteWatchdog watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT);
    executor.setWatchdog(watchdog);

    try {
        int exitValue = executor.execute(cmdLine);
    } catch (Exception e) {
        logger.fatal(
                "Failed to execute bowtie for aligning simulated Illumina reads from ART: " + e.getMessage());
    }
}

From source file:edu.emory.cci.aiw.neo4jetl.Neo4jHome.java

private void controlServer(String command) throws IOException, InterruptedException, CommandFailedException {
    LOGGER.debug("Executing neo4j command {}...", command);
    CommandLine serverControlCommand = new CommandLine(new File(this.home, SERVER_CONTROL_COMMAND));
    serverControlCommand.addArgument("${command}");
    Map<String, String> map = new HashMap<>();
    map.put("command", command);
    serverControlCommand.setSubstitutionMap(map);

    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();

    ExecuteWatchdog watchdog = new ExecuteWatchdog(60 * 1000);
    Executor executor = new DefaultExecutor();
    executor.setExitValue(1);/*from  ww w  . j a  v a2  s.com*/
    executor.setWatchdog(watchdog);
    executor.execute(serverControlCommand, resultHandler);
    LOGGER.debug("Neo4j command {} is completed, checking exit value...", command);
    resultHandler.waitFor();
    int exitValue = resultHandler.getExitValue();
    if (exitValue != 0) {
        ExecuteException exception = resultHandler.getException();
        throw new CommandFailedException(exitValue, "Neo4j command '" + command + "' failed", exception);
    }
    LOGGER.debug("Neo4j command {} was successful", command);
}

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

public void execute() throws MojoExecutionException {

    String line = getPlay2().getAbsolutePath();

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

    if (timeout > 0) {
        ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
        executor.setWatchdog(watchdog);//from   w  w w .j a va2 s  .  c o m
    }

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

    // Also delete the dist directory
    File dist = new File(project.getBasedir(), "dist");
    if (dist.exists()) {
        getLog().debug("Deleting " + dist.getAbsolutePath());
        try {
            FileUtils.deleteDirectory(dist);
        } catch (IOException e) {
            throw new MojoExecutionException("Can't delete the dist folder", e);
        }
    } else {
        getLog().debug("'dist' directory not found");
    }

    // Delete the log folder
    File logs = new File(project.getBasedir(), "logs");
    if (logs.exists()) {
        getLog().debug("Deleting " + logs.getAbsolutePath());
        try {
            FileUtils.deleteDirectory(logs);
        } catch (IOException e) {
            throw new MojoExecutionException("Can't delete the logs folder", e);
        }
    } else {
        getLog().debug("'logs' directory not found");
    }

    // Also delete the lib directory if set
    if (cleanLibFolder) {
        File lib = new File(project.getBasedir(), "lib");
        if (lib.exists()) {
            getLog().debug("Deleting " + lib.getAbsolutePath());
            try {
                FileUtils.deleteDirectory(lib);
            } catch (IOException e) {
                throw new MojoExecutionException("Can't delete the " + lib + " folder", e);
            }
        } else {
            getLog().debug("'" + lib + "' directory not found");
        }
    }
}

From source file:name.martingeisse.webide.nodejs.AbstractNodejsServer.java

/**
 * Starts this server./*from   ww w.ja v  a  2 s .c  om*/
 */
public void start() {

    // determine the path of the associated script
    URL url = getScriptUrl();
    if (!url.getProtocol().equals("file")) {
        throw new RuntimeException("unsupported protocol for associated script URL: " + url);
    }
    File scriptFile = new File(url.getPath());

    // build the command line
    CommandLine commandLine = new CommandLine(Configuration.getBashPath());
    commandLine.addArgument("--login");
    commandLine.addArgument("-c");
    commandLine.addArgument("node " + scriptFile.getName(), false);

    // build I/O streams
    ByteArrayInputStream inputStream = null;
    OutputStream outputStream = System.err;
    OutputStream errorStream = System.err;
    ExecuteStreamHandler streamHandler = new PumpStreamHandler(outputStream, errorStream, inputStream);

    // build an environment map that contains the path to the node_modules
    Map<String, String> environment = new HashMap<String, String>();
    environment.put("NODE_PATH", new File("lib/node_modules").getAbsolutePath());

    // run Node.js
    Executor executor = new DefaultExecutor();
    executor.setProcessDestroyer(new ShutdownHookProcessDestroyer());
    executor.setStreamHandler(streamHandler);
    try {
        executor.setWorkingDirectory(scriptFile.getParentFile());
        executor.execute(commandLine, environment, new ExecuteResultHandler() {

            @Override
            public void onProcessFailed(ExecuteException e) {
            }

            @Override
            public void onProcessComplete(int exitValue) {
            }

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

}