Example usage for org.apache.commons.exec DefaultExecuteResultHandler waitFor

List of usage examples for org.apache.commons.exec DefaultExecuteResultHandler waitFor

Introduction

In this page you can find the example usage for org.apache.commons.exec DefaultExecuteResultHandler waitFor.

Prototype

public void waitFor(final long timeout) throws InterruptedException 

Source Link

Document

Causes the current thread to wait, if necessary, until the process has terminated.

Usage

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

/**
 * PBS qnodes command./*from www .j a  va  2  s.  com*/
 * <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 qstat command.//from   w  w w .  j a  va2 s.  c  om
 * <p>
 * Equivalent to qstat -Q -f [name]
 *
 * @param name queue name
 * @return list of queues
 */
public static List<Queue> qstatQueues(String name) {
    final CommandLine cmdLine = new CommandLine(COMMAND_QSTAT);
    cmdLine.addArgument(PARAMETER_FULL_STATUS);
    cmdLine.addArgument(PARAMETER_QUEUE);
    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 qstat command: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new PBSException("Failed to execute qstat command: " + e.getMessage(), e);
    } catch (InterruptedException e) {
        throw new PBSException("Failed to execute qstat command: " + e.getMessage(), e);
    }

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

    final List<Queue> queues;
    try {
        queues = QSTAT_QUEUES_PARSER.parse(out.toString());
    } catch (ParseException pe) {
        throw new PBSException("Failed to parse qstat queues output: " + pe.getMessage(), pe);
    }

    return (queues == null ? new ArrayList<Queue>(0) : queues);
}

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

/**
 * PBS qsub command.//from   w ww  . j  ava  2s. c o m
 * <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:net.test.aliyun.z7.Zip7Object.java

public static int extract(final String extToosHome, final String extractFile, final String toDir)
        throws Throwable {
    String cmdLine = String.format("%s\\7z.exe x \"%s\" -aoa -y \"-o%s\"", extToosHome, extractFile, toDir);
    CommandLine commandline = CommandLine.parse(cmdLine);
    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
    DefaultExecutor executor = new DefaultExecutor();
    int extValue = -1;
    try {//  www  .j  a  va  2 s . com
        executor.execute(commandline, resultHandler);
        resultHandler.waitFor(300 * 1000);
        extValue = resultHandler.getExitValue();
        if (extValue == 0) {
            new File(extractFile).delete();
        }
    } catch (Exception e) {
        //
    } finally {
        if (extValue != 0) {
            FileUtils.deleteDir(new File(toDir));
            return extValue;
        }
    }
    //
    Iterator<File> itFile = FileUtils.iterateFiles(new File(toDir), FileFilterUtils.fileFileFilter(),
            FileFilterUtils.directoryFileFilter());
    while (itFile.hasNext()) {
        File it = itFile.next();
        if (it.isDirectory())
            continue;
        for (String com : compression) {
            if (StringUtils.endsWithIgnoreCase(it.getName(), com)) {
                String itPath = it.getAbsolutePath();
                String subToDir = itPath.substring(0, itPath.length() - com.length());
                extract(extToosHome, itPath, subToDir);
            }
        }
    }
    return 0;
}

From source file:org.apache.geode.examples.replicated.ReplicatedTest.java

/**
 * Execute the kill script that looks for pid files
 * @throws IOException//w ww  .  java 2 s.c  o m
 * @throws InterruptedException
 */
private void runKillScript() throws IOException, InterruptedException {
    CommandLine cmdLine = CommandLine.parse(shell.getFileFromClassLoader(pidkillerScriptFileName)
            .map(x -> x.getAbsolutePath()).orElseThrow(IllegalArgumentException::new));
    cmdLine.addArgument(testFolder.getRoot().getAbsolutePath());

    DefaultExecuteResultHandler resultHandler = shell.execute(cmdLine, scriptTimeout, environment,
            testFolder.getRoot());
    resultHandler.waitFor(scriptTimeout);
}

From source file:org.obm.push.mail.greenmail.ExternalProcess.java

private void waitForProcessStartTime(DefaultExecuteResultHandler resultHandler)
        throws ExternalProcessException {
    try {// w  w w  .j a va2s  .  co m
        if (processStartTimeNeeded > 0) {
            resultHandler.waitFor(processStartTimeNeeded);
        }
    } catch (InterruptedException e) {
        throw new ExternalProcessException("Could not execute process", e);
    }
}