Example usage for org.apache.commons.exec DefaultExecutor setExitValue

List of usage examples for org.apache.commons.exec DefaultExecutor setExitValue

Introduction

In this page you can find the example usage for org.apache.commons.exec DefaultExecutor setExitValue.

Prototype

public void setExitValue(final int value) 

Source Link

Usage

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

/**
 *   (commandLine) ?   ?? ?  ? ?/*from   ww  w. jav  a  2 s. co m*/
 * 
 * @param commandLine    
 * @param argument     ?
 * @param timeout     
 * 
 * @return int ? 
 */
public static int execute(CommandLine commandLine, String argument, long timeout) {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(baos);

    DefaultExecutor executor = new DefaultExecutor();
    executor.setStreamHandler(streamHandler);

    // ? 
    if (StringUtils.isNotEmpty(argument)) {
        commandLine.addArguments(argument);
    }

    //  
    if (timeout > 0) {
        ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
        executor.setWatchdog(watchdog);
    }

    // ??  ? 
    executor.setExitValue(SUCCESS_RETURN_CODE);

    try {

        DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();

        executor.execute(commandLine, resultHandler);
        resultHandler.waitFor();

        return resultHandler.getExitValue();

    } catch (Exception e) {

        throw new CmdUtilsException(e.getMessage());
    }
}

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

/**
 * Executes a PBS command./* w w w .  j  a va2 s.co m*/
 *
 * @param cmdLine command
 * @param environment env vars
 * @param out output stream
 * @param err err stream
 * @return execute handler
 * @throws ExecuteException if there is an error executing a command
 * @throws IOException in case of an IO problem
 */
static DefaultExecuteResultHandler execute(CommandLine cmdLine, Map<String, String> environment,
        OutputStream out, OutputStream err) throws ExecuteException, IOException {
    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
    ExecuteStreamHandler streamHandler = new PumpStreamHandler(out, err);
    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(0);
    executor.setStreamHandler(streamHandler);
    if (environment != null) {
        executor.execute(cmdLine, environment, resultHandler);
    } else {
        executor.execute(cmdLine, resultHandler);
    }
    return resultHandler;
}

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

private void execute(CommandLine cmdLine) throws IOException {
    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(0);
    ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
    executor.setWatchdog(watchdog);/* w w  w  .j  a  v a2s.  co m*/
    int exitValue = executor.execute(cmdLine);
    if (exitValue != 0) {
        throw new RuntimeException("Pandoc is not installed !");
    }
}

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   ww  w. j ava2  s  . com*/
    // 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.meltmedia.cadmium.blackbox.test.CliCommitCloneTest.java

/**
 * <p>Runs the Cadmium CLI command commit from Apache Commons Exec library. Then uses {@link CadmiumAssertions#assertContentDeployed(String, java.io.File, String)} to assert that the content deployed properly.</p>
 * <p>To override the site url, pass <code>-Dcom.meltmedia.cadmium.test.site.url=[URL]</code> to the jvm that this test case will be running in. The site url defaults to <code>http://localhost</code> if not set.</p>
 * @throws IOException See {@link DefaultExecutor#execute(CommandLine)} for more information.
 * @throws ExecuteException See {@link DefaultExecutor#execute(CommandLine)} for more information.
 * @throws InterruptedException //from  w  ww.jav a  2 s.c  om
 * 
 * @see <a href="http://commons.apache.org/exec">Apache Commons Exec</a>
 */
@Test
public void testCommit() throws ExecuteException, IOException {
    String deployUrl = System.getProperty("com.meltmedia.cadmium.test.site.url", DEPLOY_URL);
    CommandLine commitCmd = CommandLine
            .parse("cadmium commit -m \"Testing commit command\" " + TEST_CONTENT_LOCATION + " " + deployUrl);

    DefaultExecutor exec = new DefaultExecutor();
    exec.setExitValue(0);
    ExecuteWatchdog watchDog = new ExecuteWatchdog(60000);
    exec.setWatchdog(watchDog);

    int exitValue = exec.execute(commitCmd);

    assertEquals("Commit command returned with an error status.", exitValue, 0);

    assertContentDeployed("Failed to commit content to remote site.", new File(TEST_CONTENT_LOCATION),
            DEPLOY_URL);
}

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 {// ww w .j  a  va2  s  . com
        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:com.alibaba.jstorm.yarn.utils.JStormUtils.java

/**
 * If it is backend, please set resultHandler, such as DefaultExecuteResultHandler If it is frontend, ByteArrayOutputStream.toString get the result
 * <p/>// w  w w .  j  a  v  a2s .  c  om
 * This function don't care whether the command is successfully or not
 *
 * @param command
 * @param environment
 * @param workDir
 * @param resultHandler
 * @return
 * @throws IOException
 */
@Deprecated
public static ByteArrayOutputStream launchProcess(String command, final Map environment, final String workDir,
        ExecuteResultHandler resultHandler) throws IOException {

    String[] cmdlist = command.split(" ");

    CommandLine cmd = new CommandLine(cmdlist[0]);
    for (String cmdItem : cmdlist) {
        if (StringUtils.isBlank(cmdItem) == false) {
            cmd.addArgument(cmdItem);
        }
    }

    DefaultExecutor executor = new DefaultExecutor();

    executor.setExitValue(0);
    if (StringUtils.isBlank(workDir) == false) {
        executor.setWorkingDirectory(new File(workDir));
    }

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    PumpStreamHandler streamHandler = new PumpStreamHandler(out, out);
    if (streamHandler != null) {
        executor.setStreamHandler(streamHandler);
    }

    try {
        if (resultHandler == null) {
            executor.execute(cmd, environment);
        } else {
            executor.execute(cmd, environment, resultHandler);
        }
    } catch (ExecuteException e) {

        // @@@@
        // failed to run command
    }

    return out;

}

From source file:com.nts.alphamale.shell.AdbShellExecutor.java

/**
 * @param cmd adb //  www  . j  a  va 2  s.c  o  m
 * @param synch   ? ? true: synchronous, false: asynchronous
 * @param  (ms)
 * @return Executor Standard Output? Map
 */
public int execute(CommandLine cmd, int exitValue) {
    int rtnValue = -1;
    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(exitValue);
    try {
        rtnValue = executor.execute(cmd);
    } catch (Exception e) {
        log.error(e.getCause() + ":" + e.getMessage() + "[" + cmd + "]");
    }
    return rtnValue;
}

From source file:com.blackducksoftware.tools.scmconnector.core.CommandLineExecutor.java

@Override
public CommandResults executeCommandForOutput(Logger log, CommandLine command, File targetDir)
        throws Exception {
    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(0);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    PumpStreamHandler psh = new PumpStreamHandler(outputStream);
    executor.setStreamHandler(psh);//from ww  w .  j a va  2 s. co  m

    int exitStatus = 1;
    executor.setWorkingDirectory(targetDir);
    exitStatus = executor.execute(command);
    String outputString = outputStream.toString();

    // This log msg would reveal password
    // log.info("Command: " + command.toString() + " executed in: "
    // + targetDir.toString() + "; output: " + outputString);

    return new CommandResults(exitStatus, outputString);
}

From source file:com.blackducksoftware.tools.scmconnector.core.CommandLineExecutor.java

@Override
public int executeCommand(Logger log, CommandLine command, File targetDir, String promptResponse)
        throws Exception {
    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(0);

    PumpStreamHandler psh = new PumpStreamHandler(new ExecLogHangler(log, Level.INFO));
    executor.setStreamHandler(psh);//from w  w  w . jav a 2 s  .c om

    providePromptResponse(psh, promptResponse);

    executor.setWorkingDirectory(targetDir);

    // This log msg would reveal password
    // log.info("Command: " + command.toString() + " executed in: "
    // + targetDir.toString());

    int exitStatus = 1;
    try {
        exitStatus = executor.execute(command, EnvironmentUtils.getProcEnvironment());
    } catch (Exception e) {
        log.error("Failure executing Command: " + e.getMessage());
    }
    return exitStatus;
}