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

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

Introduction

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

Prototype

public int execute(final CommandLine command) throws ExecuteException, IOException 

Source Link

Usage

From source file:at.medevit.elexis.gdt.handler.GDTOutputHandler.java

public static void handleOutput(GDTSatzNachricht gdtSatzNachricht, IGDTCommunicationPartner cp) {
    int connectionType = cp.getConnectionType();

    switch (connectionType) {
    case SystemConstants.FILE_COMMUNICATION:
        boolean success = GDTFileHelper.writeGDTSatzNachricht(gdtSatzNachricht, cp);
        if (success) {
            GDTProtokoll.addEntry(GDTProtokoll.MESSAGE_DIRECTION_OUT, cp, gdtSatzNachricht);
        } else {/*  w  w w  . ja v a 2  s.  c o  m*/
            String message = "Fehler beim Schreiben der GDT Satznachricht "
                    + gdtSatzNachricht.getValue(GDTConstants.FELDKENNUNG_SATZIDENTIFIKATION) + " auf "
                    + cp.getLabel();
            Status status = new Status(IStatus.WARNING, Activator.PLUGIN_ID, message);
            StatusManager.getManager().handle(status, StatusManager.SHOW);
            logger.log(message, Log.WARNINGS);
        }

        // Update the protokoll view
        final IViewPart protokoll = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
                .findView(GDTProtokollView.ID);
        Display display = PlatformUI.getWorkbench().getDisplay();
        display.asyncExec(new Runnable() {
            @Override
            public void run() {
                if (protokoll != null)
                    protokoll.setFocus();
            }
        });

        // Call the external program to care for the output (if applicable)
        String handlerProgram = cp.getExternalHandlerProgram();
        if (handlerProgram != null) {
            CommandLine cmdLine = CommandLine.parse(handlerProgram);
            try {
                DefaultExecutor executor = new DefaultExecutor();
                executor.setExitValues(null); // Ignore the exit value
                int exitValue = executor.execute(cmdLine);
                logger.log("Return value of " + cmdLine + ": " + exitValue, Log.DEBUGMSG);
            } catch (ExecuteException e) {
                String message = "Fehler beim Ausfhren von " + cmdLine;
                Status status = new Status(IStatus.WARNING, Activator.PLUGIN_ID, message, e);
                StatusManager.getManager().handle(status, StatusManager.SHOW);
                logger.log(e, message, Log.ERRORS);
            } catch (IOException e) {
                String message = "Fehler beim Ausfhren von " + cmdLine;
                Status status = new Status(IStatus.WARNING, Activator.PLUGIN_ID, message, e);
                StatusManager.getManager().handle(status, StatusManager.SHOW);
                logger.log(e, message, Log.ERRORS);
            }
        }
        break;
    case SystemConstants.SERIAL_COMMUNICATION:
        // TODO Serial output implementation
        break;
    default:
        break;
    }
}

From source file:au.com.jwatmuff.genericp2p.windows.ExecUtil.java

public static String getStdoutForCommand(String cmd) {
    CommandLine cmdLine = CommandLine.parse(cmd);
    DefaultExecutor executor = new DefaultExecutor();
    executor.setWatchdog(new ExecuteWatchdog(60000));
    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    executor.setStreamHandler(new PumpStreamHandler(stdout, null));
    try {//w  ww .  j  ava  2 s.  com
        executor.execute(cmdLine);
    } catch (ExecuteException e) {
        log.error("Exception executing '" + cmd + "'", e);
    } catch (IOException e) {
        log.error("IOException executing '" + cmd + "'", e);
    }

    return stdout.toString();
}

From source file:net.robyf.dbpatcher.util.MySqlUtil.java

private static void execute(final CommandLine commandLine) {
    try {// www .j  a va 2s.c o m
        DefaultExecutor executor = new DefaultExecutor();
        executor.setWatchdog(new ExecuteWatchdog(300000L));
        int returnCode = executor.execute(commandLine);
        if (returnCode != 0) {
            throw new UtilException("Error executing: " + commandLine // NOSONAR
                    + ", return code = " + returnCode); //NOSONAR
        }
    } catch (IOException ioe) {
        throw new UtilException("Error executing: " + commandLine, ioe); //NOSONAR
    }
}

From source file:io.selendroid.io.ShellCommand.java

public static String exec(CommandLine commandline, long timeoutInMillies) throws ShellCommandException {
    log.info("executing command: " + commandline);
    PritingLogOutputStream outputStream = new PritingLogOutputStream();
    DefaultExecutor exec = new DefaultExecutor();
    exec.setWatchdog(new ExecuteWatchdog(timeoutInMillies));
    PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
    exec.setStreamHandler(streamHandler);
    try {/*from   w  w  w . ja v a 2s . c  o  m*/
        exec.execute(commandline);
    } catch (Exception e) {
        throw new ShellCommandException("An error occured while executing shell command: " + commandline,
                new ShellCommandException(outputStream.getOutput()));
    }
    return (outputStream.getOutput());
}

From source file:com.jredrain.base.utils.CommandUtils.java

public static String executeShell(File shellFile, String... args) {
    String info = null;// w  ww . j av  a  2 s . c  om
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {

        String params = " ";
        if (CommonUtils.notEmpty(args)) {
            for (String p : args) {
                params += p + " ";
            }
        }

        CommandLine commandLine = CommandLine.parse("/bin/bash +x " + shellFile.getAbsolutePath() + params);
        DefaultExecutor exec = new DefaultExecutor();
        exec.setExitValues(null);
        PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, outputStream);
        exec.setStreamHandler(streamHandler);

        exec.execute(commandLine);
        info = outputStream.toString().trim();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return info;
    }
}

From source file:io.selendroid.standalone.io.ShellCommand.java

public static String exec(CommandLine commandline, long timeoutInMillies) throws ShellCommandException {
    log.info("Executing shell command: " + commandline);
    PrintingLogOutputStream outputStream = new PrintingLogOutputStream();
    DefaultExecutor exec = new DefaultExecutor();
    exec.setWatchdog(new ExecuteWatchdog(timeoutInMillies));
    PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
    exec.setStreamHandler(streamHandler);
    try {/*from  www .j ava2  s .  c  om*/
        exec.execute(commandline);
    } catch (Exception e) {
        log.log(Level.SEVERE, "Error executing command: " + commandline, e);
        if (e.getMessage().contains("device offline")) {
            throw new DeviceOfflineException(e);
        }
        throw new ShellCommandException("Error executing shell command: " + commandline,
                new ShellCommandException(outputStream.getOutput()));
    }
    String result = outputStream.getOutput().trim();
    log.info("Shell command output\n-->\n" + result + "\n<--");
    return result;
}

From source file:net.robyf.dbpatcher.util.MySqlUtil.java

private static void executeWithInput(final CommandLine commandLine, final InputStream input) {
    try {/*www.  ja  v  a 2s .  com*/
        PumpStreamHandler handler = new PumpStreamHandler(null, null, input);

        DefaultExecutor executor = new DefaultExecutor();
        executor.setStreamHandler(handler);
        executor.setWatchdog(new ExecuteWatchdog(300000L));
        int returnCode = executor.execute(commandLine);
        if (returnCode != 0) {
            throw new UtilException("Error executing: " + commandLine //NOSONAR
                    + ", return code = " + returnCode); //NOSONAR
        }
    } catch (IOException ioe) {
        throw new UtilException("Error executing: " + commandLine, ioe); //NOSONAR
    }
}

From source file:net.robyf.dbpatcher.util.MySqlUtil.java

private static InputStream executeAndGetOutput(final CommandLine commandLine) {
    try {//from   ww  w  . j  a va 2  s .  c  o m
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        PumpStreamHandler handler = new PumpStreamHandler(outStream);

        DefaultExecutor executor = new DefaultExecutor();
        executor.setWatchdog(new ExecuteWatchdog(300000L));
        executor.setStreamHandler(handler);
        int returnCode = executor.execute(commandLine);
        if (returnCode != 0) {
            throw new UtilException("Error executing: " + commandLine //NOSONAR
                    + ", return code = " + returnCode); //NOSONAR
        }
        return new ByteArrayInputStream(outStream.toByteArray());
    } catch (IOException ioe) {
        throw new UtilException("Error executing: " + commandLine, ioe); //NOSONAR
    }
}

From source file:de.tudarmstadt.ukp.experiments.argumentation.convincingness.svmlib.SVMLibExperimentRunner.java

public static void runCommand(String command) throws IOException {
    CommandLine cmdLine = CommandLine.parse(command);
    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(0);/*from   w ww . j a  v  a  2s  .  com*/

    // set one hour limit for training
    ExecuteWatchdog watchdog = new ExecuteWatchdog(1000 * 60 * 60);
    executor.setWatchdog(watchdog);

    System.out.println("Running\n" + command);

    int exitValue = executor.execute(cmdLine);
}

From source file:com.ghgande.j2mod.modbus.utils.TestUtils.java

/**
 * Runs a command line task and returns the screen output or throws and
 * error if something bad happened/*from w  w  w. jav  a  2  s. c o  m*/
 *
 * @param command Command to run
 *
 * @return Screen output
 *
 * @throws Exception
 */
public static String execToString(String command) throws Exception {

    // Prepare the command line

    CommandLine commandline = CommandLine.parse(command);

    // Prepare the output stream

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

    // Prepare the executor

    DefaultExecutor exec = new DefaultExecutor();
    exec.setExitValues(null);
    exec.setStreamHandler(streamHandler);
    exec.setWatchdog(new ExecuteWatchdog(5000));

    // Execute the command
    try {
        exec.execute(commandline);
        return (outputStream.toString());
    } catch (Exception e) {
        throw new Exception(String.format("%s - %s", outputStream.toString(), e.getMessage()));
    }
}