/** * Stopping a pumper thread. The implementation actually waits * longer than specified in 'timeout' to detect if the timeout * was indeed exceeded. If the timeout was exceeded an IOException * is created to be thrown to the caller. * * @param thread the thread to be stopped * @param timeout the time in ms to wait to join */ protected void stopThread(final Thread thread, final long timeout) { if (thread != null) { try { if (timeout == 0) { thread.join(); } else { final long timeToWait = timeout + STOP_TIMEOUT_ADDITION; final long startTime = System.currentTimeMillis(); thread.join(timeToWait); if (!(System.currentTimeMillis() < startTime + timeToWait)) { final String msg = "The stop timeout of " + timeout + " ms was exceeded"; caught = new ExecuteException(msg, Executor.INVALID_EXITVALUE); } } } catch (final InterruptedException e) { thread.interrupt(); } } }
/** * @see org.apache.commons.exec.Executor#execute(CommandLine, * java.util.Map, org.apache.commons.exec.ExecuteResultHandler) */ public void execute(final CommandLine command, final Map<String, String> environment, final ExecuteResultHandler handler) throws ExecuteException, IOException { if (workingDirectory != null && !workingDirectory.exists()) { throw new IOException(workingDirectory + " doesn't exist."); } if (watchdog != null) { watchdog.setProcessNotStarted(); } final Runnable runnable = new Runnable() { public void run() { int exitValue = Executor.INVALID_EXITVALUE; try { exitValue = executeInternal(command, environment, workingDirectory, streamHandler); handler.onProcessComplete(exitValue); } catch (final ExecuteException e) { handler.onProcessFailed(e); } catch (final Exception e) { handler.onProcessFailed(new ExecuteException("Execution failed", exitValue, e)); } } }; this.executorThread = createThread(runnable, "Exec Default Executor"); getExecutorThread().start(); }
public void run() { int exitValue = Executor.INVALID_EXITVALUE; try { exitValue = executeInternal(command, environment, workingDirectory, streamHandler); handler.onProcessComplete(exitValue); } catch (final ExecuteException e) { handler.onProcessFailed(e); } catch (final Exception e) { handler.onProcessFailed(new ExecuteException("Execution failed", exitValue, e)); } } };
throw new ExecuteException("Process exited with an error: " + exitValue, exitValue);
private static void execute(CommandLine cmdLine, File dir, DefaultExecutor executor, Map<String,String> environment) throws IOException { log.info("-Executing(" + dir + "): " + cmdLine); try { int exitValue = executor.execute(cmdLine, environment); if (exitValue != 0) { log.info("Had exit code " + exitValue + " when calling " + cmdLine); } } catch (ExecuteException e) { throw new ExecuteException("While executing (" + dir + "); " + cmdLine, e.getExitValue(), e); } } }
/** * @VisibleForTesting */ void doProcess(final InputStream in, final OutputStream out) throws ExecuteException, IOException { final ByteArrayOutputStream errorStream = new ByteArrayOutputStream(); final Executor executor = new DefaultExecutor(); executor.setStreamHandler(new PumpStreamHandler(out, errorStream, in)); final int result = executor.execute(CommandLine.parse(NGMIN_COMMAND)); LOG.debug("result={}", result); if (result != 0) { throw new ExecuteException("Processing failed: " + new String(errorStream.toByteArray()), result); } }
/** * @VisibleForTesting */ void doProcess(final InputStream in, final OutputStream out) throws ExecuteException, IOException { final ByteArrayOutputStream errorStream = new ByteArrayOutputStream(); final Executor executor = new DefaultExecutor(); executor.setStreamHandler(new PumpStreamHandler(out, errorStream, in)); final int result = executor.execute(CommandLine.parse(NGMIN_COMMAND)); LOG.debug("result={}", result); if (result != 0) { throw new ExecuteException("Processing failed: " + new String(errorStream.toByteArray()), result); } }
@Override public String handle(Task aTask) throws Exception { File scriptFile = File.createTempFile("_script", ".sh"); File logFile = File.createTempFile("log", null); FileUtils.writeStringToFile(scriptFile, aTask.getRequiredString("script")); try (PrintStream stream = new PrintStream(logFile);) { Process chmod = Runtime.getRuntime().exec(String.format("chmod u+x %s",scriptFile.getAbsolutePath())); int chmodRetCode = chmod.waitFor(); if(chmodRetCode != 0) { throw new ExecuteException("Failed to chmod", chmodRetCode); } CommandLine cmd = new CommandLine (scriptFile.getAbsolutePath()); logger.debug("{}",cmd); DefaultExecutor exec = new DefaultExecutor(); exec.setStreamHandler(new PumpStreamHandler(stream)); exec.execute(cmd); return FileUtils.readFileToString(logFile); } catch (ExecuteException e) { throw new ExecuteException(e.getMessage(),e.getExitValue(), new RuntimeException(FileUtils.readFileToString(logFile))); } finally { FileUtils.deleteQuietly(logFile); FileUtils.deleteQuietly(scriptFile); } }
/** * @VisibleForTesting */ void doProcess(final InputStream in, final OutputStream out) throws IOException { final ByteArrayOutputStream errorStream = new ByteArrayOutputStream(); final Executor executor = new DefaultExecutor(); executor.setStreamHandler(new PumpStreamHandler(out, errorStream, in)); final int result = executor.execute(CommandLine.parse(NGANN_COMMAND)); LOG.debug("result={}", result); if (result != 0) { throw new ExecuteException("Processing failed: " + new String(errorStream.toByteArray()), result); } }
@Override public Object handle(Task aTask) throws Exception { List<String> options = aTask.getList("options", String.class); CommandLine cmd = new CommandLine ("ffmpeg"); options.forEach(o->cmd.addArgument(o)); log.debug("{}",cmd); DefaultExecutor exec = new DefaultExecutor(); File tempFile = File.createTempFile("log", null); try (PrintStream stream = new PrintStream(tempFile);) { exec.setStreamHandler(new PumpStreamHandler(stream)); int exitValue = exec.execute(cmd); return exitValue!=0?FileUtils.readFileToString(tempFile):cmd.toString(); } catch (ExecuteException e) { throw new ExecuteException(e.getMessage(),e.getExitValue(), new RuntimeException(FileUtils.readFileToString(tempFile))); } finally { FileUtils.deleteQuietly(tempFile); } }
@Override public Map<String,Object> handle (Task aTask) throws Exception { CommandLine cmd = new CommandLine ("mediainfo"); cmd.addArgument(aTask.getRequiredString("input")); log.debug("{}",cmd); DefaultExecutor exec = new DefaultExecutor(); File tempFile = File.createTempFile("log", null); try (PrintStream stream = new PrintStream(tempFile);) { exec.setStreamHandler(new PumpStreamHandler(stream)); exec.execute(cmd); return parse(FileUtils.readFileToString(tempFile)); } catch (ExecuteException e) { throw new ExecuteException(e.getMessage(),e.getExitValue(), new RuntimeException(FileUtils.readFileToString(tempFile))); } finally { FileUtils.deleteQuietly(tempFile); } }
throw new ExecuteException("problems running command: " + command, resultHandler.getExitValue());
@Override public Map<String,Object> handle(Task aTask) throws Exception { CommandLine cmd = new CommandLine ("ffprobe"); cmd.addArgument("-v") .addArgument("quiet") .addArgument("-print_format") .addArgument("json") .addArgument("-show_error") .addArgument("-show_format") .addArgument("-show_streams") .addArgument(aTask.getRequiredString("input")); log.debug("{}",cmd); DefaultExecutor exec = new DefaultExecutor(); File tempFile = File.createTempFile("log", null); try (PrintStream stream = new PrintStream(tempFile);) { exec.setStreamHandler(new PumpStreamHandler(stream)); exec.execute(cmd); return parse(FileUtils.readFileToString(tempFile)); } catch (ExecuteException e) { throw new ExecuteException(e.getMessage(),e.getExitValue(), new RuntimeException(FileUtils.readFileToString(tempFile))); } finally { FileUtils.deleteQuietly(tempFile); } }
private static void internalExecute(CommandLine cmdLine, OutputStream outputStream, File workingDirectory) throws IOException { Executor executor = getExecutor(); ExecuteWatchdog watchdog = getExecuteWatchdog(); executor.setWatchdog(watchdog); if (workingDirectory != null) { executor.setWorkingDirectory(workingDirectory); } // set allowed exit values (0 is actually the default) executor.setExitValue(0); PumpStreamHandler psh = new PumpStreamHandler(outputStream); executor.setStreamHandler(psh); executor.execute(cmdLine); if (watchdog.killedProcess()) { throw new ExecuteException(String.format("Watchdog killed Sencha Cmd process after %s ms.", MAX_EXECUTION_TIME), 0); } }