List of usage examples for org.apache.commons.exec PumpStreamHandler stop
public void stop() throws IOException
From source file:org.waarp.openr66.context.task.ExecOutputTask.java
@Override public void run() { /*/*from w w w . j a va2 s . c o m*/ * First apply all replacements and format to argRule from context and argTransfer. Will * call exec (from first element of resulting string) with arguments as the following value * from the replacements. Return 0 if OK, else 1 for a warning else as an error. In case of * an error (> 0), all the line from output will be send back to the partner with the Error * code. No change is made to the file. */ logger.info("ExecOutput with " + argRule + ":" + argTransfer + " and {}", session); String finalname = argRule; finalname = getReplacedValue(finalname, argTransfer.split(" ")); // Force the WaitForValidation waitForValidation = true; if (Configuration.configuration.isUseLocalExec() && useLocalExec) { LocalExecClient localExecClient = new LocalExecClient(); if (localExecClient.connect()) { localExecClient.runOneCommand(finalname, delay, waitForValidation, futureCompletion); LocalExecResult result = localExecClient.getLocalExecResult(); finalize(result.getStatus(), result.getResult(), finalname); localExecClient.disconnect(); return; } // else continue } String[] args = finalname.split(" "); File exec = new File(args[0]); if (exec.isAbsolute()) { if (!exec.canExecute()) { logger.error("Exec command is not executable: " + finalname); R66Result result = new R66Result(session, false, ErrorCode.CommandNotFound, session.getRunner()); futureCompletion.setResult(result); futureCompletion.cancel(); return; } } CommandLine commandLine = new CommandLine(args[0]); for (int i = 1; i < args.length; i++) { commandLine.addArgument(args[i]); } DefaultExecutor defaultExecutor = new DefaultExecutor(); PipedInputStream inputStream = new PipedInputStream(); PipedOutputStream outputStream = null; try { outputStream = new PipedOutputStream(inputStream); } catch (IOException e1) { try { inputStream.close(); } catch (IOException e) { } logger.error("Exception: " + e1.getMessage() + " Exec in error with " + commandLine.toString(), e1); futureCompletion.setFailure(e1); return; } PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(outputStream, null); defaultExecutor.setStreamHandler(pumpStreamHandler); int[] correctValues = { 0, 1 }; defaultExecutor.setExitValues(correctValues); ExecuteWatchdog watchdog = null; if (delay > 0) { watchdog = new ExecuteWatchdog(delay); defaultExecutor.setWatchdog(watchdog); } AllLineReader allLineReader = new AllLineReader(inputStream); Thread thread = new Thread(allLineReader, "ExecRename" + session.getRunner().getSpecialId()); thread.setDaemon(true); Configuration.configuration.getExecutorService().execute(thread); int status = -1; try { status = defaultExecutor.execute(commandLine); } catch (ExecuteException e) { if (e.getExitValue() == -559038737) { // Cannot run immediately so retry once try { Thread.sleep(Configuration.RETRYINMS); } catch (InterruptedException e1) { } try { status = defaultExecutor.execute(commandLine); } catch (ExecuteException e1) { finalizeFromError(outputStream, pumpStreamHandler, inputStream, allLineReader, thread, status, commandLine); return; } catch (IOException e1) { try { outputStream.flush(); } catch (IOException e2) { } try { outputStream.close(); } catch (IOException e2) { } thread.interrupt(); try { inputStream.close(); } catch (IOException e2) { } try { pumpStreamHandler.stop(); } catch (IOException e2) { } logger.error( "IOException: " + e.getMessage() + " . Exec in error with " + commandLine.toString()); futureCompletion.setFailure(e); return; } } else { finalizeFromError(outputStream, pumpStreamHandler, inputStream, allLineReader, thread, status, commandLine); return; } } catch (IOException e) { try { outputStream.close(); } catch (IOException e1) { } thread.interrupt(); try { inputStream.close(); } catch (IOException e1) { } try { pumpStreamHandler.stop(); } catch (IOException e2) { } logger.error("IOException: " + e.getMessage() + " . Exec in error with " + commandLine.toString()); futureCompletion.setFailure(e); return; } try { outputStream.flush(); } catch (IOException e) { } try { outputStream.close(); } catch (IOException e) { } try { pumpStreamHandler.stop(); } catch (IOException e2) { } try { if (delay > 0) { thread.join(delay); } else { thread.join(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } try { inputStream.close(); } catch (IOException e1) { } String newname = null; if (defaultExecutor.isFailure(status) && watchdog != null && watchdog.killedProcess()) { // kill by the watchdoc (time out) status = -1; newname = "TimeOut"; } else { newname = allLineReader.getLastLine().toString(); } finalize(status, newname, commandLine.toString()); }
From source file:org.waarp.openr66.context.task.ExecOutputTask.java
private void finalizeFromError(PipedOutputStream outputStream, PumpStreamHandler pumpStreamHandler, PipedInputStream inputStream, AllLineReader allLineReader, Thread thread, int status, CommandLine commandLine) {/*from ww w . j a v a2 s. co m*/ try { Thread.sleep(Configuration.RETRYINMS); } catch (InterruptedException e) { } try { outputStream.flush(); } catch (IOException e2) { } try { Thread.sleep(Configuration.RETRYINMS); } catch (InterruptedException e) { } try { outputStream.close(); } catch (IOException e1) { } thread.interrupt(); try { inputStream.close(); } catch (IOException e1) { } try { Thread.sleep(Configuration.RETRYINMS); } catch (InterruptedException e) { } try { pumpStreamHandler.stop(); } catch (IOException e2) { } try { Thread.sleep(Configuration.RETRYINMS); } catch (InterruptedException e) { } String result = allLineReader.getLastLine().toString(); logger.error("Status: " + status + " Exec in error with " + commandLine + " returns " + result); OpenR66RunnerErrorException exc = new OpenR66RunnerErrorException( "<STATUS>" + status + "</STATUS><ERROR>" + result + "</ERROR>"); futureCompletion.setFailure(exc); }
From source file:org.waarp.openr66.context.task.ExecTask.java
@Override public void run() { /*// w ww . j a v a 2 s . c o m * First apply all replacements and format to argRule from context and argTransfer. Will * call exec (from first element of resulting string) with arguments as the following value * from the replacements. Return 0 if OK, else 1 for a warning else as an error. No change * should be done in the FILENAME */ logger.debug("Exec with " + argRule + ":" + argTransfer + " and {}", session); String finalname = argRule; finalname = getReplacedValue(finalname, argTransfer.split(" ")); // Check if the execution will be done through LocalExec daemon if (Configuration.configuration.isUseLocalExec() && useLocalExec) { LocalExecClient localExecClient = new LocalExecClient(); if (localExecClient.connect()) { localExecClient.runOneCommand(finalname, delay, waitForValidation, futureCompletion); localExecClient.disconnect(); return; } // else continue } // Execution is done internally String[] args = finalname.split(" "); File exec = new File(args[0]); if (exec.isAbsolute()) { if (!exec.canExecute()) { logger.error("Exec command is not executable: " + finalname); R66Result result = new R66Result(session, false, ErrorCode.CommandNotFound, session.getRunner()); futureCompletion.setResult(result); futureCompletion.cancel(); return; } } CommandLine commandLine = new CommandLine(args[0]); for (int i = 1; i < args.length; i++) { commandLine.addArgument(args[i]); } DefaultExecutor defaultExecutor = new DefaultExecutor(); PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(null, null); defaultExecutor.setStreamHandler(pumpStreamHandler); int[] correctValues = { 0, 1 }; defaultExecutor.setExitValues(correctValues); ExecuteWatchdog watchdog = null; if (delay > 0 && waitForValidation) { watchdog = new ExecuteWatchdog(delay); defaultExecutor.setWatchdog(watchdog); } if (!waitForValidation) { // Do not wait for validation futureCompletion.setSuccess(); logger.info("Exec will start but no WAIT with {}", commandLine); } int status = -1; try { status = defaultExecutor.execute(commandLine); } catch (ExecuteException e) { if (e.getExitValue() == -559038737) { // Cannot run immediately so retry once try { Thread.sleep(Configuration.RETRYINMS); } catch (InterruptedException e1) { } try { status = defaultExecutor.execute(commandLine); } catch (ExecuteException e1) { try { pumpStreamHandler.stop(); } catch (IOException e2) { } logger.error("Exception: " + e.getMessage() + " Exec in error with " + commandLine.toString()); if (waitForValidation) { futureCompletion.setFailure(e); } return; } catch (IOException e1) { try { pumpStreamHandler.stop(); } catch (IOException e2) { } logger.error("Exception: " + e.getMessage() + " Exec in error with " + commandLine.toString()); if (waitForValidation) { futureCompletion.setFailure(e); } return; } } else { try { pumpStreamHandler.stop(); } catch (IOException e2) { } logger.error("Exception: " + e.getMessage() + " Exec in error with " + commandLine.toString()); if (waitForValidation) { futureCompletion.setFailure(e); } return; } } catch (IOException e) { try { pumpStreamHandler.stop(); } catch (IOException e2) { } logger.error("Exception: " + e.getMessage() + " Exec in error with " + commandLine.toString()); if (waitForValidation) { futureCompletion.setFailure(e); } return; } try { pumpStreamHandler.stop(); } catch (IOException e2) { } if (defaultExecutor.isFailure(status) && watchdog != null && watchdog.killedProcess()) { // kill by the watchdoc (time out) logger.error("Exec is in Time Out"); status = -1; } if (status == 0) { if (waitForValidation) { futureCompletion.setSuccess(); } logger.info("Exec OK with {}", commandLine); } else if (status == 1) { logger.warn("Exec in warning with " + commandLine.toString()); if (waitForValidation) { futureCompletion.setSuccess(); } } else { logger.error("Status: " + status + " Exec in error with " + commandLine.toString()); if (waitForValidation) { futureCompletion.cancel(); } } }