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

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

Introduction

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

Prototype

public DefaultExecuteResultHandler() 

Source Link

Document

Constructor.

Usage

From source file:uk.org.sappho.applications.transcript.service.registry.vcs.CommandExecuter.java

public String execute(Command command, File directory) throws TranscriptException {

    try {/*from  ww  w.  ja  v a  2 s.  c om*/
        if (logger.isLoggable(Level.INFO)) {
            logger.info(command.getSafeCommand());
        }
        DefaultExecutor executor = new DefaultExecutor();
        DefaultExecuteResultHandler commandResultsHandler = new DefaultExecuteResultHandler();
        ByteArrayOutputStream commandOutputStream = new ByteArrayOutputStream();
        ByteArrayOutputStream commandErrorStream = new ByteArrayOutputStream();
        PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(commandOutputStream, commandErrorStream);
        executor.setWatchdog(new ExecuteWatchdog(60000));
        executor.setStreamHandler(pumpStreamHandler);
        executor.setWorkingDirectory(directory);
        executor.execute(command.getCommandLine(), commandResultsHandler);
        commandResultsHandler.waitFor();
        if (commandResultsHandler.getExitValue() != 0) {
            throw new TranscriptException(commandErrorStream.toString());
        }
        return commandOutputStream.toString();
    } catch (Throwable throwable) {
        if (logger.isLoggable(Level.WARNING)) {
            logger.warning(throwable.getMessage());
        }
        throw new TranscriptException("Unable to execute system command: " + command.getSafeCommand(),
                throwable);
    }
}