Search in sources :

Example 21 with PumpStreamHandler

use of org.apache.commons.exec.PumpStreamHandler in project ddf by codice.

the class VideoThumbnailPlugin method getVideoDuration.

private Duration getVideoDuration(final String videoFilePath) throws IOException, InterruptedException {
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    final PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
    final CommandLine command = getFFmpegInfoCommand(videoFilePath);
    final DefaultExecuteResultHandler resultHandler = executeFFmpeg(command, 15, streamHandler);
    resultHandler.waitFor();
    return parseVideoDuration(outputStream.toString(StandardCharsets.UTF_8.name()));
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) DefaultExecuteResultHandler(org.apache.commons.exec.DefaultExecuteResultHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 22 with PumpStreamHandler

use of org.apache.commons.exec.PumpStreamHandler in project stanbol by apache.

the class JarExecutor method start.

/**
     * Start the jar if not done yet, and setup runtime hook to stop it.
     */
public void start() throws Exception {
    final ExecuteResultHandler h = new ExecuteResultHandler() {

        @Override
        public void onProcessFailed(ExecuteException ex) {
            log.error("Process execution failed:" + ex, ex);
        }

        @Override
        public void onProcessComplete(int result) {
            log.info("Process execution complete, exit code=" + result);
        }
    };
    final String vmOptions = System.getProperty("jar.executor.vm.options");
    final Executor e = new DefaultExecutor();
    if (this.workingDirectory != null) {
        e.setWorkingDirectory(this.workingDirectory);
    }
    final CommandLine cl = new CommandLine(javaExecutable);
    if (vmOptions != null && vmOptions.length() > 0) {
        // not the case for common usage patterns
        for (String option : StringUtils.split(vmOptions, " ")) {
            cl.addArgument(option);
        }
    }
    cl.addArgument("-jar");
    cl.addArgument(jarToExecute.getAbsolutePath());
    cl.addArgument("-p");
    cl.addArgument(String.valueOf(serverPort));
    log.info("Executing " + cl);
    e.setStreamHandler(new PumpStreamHandler());
    e.setProcessDestroyer(new ShutdownHookProcessDestroyer());
    e.execute(cl, h);
}
Also used : ExecuteResultHandler(org.apache.commons.exec.ExecuteResultHandler) CommandLine(org.apache.commons.exec.CommandLine) Executor(org.apache.commons.exec.Executor) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteException(org.apache.commons.exec.ExecuteException) ShutdownHookProcessDestroyer(org.apache.commons.exec.ShutdownHookProcessDestroyer)

Example 23 with PumpStreamHandler

use of org.apache.commons.exec.PumpStreamHandler in project sling by apache.

the class JarExecutor method start.

/** Start the jar if not done yet, and setup runtime hook
     *  to stop it.
     */
public void start() throws Exception {
    final ExecuteResultHandler h = new ExecuteResultHandler() {

        public void onProcessFailed(ExecuteException ex) {
            log.error("Process execution failed:" + ex, ex);
        }

        public void onProcessComplete(int result) {
            log.info("Process execution complete, exit code=" + result);
        }
    };
    final String vmOptions = config.getProperty(PROP_VM_OPTIONS);
    executor = new DefaultExecutor();
    final CommandLine cl = new CommandLine(jvmFullPath);
    if (vmOptions != null && vmOptions.length() > 0) {
        cl.addArguments(vmOptions);
    }
    cl.addArgument("-jar");
    cl.addArgument(jarToExecute.getAbsolutePath());
    // Additional options for the jar that's executed.
    // $JAREXEC_SERVER_PORT$ is replaced our serverPort value
    String jarOptions = config.getProperty(PROP_JAR_OPTIONS);
    if (jarOptions != null && jarOptions.length() > 0) {
        jarOptions = jarOptions.replaceAll("\\$JAREXEC_SERVER_PORT\\$", String.valueOf(serverPort));
        log.info("Executable jar options: {}", jarOptions);
        cl.addArguments(jarOptions);
    }
    final String workFolderOption = config.getProperty(PROP_WORK_FOLDER);
    if (workFolderOption != null && workFolderOption.length() > 0) {
        final File workFolder = new File(workFolderOption);
        if (!workFolder.isDirectory()) {
            throw new IOException("Work dir set by " + PROP_WORK_FOLDER + " option does not exist: " + workFolder.getAbsolutePath());
        }
        log.info("Setting working directory for executable jar: {}", workFolder.getAbsolutePath());
        executor.setWorkingDirectory(workFolder);
    }
    String tmStr = config.getProperty(PROP_EXIT_TIMEOUT_SECONDS);
    final int exitTimeoutSeconds = tmStr == null ? DEFAULT_EXIT_TIMEOUT : Integer.valueOf(tmStr);
    if ("true".equals(config.getProperty(PROP_SYNC_EXEC, ""))) {
        final long start = System.currentTimeMillis();
        log.info("Executing and waiting for result: " + cl);
        final int result = executor.execute(cl);
        final int expected = Integer.valueOf(config.getProperty(PROP_SYNC_EXEC_EXPECTED, "0"));
        log.info("Execution took " + (System.currentTimeMillis() - start) + " msec");
        if (result != expected) {
            throw new ExecutorException("Expected result code " + expected + ", got " + result);
        }
    } else {
        log.info("Executing asynchronously: " + cl);
        executor.setStreamHandler(new PumpStreamHandler());
        final ShutdownHookSingleProcessDestroyer pd = new ShutdownHookSingleProcessDestroyer("java -jar " + jarToExecute.getName(), exitTimeoutSeconds);
        final boolean waitOnShutdown = Boolean.valueOf(config.getProperty(PROP_WAIT_ONSHUTDOWN, "false"));
        log.info("Setting up ProcessDestroyer with waitOnShutdown=" + waitOnShutdown);
        pd.setWaitOnShutdown(waitOnShutdown);
        executor.setProcessDestroyer(pd);
        executor.execute(cl, h);
    }
}
Also used : ExecuteResultHandler(org.apache.commons.exec.ExecuteResultHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) IOException(java.io.IOException) CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) ExecuteException(org.apache.commons.exec.ExecuteException) File(java.io.File)

Example 24 with PumpStreamHandler

use of org.apache.commons.exec.PumpStreamHandler in project sling by apache.

the class JarExecutor method start.

/** Start the jar if not done yet, and setup runtime hook
     *  to stop it.
     */
public void start() throws Exception {
    final ExecuteResultHandler h = new ExecuteResultHandler() {

        public void onProcessFailed(ExecuteException ex) {
            log.error("Process execution failed:" + ex, ex);
        }

        public void onProcessComplete(int result) {
            log.info("Process execution complete, exit code=" + result);
        }
    };
    final String vmOptions = config.getProperty(PROP_VM_OPTIONS);
    executor = new DefaultExecutor();
    final CommandLine cl = new CommandLine(jvmFullPath);
    if (vmOptions != null && vmOptions.length() > 0) {
        cl.addArguments(vmOptions);
    }
    cl.addArgument("-jar");
    cl.addArgument(jarToExecute.getAbsolutePath());
    // Additional options for the jar that's executed.
    // $JAREXEC_SERVER_PORT$ is replaced our serverPort value
    String jarOptions = config.getProperty(PROP_JAR_OPTIONS);
    if (jarOptions != null && jarOptions.length() > 0) {
        jarOptions = jarOptions.replaceAll("\\$JAREXEC_SERVER_PORT\\$", String.valueOf(serverPort));
        log.info("Executable jar options: {}", jarOptions);
        cl.addArguments(jarOptions);
    }
    final String workFolderOption = config.getProperty(PROP_WORK_FOLDER);
    if (workFolderOption != null && workFolderOption.length() > 0) {
        final File workFolder = new File(workFolderOption);
        if (!workFolder.isDirectory()) {
            throw new IOException("Work dir set by " + PROP_WORK_FOLDER + " option does not exist: " + workFolder.getAbsolutePath());
        }
        log.info("Setting working directory for executable jar: {}", workFolder.getAbsolutePath());
        executor.setWorkingDirectory(workFolder);
    }
    String tmStr = config.getProperty(PROP_EXIT_TIMEOUT_SECONDS);
    final int exitTimeoutSeconds = tmStr == null ? DEFAULT_EXIT_TIMEOUT : Integer.valueOf(tmStr);
    if ("true".equals(config.getProperty(PROP_SYNC_EXEC, ""))) {
        final long start = System.currentTimeMillis();
        log.info("Executing and waiting for result: " + cl);
        final int result = executor.execute(cl);
        final int expected = Integer.valueOf(config.getProperty(PROP_SYNC_EXEC_EXPECTED, "0"));
        log.info("Execution took " + (System.currentTimeMillis() - start) + " msec");
        if (result != expected) {
            throw new ExecutorException("Expected result code " + expected + ", got " + result);
        }
    } else {
        log.info("Executing asynchronously: " + cl);
        executor.setStreamHandler(new PumpStreamHandler());
        final ShutdownHookSingleProcessDestroyer pd = new ShutdownHookSingleProcessDestroyer("java -jar " + jarToExecute.getName(), exitTimeoutSeconds);
        final boolean waitOnShutdown = Boolean.valueOf(config.getProperty(PROP_WAIT_ONSHUTDOWN, "false"));
        log.info("Setting up ProcessDestroyer with waitOnShutdown=" + waitOnShutdown);
        pd.setWaitOnShutdown(waitOnShutdown);
        executor.setProcessDestroyer(pd);
        executor.execute(cl, h);
    }
}
Also used : ExecuteResultHandler(org.apache.commons.exec.ExecuteResultHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) IOException(java.io.IOException) CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) ExecuteException(org.apache.commons.exec.ExecuteException) File(java.io.File)

Example 25 with PumpStreamHandler

use of org.apache.commons.exec.PumpStreamHandler in project project-build-plugin by axonivy.

the class EngineControl method createEngineLogStreamForwarder.

private PumpStreamHandler createEngineLogStreamForwarder(Consumer<String> logLineHandler) throws FileNotFoundException {
    OutputStream output = getEngineLogTarget();
    OutputStream engineLogStream = new LineOrientedOutputStreamRedirector(output) {

        @Override
        protected void processLine(byte[] b) throws IOException {
            // write file log
            super.processLine(b);
            String line = new String(b);
            context.log.debug("engine: " + line);
            if (logLineHandler != null) {
                logLineHandler.accept(line);
            }
        }
    };
    PumpStreamHandler streamHandler = new PumpStreamHandler(engineLogStream, System.err) {

        @Override
        public void stop() throws IOException {
            super.stop();
            // we opened the stream - we're responsible to close it!
            engineLogStream.close();
        }
    };
    return streamHandler;
}
Also used : PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) LineOrientedOutputStreamRedirector(ch.ivyteam.ivy.maven.util.stream.LineOrientedOutputStreamRedirector)

Aggregations

PumpStreamHandler (org.apache.commons.exec.PumpStreamHandler)25 DefaultExecutor (org.apache.commons.exec.DefaultExecutor)20 CommandLine (org.apache.commons.exec.CommandLine)19 ByteArrayOutputStream (java.io.ByteArrayOutputStream)17 IOException (java.io.IOException)16 ExecuteException (org.apache.commons.exec.ExecuteException)11 ExecuteWatchdog (org.apache.commons.exec.ExecuteWatchdog)10 File (java.io.File)5 InputStream (java.io.InputStream)4 PipedInputStream (java.io.PipedInputStream)4 PipedOutputStream (java.io.PipedOutputStream)4 OutputStream (java.io.OutputStream)3 OutputStreamWriter (java.io.OutputStreamWriter)3 Map (java.util.Map)3 ExecuteResultHandler (org.apache.commons.exec.ExecuteResultHandler)3 ExecuteStreamHandler (org.apache.commons.exec.ExecuteStreamHandler)3 Executor (org.apache.commons.exec.Executor)3 BufferedWriter (java.io.BufferedWriter)2 DataInputStream (java.io.DataInputStream)2 FileOutputStream (java.io.FileOutputStream)2