コード例 #1
0
ファイル: NetworkUtils.java プロジェクト: yyqqing/logstash
 public Map<String, String> getEnvironment() {
   Map<String, String> env = Collections.emptyMap();
   try {
     env = EnvironmentUtils.getProcEnvironment();
   } catch (IOException e) {
     LOG.error("Unable to get environmental variables", e);
   }
   return env;
 }
コード例 #2
0
  /** @throws MojoExecutionException if anything unexpected happens. */
  public void execute() throws MojoExecutionException {
    if (!workingDirectory.exists()) {
      workingDirectory.mkdirs();
    }

    CommandLine commandLine = new CommandLine(executable);
    Executor exec = new DefaultExecutor();
    exec.setWorkingDirectory(workingDirectory);

    Map env = new HashMap();
    try {
      Map systemEnvVars = EnvironmentUtils.getProcEnvironment();
      env.putAll(systemEnvVars);
    } catch (IOException e) {
      getLog().error("Could not assign default system enviroment variables.", e);
    }
    env.put("NODE_PATH", new File(workingDirectory, "node_modules").getAbsolutePath());
    env.put("XUNIT_FILE", outputFile.getAbsolutePath());

    List commandArguments = new ArrayList();
    commandArguments.add("test");

    String[] args = new String[commandArguments.size()];
    for (int i = 0; i < commandArguments.size(); i++) {
      args[i] = (String) commandArguments.get(i);
    }

    commandLine.addArguments(args, false);

    OutputStream stdout = System.out;
    OutputStream stderr = System.err;

    try {
      outputFile.getParentFile().mkdirs();
      getLog()
          .debug(
              "Executing command line "
                  + commandLine
                  + " in directory "
                  + workingDirectory.getAbsolutePath());
      exec.setStreamHandler(new PumpStreamHandler(stdout, stderr, System.in));

      int resultCode = exec.execute(commandLine, env);

      if (0 != resultCode) {
        throw new MojoExecutionException(
            "Result of " + commandLine + " execution is: '" + resultCode + "'.");
      }
    } catch (ExecuteException e) {
      throw new MojoExecutionException(EXECUTION_FAILED, e);

    } catch (IOException e) {
      throw new MojoExecutionException(EXECUTION_FAILED, e);
    }
  }
コード例 #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;
  }