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:com.comcast.tvx.haproxy.HAProxyServiceController.java

@Override
public int reload() {
    if (!new File("/etc/init.d/haproxy").exists()) {
        logger.info("HaProxy is not installed");
        throw new IllegalArgumentException("HaProxy is not installed");
    }//w w  w .j  av a2s. c o  m

    CommandLine cmdLine = new CommandLine("sudo");
    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    PumpStreamHandler psh = new PumpStreamHandler(stdout);
    cmdLine.addArgument("service");
    cmdLine.addArgument("haproxy");
    cmdLine.addArgument("reload");

    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValues(new int[] { 0, 1 });

    ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
    executor.setWatchdog(watchdog);
    executor.setStreamHandler(psh);

    int exitValue;
    try {
        exitValue = executor.execute(cmdLine);
    } catch (ExecuteException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    logger.info("output from running process: " + stdout.toString());
    logger.info("Exit value was: " + exitValue);
    return exitValue;

}

From source file:com.rest4j.generator.PythonGeneratorTest.java

@Test
public void testPythonClient() throws Exception {
    gen.setStylesheet("com/rest4j/client/python.xslt");
    new File("target/python").mkdir();
    gen.setApiXmlUrl(getClass().getResource("doc-generator-graph.xml"));
    gen.setOutputDir("target/python");
    gen.addParam(new TemplateParam("common-params", "access-token"));
    gen.addParam(new TemplateParam("additional-client-code", "\t# ADDITIONAL CODE"));
    gen.generate();//  ww w  . j a  v a  2  s. com

    // let's try compiling the damn thing
    CommandLine cmdLine = CommandLine.parse("python apiclient.py");
    DefaultExecutor executor = new DefaultExecutor();
    executor.setWorkingDirectory(new File("target/python"));
    ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
    executor.setWatchdog(watchdog);
    int exitValue = executor.execute(cmdLine);
    assertFalse(executor.isFailure(exitValue));

    String a = IOUtils.toString(new File("target/python/apiclient.py").toURI());

    // check doc comments
    assertTrue(a, a.contains("Some additional client info"));
    assertTrue(a, a.contains("Some additional python client info"));

    // check existence of Parameter Object class
    assertTrue(a, a.contains("class PatchBRequest"));

    // check additional-client-code
    assertTrue(a, a.contains("\t# ADDITIONAL CODE"));
}

From source file:de.akquinet.innovation.play.maven.Play2TestMojo.java

public void execute() throws MojoExecutionException {

    if (isSkipExecution()) {
        getLog().info("Test phase skipped");
        return;//  w w  w  .j av  a  2 s  .  c  o m
    }

    String line = getPlay2().getAbsolutePath();

    CommandLine cmdLine = CommandLine.parse(line);
    cmdLine.addArguments(getPlay2SystemPropertiesArguments(), false);
    cmdLine.addArgument("test");
    DefaultExecutor executor = new DefaultExecutor();

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

    executor.setWorkingDirectory(project.getBasedir());

    executor.setExitValue(0);
    try {
        executor.execute(cmdLine, getEnvironment());
    } catch (IOException e) {
        if (testFailureIgnore) {
            getLog().error("Test execution failures ignored");
        } else {
            throw new MojoExecutionException("Error during compilation", e);
        }
    }
}

From source file:com.rest4j.generator.JavaGeneratorTest.java

@Test
public void testJavaClient() throws Exception {
    gen.setStylesheet("com/rest4j/client/java.xslt");
    new File("target/java").mkdir();
    gen.setApiXmlUrl(getClass().getResource("doc-generator-graph.xml"));
    gen.setOutputDir("target/java");
    gen.addParam(new TemplateParam("common-params", "access-token"));
    gen.addParam(new TemplateParam("additional-client-code", "// ADDITIONAL CODE"));
    gen.generate();/*www  .j av a  2  s  .c  o  m*/

    // let's try compiling the damn thing
    CommandLine cmdLine = CommandLine.parse("mvn package");
    DefaultExecutor executor = new DefaultExecutor();
    executor.setWorkingDirectory(new File("target/java"));
    ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
    executor.setWatchdog(watchdog);
    int exitValue = executor.execute(cmdLine);
    assertFalse(executor.isFailure(exitValue));

    // check doc comments in the file A.java
    String a = IOUtils.toString(new File("target/java/src/main/java/api/model/A.java").toURI());
    assertTrue(a, a.contains("Some additional client info"));
    assertFalse(a, a.contains("Some additional python client info"));

    // check existence of Parameter Object class
    a = IOUtils.toString(new File("target/java/src/main/java/api/model/PatchBRequest.java").toURI());
    assertTrue(a, a.contains("class PatchBRequest"));

    // check some file paths
    assertTrue(new File("target/java/src/main/java/api/util/JsonUtil.java").canRead());
    assertTrue(new File("target/java/src/main/java/api/Request.java").canRead());

    String client = IOUtils.toString(new File("target/java/src/main/java/api/Client.java").toURI());
    assertTrue(client.contains("ADDITIONAL CODE"));
}

From source file:hoot.services.nativeinterfaces.CommandRunnerImpl.java

@Override
public CommandResult exec(String[] command) throws IOException {
    logger.debug("Executing the following command: {}", Arrays.toString(command));

    try (OutputStream stdout = new ByteArrayOutputStream(); OutputStream stderr = new ByteArrayOutputStream()) {

        this.stdout = stdout;
        this.stderr = stderr;

        CommandLine cmdLine = new CommandLine(command[0]);
        for (int i = 1; i < command.length; i++) {
            cmdLine.addArgument(command[i], false);
        }/*from  w ww.  j av a  2s  . co m*/

        ExecuteStreamHandler executeStreamHandler = new PumpStreamHandler(stdout, stderr);
        DefaultExecutor executor = new DefaultExecutor();
        this.watchDog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT);
        executor.setWatchdog(this.watchDog);
        executor.setStreamHandler(executeStreamHandler);

        int exitValue;
        try {
            exitValue = executor.execute(cmdLine);

            if (executor.isFailure(exitValue) && this.watchDog.killedProcess()) {
                // it was killed on purpose by the watchdog
                logger.info("Process for '{}' command was killed!", cmdLine);
            }
        } catch (Exception e) {
            exitValue = -1;
            logger.warn("Error executing: {}", cmdLine, e);
        }

        CommandResult commandResult = new CommandResult(cmdLine.toString(), exitValue, stdout.toString(),
                stderr.toString());

        logger.debug("Finished executing: {}", commandResult);

        return commandResult;
    }
}

From source file:com.sonar.it.jenkins.orchestrator.container.JenkinsWrapper.java

@VisibleForTesting
DefaultExecutor createExecutor(ExecuteWatchdog watchDog) {
    DefaultExecutor newExecutor = new DefaultExecutor();
    newExecutor.setWatchdog(watchDog);
    newExecutor.setProcessDestroyer(new ShutdownHookProcessDestroyer());
    return newExecutor;
}

From source file:de.slackspace.wfail2ban.firewall.impl.DefaultFirewallManager.java

private void deleteFirewallRule(int ruleNumber, String filterName) {
    CommandLine cmdLine = new CommandLine("cmd.exe");
    cmdLine.addArgument("/C");
    cmdLine.addArgument(System.getenv("WINDIR") + "\\system32\\netsh.exe");
    cmdLine.addArgument("advfirewall");
    cmdLine.addArgument("firewall");
    cmdLine.addArgument("delete");
    cmdLine.addArgument("rule");
    cmdLine.addArgument(createFinalRuleName(ruleNumber, filterName));
    DefaultExecutor executor = new DefaultExecutor();
    ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
    executor.setWatchdog(watchdog);
    try {// w  w w  . j  ava2s  .com
        executor.execute(cmdLine);
        if (logger.isDebugEnabled()) {
            logger.debug("Deleted firewall rule " + createFinalRuleName(ruleNumber, filterName));
        }
    } catch (ExecuteException e) {
        logger.error("Could not delete firewall rule. Error was: ", e);
    } catch (IOException e) {
        logger.error("Could not delete firewall rule. Error was: ", e);
    }
}

From source file:de.slackspace.wfail2ban.firewall.impl.DefaultFirewallManager.java

private boolean checkFirewallRuleExists(int ruleNumber, String filterName) {
    CommandLine cmdLine = new CommandLine("cmd.exe");
    cmdLine.addArgument("/C");
    cmdLine.addArgument(System.getenv("WINDIR") + "\\system32\\netsh.exe");
    cmdLine.addArgument("advfirewall");
    cmdLine.addArgument("firewall");
    cmdLine.addArgument("show");
    cmdLine.addArgument("rule");
    cmdLine.addArgument(createFinalRuleName(ruleNumber, filterName));

    DefaultExecutor executor = new DefaultExecutor();
    ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
    executor.setWatchdog(watchdog);
    try {/*from   w  ww  . j  av a2  s.  c om*/
        executor.execute(cmdLine);
        return true;
    } catch (ExecuteException e) {
        //rule does not exist
        return false;
    } catch (IOException e) {
        logger.error("Could not list firewall rule. Error was: ", e);
    }
    return false;
}

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  v  a 2 s  . c  o m*/
        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:de.slackspace.wfail2ban.firewall.impl.DefaultFirewallManager.java

private void addDefaultWindowsFirewallRule(int ruleNumber, String filterName, String ipList) {
    Map<Object, Object> map = new HashMap<Object, Object>();
    map.put("name", createFinalRuleName(ruleNumber, filterName));
    map.put("direction", "in");
    map.put("ipList", ipList);
    CommandLine cmdLine = new CommandLine("cmd.exe");
    cmdLine.addArgument("/C");
    cmdLine.addArgument(System.getenv("WINDIR") + "\\system32\\netsh.exe");
    cmdLine.addArgument("advfirewall");
    cmdLine.addArgument("firewall");
    cmdLine.addArgument("add");
    cmdLine.addArgument("rule");
    cmdLine.addArgument("name=${name}");
    cmdLine.addArgument("dir=${direction}");
    cmdLine.addArgument("action=block");
    cmdLine.addArgument("localip=any");
    cmdLine.addArgument("remoteip=${ipList}");
    cmdLine.addArgument("description=This is a generated rule from wfail2ban. Do not edit!");
    cmdLine.addArgument("profile=any");
    cmdLine.addArgument("interfacetype=any");
    cmdLine.setSubstitutionMap(map);/*from  ww  w  .  ja  va  2s .  com*/
    DefaultExecutor executor = new DefaultExecutor();
    ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
    executor.setWatchdog(watchdog);
    try {
        executor.execute(cmdLine);
        if (logger.isDebugEnabled()) {
            logger.debug("Added firewall rule " + createFinalRuleName(ruleNumber, filterName));
        }
    } catch (ExecuteException e) {
        ConsolePrinter.printError("Could not create firewall rule. Continuing with next one...");
        logger.error("", e);
    } catch (IOException e) {
        logger.error("Could not create firewall rule. Error was: ", e);
    }
}