Ejemplo n.º 1
0
  public OperationResult invokeOperation(String name, Configuration params) throws Exception {
    if (name.equals(EXECUTE_OPERATION)) {
      OperationResult operationResult = new OperationResult();

      File scriptFile = getScriptFile();
      SystemInfo systemInfo = this.resourceContext.getSystemInformation();
      ProcessExecution processExecution =
          ProcessExecutionUtility.createProcessExecution(scriptFile);

      processExecution.setWaitForCompletion(1000L * 60 * 60); // 1 hour
      processExecution.setCaptureOutput(true);

      // TODO: Make the script's cwd configurable, but default it to the
      // directory containing the script.
      processExecution.setWorkingDirectory(scriptFile.getParent());

      setEnvironmentVariables(processExecution);
      setCommandLineArguments(params, processExecution);

      if (log.isDebugEnabled()) {
        log.debug(processExecution);
      }

      ProcessExecutionResults processExecutionResults = systemInfo.executeProcess(processExecution);
      if (processExecutionResults.getError() != null) {
        throw new Exception(processExecutionResults.getError());
      }

      Integer exitCode = processExecutionResults.getExitCode();
      String output = processExecutionResults.getCapturedOutput(); // NOTE:
      // this
      // is
      // stdout
      // +
      // stderr

      Configuration complexResults = operationResult.getComplexResults();
      complexResults.put(new PropertySimple(EXIT_CODE_RESULT_PROP, exitCode));
      complexResults.put(new PropertySimple(OUTPUT_RESULT_PROP, output));
      if (exitCode != null && exitCode != 0) {
        operationResult.setErrorMessage(
            "Exit code was '" + exitCode + "', see operation results for details");
      }

      return operationResult;
    } else {
      throw new IllegalArgumentException("Unsupported operation: " + name);
    }
  }