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()));
}
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);
}
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);
}
}
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);
}
}
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;
}
Aggregations