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

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

Introduction

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

Prototype

public ExecuteWatchdog(final long timeout) 

Source Link

Document

Creates a new watchdog with a given timeout.

Usage

From source file:com.taobao.ad.es.common.job.executor.ShellHttpJobExecutor.java

@Override
public JobResult execute(JobData jobData) throws IOException {
    JobResult jobResult = JobResult.succcessResult();
    CommandLine cmdLine = CommandLine.parse(jobData.getData().get(JobData.JOBDATA_DATA_JOBCOMMAND));
    Executor executor = new DefaultExecutor();
    ExecuteWatchdog watchdog = new ExecuteWatchdog(1200000);
    executor.setExitValue(0);/*from   www .j  av a2  s. c o  m*/
    executor.setWatchdog(watchdog);
    int exitValue = -1;
    try {
        exitValue = executor.execute(cmdLine);
    } catch (ExecuteException e) {
        exitValue = e.getExitValue();
    }
    if (exitValue != 0) {
        jobResult = JobResult.errorResult(JobResult.RESULTCODE_OTHER_ERR,
                "Shell?");
        jobResult.setResultCode(exitValue);
        return jobResult;
    }
    jobResult.setResultCode(exitValue);
    return jobResult;
}

From source file:com.technofovea.packbsp.packaging.ExternalController.java

public ExternalController() {
    executor.setExitValue(SUCCESSFUL_EXIT);
    watchdog = new ExecuteWatchdog(WATCHDOG_TIME);
    executor.setWatchdog(watchdog);/*from  w w w  . j av a  2 s. c om*/
    environment = new HashMap<String, String>(System.getenv());
}

From source file:com.boulmier.machinelearning.jobexecutor.job.Job.java

public void start() throws IOException {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final DefaultExecutor exec = new DefaultExecutor();
    final ExecuteWatchdog wd = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT);
    final PumpStreamHandler output = new PumpStreamHandler(out);
    final DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler();

    exec.setWatchdog(wd);//w ww . j  a va  2  s.c  om
    exec.setStreamHandler(output);
    exec.execute(cl, handler);
    JobExecutor.logger.info("Running job " + jobid);

    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                handler.waitFor();
                Computer.ComputeProperties properties = Computer.ComputeProperties.buildFromRequest(req);
                new SenderComputer(new StorageComputer(out.toString(), properties)).compute();
                JobExecutor.logger.info("Job complete " + jobid);
            } catch (InterruptedException ex) {
                exec.getWatchdog().destroyProcess();
                JobExecutor.logger.error(
                        "Job (" + jobid + ") has been destroyed due to internal error " + ex.getMessage());
            }
        }

    }).start();

}

From source file:io.github.binout.wordpress2html.writer.Html2AsciidocConverter.java

private void execute(CommandLine cmdLine) throws IOException {
    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(0);// w w  w. jav a 2s  .  c om
    ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
    executor.setWatchdog(watchdog);
    int exitValue = executor.execute(cmdLine);
    if (exitValue != 0) {
        throw new RuntimeException("Pandoc is not installed !");
    }
}

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

public void execute() throws MojoExecutionException {

    String line = getPlay2().getAbsolutePath();

    CommandLine cmdLine = CommandLine.parse(line);
    cmdLine.addArgument("clean");
    DefaultExecutor executor = new DefaultExecutor();

    if (timeout > 0) {
        ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
        executor.setWatchdog(watchdog);/*from ww w.  ja  v a2s  . co  m*/
    }

    executor.setWorkingDirectory(project.getBasedir());
    executor.setExitValue(0);
    try {
        executor.execute(cmdLine, getEnvironment());
    } catch (IOException e) {
        throw new MojoExecutionException("Error during cleanup", e);
    }

    // Also delete the dist directory
    File dist = new File(project.getBasedir(), "dist");
    if (dist.exists()) {
        getLog().debug("Deleting " + dist.getAbsolutePath());
        try {
            FileUtils.deleteDirectory(dist);
        } catch (IOException e) {
            throw new MojoExecutionException("Can't delete the dist folder", e);
        }
    } else {
        getLog().debug("'dist' directory not found");
    }

    // Delete the log folder
    File logs = new File(project.getBasedir(), "logs");
    if (logs.exists()) {
        getLog().debug("Deleting " + logs.getAbsolutePath());
        try {
            FileUtils.deleteDirectory(logs);
        } catch (IOException e) {
            throw new MojoExecutionException("Can't delete the logs folder", e);
        }
    } else {
        getLog().debug("'logs' directory not found");
    }

    // Also delete the lib directory if set
    if (cleanLibFolder) {
        File lib = new File(project.getBasedir(), "lib");
        if (lib.exists()) {
            getLog().debug("Deleting " + lib.getAbsolutePath());
            try {
                FileUtils.deleteDirectory(lib);
            } catch (IOException e) {
                throw new MojoExecutionException("Can't delete the " + lib + " folder", e);
            }
        } else {
            getLog().debug("'" + lib + "' directory not found");
        }
    }
}

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

public void execute() throws MojoExecutionException {

    String line = getPlay2().getAbsolutePath();

    CommandLine cmdLine = CommandLine.parse(line);
    cmdLine.addArgument("compile");
    DefaultExecutor executor = new DefaultExecutor();

    if (timeout > 0) {
        ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
        executor.setWatchdog(watchdog);/* ww  w  .ja  v  a2s.  c  o m*/
    }

    executor.setExitValue(0);
    executor.setWorkingDirectory(project.getBasedir());
    try {
        executor.execute(cmdLine, getEnvironment());
    } catch (IOException e) {
        throw new MojoExecutionException("Error during compilation", e);
    }
}

From source file:io.rhiot.utils.process.ExecProcessManager.java

@Override
public List<String> executeAndJoinOutput(String... command) {

    CommandLine cmdLine = CommandLine.parse(String.join(" ", command));
    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(0);/* w  w  w  .java 2  s .co m*/
    ExecResultHandler resultHandler = null;

    if (getTimeout() > 0) {
        ExecuteWatchdog watchdog = new ExecuteWatchdog(getTimeout());
        executor.setWatchdog(watchdog);
        resultHandler = new ExecResultHandler(watchdog);
    }
    try {
        CollectingLogOutputStream outAndErr = new CollectingLogOutputStream();
        executor.setStreamHandler(new PumpStreamHandler(outAndErr));
        if (resultHandler != null) {
            executor.execute(cmdLine, resultHandler);
        } else {
            executor.execute(cmdLine);
        }
        resultHandler.waitFor();
        return outAndErr.getLines();

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.technofovea.packbsp.WindowsRegistryChecker.java

public WindowsRegistryChecker() {
    executor.setExitValue(SUCCESSFUL_EXIT);
    watchdog = new ExecuteWatchdog(WATCHDOG_TIME);
    executor.setWatchdog(watchdog);//from   w  ww  .j  a  va2 s  . c  o  m
    environment = new HashMap<String, String>(System.getenv());
    cmd = CommandLine.parse("reg.exe");
    cmd.addArgument("query");
    cmd.addArgument("${" + KEY_PATH + "}");
    cmd.addArgument("/v");
    cmd.addArgument("${" + KEY_VALUE_NAME + "}");
}

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 ava 2  s  . co  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.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();//from  w w  w  .j av a2  s .com

    // 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"));
}