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

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

Introduction

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

Prototype

public void setWatchdog(final ExecuteWatchdog watchDog) 

Source Link

Usage

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 {/* ww  w . j  av a 2s.  c  om*/
        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: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 {/*www .j a v a2 s. 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: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 {// ww w  .  j  av  a2  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:com.cprassoc.solr.auth.util.CommandLineExecutor.java

public static int exec(String cmd) {
    int exitValue = -1;
    try {//w ww . j a  v  a  2 s  .  c  o  m
        CommandLine cmdLine = CommandLine.parse(cmd);
        DefaultExecutor executor = new DefaultExecutor();
        executor.setExitValue(1);
        ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
        executor.setWatchdog(watchdog);
        exitValue = executor.execute(cmdLine);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return exitValue;
}

From source file:common.UglyLaunchTempPatch.java

/**
 * code for lunching external jar file//from www  .  ja  v a  2  s .  c  om
 * 
 * @param jarFile
 *            the jar file that is being lunched
 * @param Server
 *            is this a server lunch or not
 * @throws IOException
 * @throws ClassNotFoundException
 * @throws NoSuchMethodException
 * @throws InvocationTargetException
 * @throws IllegalAccessException
 * @throws InterruptedException
 */
public static void jar(File jarFile) throws IOException, ClassNotFoundException, NoSuchMethodException,
        InvocationTargetException, IllegalAccessException, InterruptedException {

    Main.print("\"" + jarFile.getAbsolutePath() + "\"");
    // sets jar to deleate on exit of program
    jarFile.deleteOnExit();

    //runs the client version of the forge installer.

    Map map = new HashMap();
    map.put("file", jarFile);
    CommandLine cmdLine = new CommandLine("java");
    cmdLine.addArgument("-jar");
    cmdLine.addArgument("${file}");
    cmdLine.setSubstitutionMap(map);
    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(0);
    ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
    executor.setWatchdog(watchdog);
    int exitValue = executor.execute(cmdLine);
}

From source file:client.UglyLaunchTempPatch.java

/**
 * code for lunching external jar file//from   ww  w.  j  a  v a 2  s.com
 * 
 * @param jarFile
 *            the jar file that is being lunched
 * @param Server
 *            is this a server lunch or not
 * @throws IOException
 * @throws ClassNotFoundException
 * @throws NoSuchMethodException
 * @throws InvocationTargetException
 * @throws IllegalAccessException
 * @throws InterruptedException
 */
public static void jar(File jarFile) throws IOException, ClassNotFoundException, NoSuchMethodException,
        InvocationTargetException, IllegalAccessException, InterruptedException {

    main.print("\"" + jarFile.getAbsolutePath() + "\"");
    // sets jar to deleate on exit of program
    jarFile.deleteOnExit();

    //runs the client version of the forge installer.

    Map map = new HashMap();
    map.put("file", jarFile);
    CommandLine cmdLine = new CommandLine("java");
    cmdLine.addArgument("-jar");
    cmdLine.addArgument("${file}");
    cmdLine.setSubstitutionMap(map);
    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(0);
    ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
    executor.setWatchdog(watchdog);
    int exitValue = executor.execute(cmdLine);
}

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

private static void execute(final CommandLine commandLine) {
    try {// w ww  .j  a  va2  s.co  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:net.robyf.dbpatcher.util.MySqlUtil.java

private static InputStream executeAndGetOutput(final CommandLine commandLine) {
    try {//  w ww.j  a  va 2 s .com
        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);// w ww .  j  a v a  2s. c o m

    // 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:net.robyf.dbpatcher.util.MySqlUtil.java

private static void executeWithInput(final CommandLine commandLine, final InputStream input) {
    try {//  ww  w .  j a  v  a2  s. co m
        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
    }
}