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

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

Introduction

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

Prototype

ExecuteStreamHandler

Source Link

Usage

From source file:net.tkausl.RunMojo.java

public void execute() throws MojoExecutionException, MojoFailureException {
    File plugins = pluginfolder == null ? new File(serverPath, "plugins") : new File(serverPath, pluginfolder);
    if (!plugins.exists())
        plugins.mkdirs();//from w w  w  .  j a v  a2 s. co  m

    File pluginFile = new File(plugins, fileName + ".dev");
    if (!pluginFile.exists()) {
        try {
            PrintWriter w = new PrintWriter(pluginFile);
            w.print(outputDir);
            w.close();
        } catch (FileNotFoundException ex) {
            throw new MojoExecutionException("Could not write .dev-file");
        }
    }
    Executor ex = new DefaultExecutor();
    CommandLine commandLine = CommandLine.parse("java");
    String execArgs = System.getProperty("exec.args");
    if (execArgs != null && execArgs.trim().length() > 0) {
        commandLine.addArguments(execArgs.split(" "));
    }
    commandLine.addArguments(new String[] { "-jar", serverJar });

    if (pluginfolder != null)
        commandLine.addArguments(new String[] { "--plugins", pluginfolder });

    ex.setWorkingDirectory(serverPath);
    //PumpStreamHandler psh = new PumpStreamHandler(System.out, System.err, System.in);
    //ex.setStreamHandler(psh);
    ex.setStreamHandler(new ExecuteStreamHandler() {
        private PumpStreamHandler psh = new PumpStreamHandler(System.out, System.err);
        InputStreamPumper isp;
        Thread ispt;

        public void setProcessInputStream(OutputStream os) throws IOException {
            isp = new InputStreamPumper(System.in, os);
        }

        public void setProcessErrorStream(InputStream is) throws IOException {
            psh.setProcessErrorStream(is);
        }

        public void setProcessOutputStream(InputStream is) throws IOException {
            psh.setProcessOutputStream(is);
        }

        public void start() throws IOException {
            if (isp != null) {
                ispt = new Thread(isp);
                ispt.setDaemon(true);
                ispt.start();
            }
            psh.start();
        }

        public void stop() throws IOException {
            if (ispt != null)
                ispt.interrupt();
            psh.stop();
        }
    });
    try {
        ex.execute(commandLine);
    } catch (IOException ex1) {
        throw new MojoExecutionException("Error in Execution");
    }
}

From source file:org.jfastcgi.client.FastCGIHandler.java

public void startProcess(final String cmd) throws IOException {
    final DefaultExecutor pe = new DefaultExecutor();
    processExecutor = pe;//from w w  w  .  j av  a 2 s. c  o  m
    pe.setWatchdog(new ExecuteWatchdog(60000));

    processExecutor.setStreamHandler(new ExecuteStreamHandler() {

        private final Set<StreamLogger> loggers = new HashSet<StreamLogger>();

        public void stop() throws IOException {

        }

        public void start() throws IOException {

        }

        public void setProcessOutputStream(final InputStream is) throws IOException {
            loggers.add(new StreamLogger(is,
                    LoggerFactory.getLogger(FastCGIHandler.class.getName() + ".externalprocess.stdout")));
        }

        public void setProcessInputStream(final OutputStream os) throws IOException {

        }

        public void setProcessErrorStream(final InputStream is) throws IOException {
            loggers.add(new StreamLogger(is,
                    LoggerFactory.getLogger(FastCGIHandler.class.getName() + ".externalprocess.stderr")));
        }
    });

    getLog().info("Starting external process : " + cmd);
    pe.execute(CommandLine.parse(cmd), new DefaultExecuteResultHandler() {
        @Override
        public void onProcessFailed(final ExecuteException e) {
            super.onProcessFailed(e);
            getLog().error("while running process", e);
        }

        @Override
        public void onProcessComplete(final int exitValue) {
            getLog().info(String.format("external process exited with code %s : %s", exitValue, cmd));
        }
    });

}