コード例 #1
0
 public void onProcessFailed(ExecuteException e) {
   super.onProcessFailed(e);
   if (watchdog != null && watchdog.killedProcess())
     System.err.println("[resultHandler] The Syntren launcher process timed out");
   else
     System.err.println(
         "[resultHandler] The Syntren laucher process failed to do: " + e.getMessage());
 }
コード例 #2
0
  /**
   * Execute a command line in with a given map of environment settings. The execution outupt is
   * filtered unless verbose is set to true.
   *
   * @param cmdLine The command to execute
   * @param env Environment settings available for the command execution
   * @return the command's execution exit code, or -2 if the command failed to execute
   */
  private int executeRecipe(final CommandLine cmdLine, final Map<Object, Object> env) {
    final DefaultExecutor executor = new DefaultExecutor();

    // The watchdog will terminate the process if it does not end within the
    // specified timeout
    final int externalProcessTimeout = (this.timeout + EXTERNAL_PROCESS_WATCHDOG_TIMEOUT) * 1000;
    final ExecuteWatchdog watchdog = new ExecuteWatchdog(externalProcessTimeout);
    executor.setWatchdog(watchdog);

    executor.setExitValue(0);
    int result = -1;

    PipedInputStream in = null;
    PipedOutputStream out = null;
    BufferedReader reader = null;
    try {
      in = new PipedInputStream();
      out = new PipedOutputStream(in);
      reader = new BufferedReader(new InputStreamReader(in));

      final Thread thread = new Thread(new FilteredOutputHandler(reader, this.verbose));
      thread.setDaemon(true);
      thread.start();

      final PumpStreamHandler psh = new PumpStreamHandler(out, out);
      executor.setStreamHandler(psh);
      result = executor.execute(cmdLine, env);
    } catch (final ExecuteException e) {
      logger.log(
          Level.SEVERE,
          "A problem was encountered while executing the recipe: " + e.getMessage(),
          e);
    } catch (final IOException e) {
      logger.log(
          Level.SEVERE,
          "An IO Exception was encountered while executing the recipe: " + e.getMessage(),
          e);
      result = -2;
    }

    return result;
  }
コード例 #3
0
  @SuppressWarnings({"unchecked", "rawtypes"})
  @Override
  protected int _exec() {
    int returnValue = 0;
    CommandLine cmdLine = getCommandLine();

    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
    ExecuteWatchdog watchdog = new ScriptExecuteWatchdog();

    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(0);
    executor.setWorkingDirectory(currentDir);
    executor.setWatchdog(watchdog);
    if (terminal != null) {
      ExecuteStreamHandler monitorStreamHandler =
          new PumpStreamHandler(terminal.getOutputStream(), terminal.getErrorStream());
      executor.setStreamHandler(monitorStreamHandler);
    } else {
      ExecuteStreamHandler silentStreamHandler = new PumpStreamHandler();
      executor.setStreamHandler(silentStreamHandler);
    }

    Map procEnvironment = null;
    if (environment != null) {
      try {
        procEnvironment = EnvironmentUtils.getProcEnvironment();
        procEnvironment.putAll(environment);
      } catch (IOException e) {
        logger.error(e.getMessage());
        procEnvironment = null;
      }
    }

    try {
      executor.execute(cmdLine, procEnvironment, resultHandler);
      notifyStart();
    } catch (Exception e) {
      logger.warn("[EXECUTOR] ERROR: {}", e.getMessage());

      notifyError(-1, e.getMessage());

      return 1;
    }

    try {
      if (resultHandler.hasResult()) {
        notifyRefresh();
      } else {
        logger.info("[EXECUTOR] RUNNING");
        while (!resultHandler.hasResult()) {
          resultHandler.waitFor(PrefUtil.getInt(PrefUtil.SCRIPT_RUN_REFRESH_TIME, 1000));
          notifyRefresh();
        }
      }
    } catch (InterruptedException e) {
      logger.warn("[EXECUTOR] INTERRUPTED");
      watchdog.destroyProcess();
    } finally {
      if (watchdog.killedProcess()) {
        logger.warn("[EXECUTOR] WAITING FOR KILL");
        try {
          resultHandler.waitFor(PrefUtil.getInt(PrefUtil.SCRIPT_WAIT_FOR_KILL_REFRESH_TIME, 5000));
        } catch (InterruptedException e) {
          logger.warn("[EXECUTOR] INTERRUPTED: {}", e.getMessage());
          notifyError(-1, e.getMessage());
        }
      }
      try {
        ExecuteException exception = resultHandler.getException();
        if (exception != null) {
          returnValue = resultHandler.getExitValue();
          logger.warn("[EXECUTOR] ERROR: {}", exception.getMessage());

          if (terminal != null) {
            // String error = terminal.getErrorStream().peekLines();
            // notifyError(returnValue, error);
            // error is empty
            notifyError(returnValue, exception.getMessage());
          } else {
            notifyError(returnValue, exception.getMessage());
          }
          service.shutdownNow();
        } else {
          returnValue = resultHandler.getExitValue();
          logger.info("[EXECUTOR] FINISH: {}", returnValue);
          notifyFinish(resultHandler.getExitValue());
        }
      } catch (IllegalStateException e) {
        returnValue = -1;
        logger.warn("[EXECUTOR] INTERRUPTED: {}", e.getMessage());
        notifyError(returnValue, e.getMessage());
      }
    }
    if (!keepFileOnEnd) {
      internalDeleteOnEnd();
    }

    return returnValue;
  }