Exemplo n.º 1
0
  public static void action() throws IOException, InterruptedException {

    // Connect to remote site
    String response = fetchTask();
    System.out.println("JSON: " + response.trim());

    JSONObject json = JSONObject.fromObject(response);
    int code = json.getInt("code");
    String desc = json.getString("desc");
    if (code != 0) {
      System.out.println("Code: " + code + " Desc: " + desc);
    }

    JSONObject task = json.getJSONObject("task");
    String jar = task.getString("jar");
    String cmd = task.getString("cmd");

    // Create temporary directory
    File dir = createTempDirectory();

    System.out.println("Temporary directory: " + dir.getAbsolutePath());
    // Download current version
    System.out.println("Downloading: " + jar);
    String fileName = download(jar, dir);

    System.out.println("Unzipping: " + fileName);
    UnZip.unzip(dir.getAbsolutePath() + File.separator + fileName, dir.getAbsolutePath());

    System.out.println("Executing: ");
    System.out.println(cmd);
    long startTime = System.currentTimeMillis();

    Runtime rt = Runtime.getRuntime();
    Process pr = rt.exec(cmd, null, dir);
    StreamWrapper errorStream = StreamWrapper.createStreamWrapper(pr.getErrorStream());
    StreamWrapper inputStream = StreamWrapper.createStreamWrapper(pr.getInputStream());
    int errorCode = pr.waitFor();
    System.out.println(errorCode);
    System.out.println(errorStream);
    if (!ECHO) {
      System.out.println(inputStream);
    }
    long duration = System.currentTimeMillis() - startTime;
    task.put("duration", duration);
    String result = inputStream.toString() + errorStream.toString();
    task.put("result", result);
    System.out.println("Sending results");
    String status = sendResults(task);

    System.out.println(status.trim());

    System.out.println("Cleaning up...");
    deleteDirectory(dir);
    System.out.println("Done.");
  }
Exemplo n.º 2
0
 private static String fetchTask() throws IOException, InterruptedException {
   String[] paramsNames = {"os.name", "os.arch", "os.version", "java.vendor", "java.version"};
   StringBuffer params = new StringBuffer();
   int max = paramsNames.length;
   for (String p : paramsNames) {
     params.append(p.replace('.', '_'));
     params.append('=');
     params.append(URLEncoder.encode(System.getProperty(p), "utf-8"));
     if (--max > 0) params.append('&');
   }
   String urlParameters = params.toString();
   //		System.out.println(urlParameters);
   String request = FETCH_REQUEST_URL + FETCH_REQUEST_URL_EXT;
   URL url = new URL(request);
   HttpURLConnection connection = (HttpURLConnection) url.openConnection();
   connection.setDoOutput(true);
   connection.setDoInput(true);
   connection.setInstanceFollowRedirects(false);
   connection.setRequestMethod("POST");
   connection.setRequestProperty("charset", "utf-8");
   connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
   connection.setRequestProperty(
       "Content-Length", Integer.toString(urlParameters.getBytes().length));
   connection.setUseCaches(false);
   connection.getOutputStream().write(urlParameters.getBytes());
   connection.getOutputStream().close();
   StreamWrapper requestStream =
       StreamWrapper.createStreamWrapper(connection.getInputStream(), false);
   requestStream.join();
   String response = requestStream.toString();
   return response;
 }
Exemplo n.º 3
0
 private static String sendResults(JSONObject task) throws IOException, InterruptedException {
   String body = "t=" + task.toString();
   String request = RESULTS_REQUEST_URL;
   URL url = new URL(request);
   HttpURLConnection connection = (HttpURLConnection) url.openConnection();
   connection.setDoOutput(true);
   connection.setDoInput(true);
   connection.setInstanceFollowRedirects(false);
   connection.setRequestMethod("POST");
   connection.setRequestProperty("charset", "utf-8");
   connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
   connection.setRequestProperty("Content-Length", String.valueOf(body.getBytes().length));
   connection.setUseCaches(false);
   connection.getOutputStream().write(body.getBytes());
   connection.getOutputStream().close();
   StreamWrapper requestStream =
       StreamWrapper.createStreamWrapper(connection.getInputStream(), false);
   requestStream.join();
   String response = requestStream.toString();
   return response;
 }