示例#1
0
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    HttpSession session = request.getSession(true);
    String employeeId = ((LoginPass) session.getAttribute(ShsContext.LOGIN_PASS)).getUsername();

    Map<String, String> jsonKeyValMap = new HashMap<String, String>();
    String resourceFile =
        ShsContext.getStorageDir(employeeId) + request.getParameter(ShsContext.JAR_FILENAME);

    String className = request.getParameter(ShsContext.CLASS_NAME);

    String additionalParameters = request.getParameter(ShsContext.ADDITIONAL_PARAMETERS);
    if (StringUtils.isEmpty(additionalParameters)) {
      additionalParameters = "";
    }
    String command =
        ShsContext.getCommand(
            ShsContext.SCRIPT_FILENAME, resourceFile, className, additionalParameters);

    System.out.println("command=\n" + command);
    Process process = ShsContext.execute(command);
    InputStream in = process.getInputStream();
    InputStream error = process.getErrorStream();
    String jobId = null;
    if (in.available() > 0) {
      System.out.println("Trying to get jobId from InputStream...");
      jobId = ShsContext.getJobId(in);
    } else {
      System.out.println("Trying to get jobId from ErrorInputStream...");
      jobId = ShsContext.getJobId(error);
    }

    if (jobId != null) {
      jsonKeyValMap.put("jobid", jobId);
    } else {
      jsonKeyValMap.put(
          "error_msg",
          "Failed to submit the MapReduce task, please contact administrator for more details.");
    }
    in.close();
    error.close();
    PrintWriter out = response.getWriter();
    response.setContentType("application/json");
    out.write(ShsContext.getJsonString(jsonKeyValMap));
    out.close();
  }