示例#1
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;
 }
示例#2
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;
 }
示例#3
0
  // this is where the action is
  public static void main(String[] args) {
    Runtime rt = Runtime.getRuntime();

    RuntimeExec rte = new RuntimeExec();

    StreamWrapper error, output;

    try {

      Process proc = rt.exec("cmd /c dir");
      error = rte.getStreamWrapper(proc.getErrorStream(), "ERROR");
      output = rte.getStreamWrapper(proc.getInputStream(), "OUTPUT");
      int exitVal = 0;

      error.start();
      output.start();
      error.join(3000);
      output.join(3000);
      exitVal = proc.waitFor();

      System.out.println(
          "Output: " + output.message + "\nError: " + error.message + "exitvalue:" + exitVal);

    } catch (IOException e) {
      e.printStackTrace();

    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
示例#4
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.");
  }
示例#5
0
  public Continuation runContinuations(StreamWrapper lastActive, EmptyCollector collector) {

    Iterator<CompletableFuture> it = lastActive.stream().iterator();

    Continuation[] cont = new Continuation[1];

    Continuation finish =
        new Continuation(
            () -> {
              collector.getResults();
              runnable.run();
              throw new ClosedQueueException();
            });
    Continuation finishNoCollect =
        new Continuation(
            () -> {
              runnable.run();
              throw new ClosedQueueException();
            });

    cont[0] =
        new Continuation(
            () -> {
              try {

                if (it.hasNext()) {

                  CompletableFuture f = it.next();

                  collector.accept(f);
                }
                if (it.hasNext()) return cont[0];
                else {
                  return finish.proceed();
                }
              } catch (SimpleReactProcessingException e) {

              } catch (java.util.concurrent.CompletionException e) {

              } catch (Throwable e) {
              }
              return finishNoCollect;
            });

    return cont[0];
  }
示例#6
0
  public boolean run(StreamWrapper lastActive, EmptyCollector collector) {

    try {
      lastActive
          .stream()
          .forEach(
              n -> {
                collector.accept(n);
              });
      collector.getResults();
    } catch (SimpleReactProcessingException e) {

    } catch (java.util.concurrent.CompletionException e) {

    } catch (Throwable e) {

    }

    runnable.run();
    return true;
  }
示例#7
0
 public static StreamWrapper createStreamWrapper(InputStream is, boolean buffered) {
   StreamWrapper sw = new StreamWrapper(is, buffered);
   sw.start();
   return sw;
 }
 public boolean isContentXHTML() throws IOException {
   pw.flush();
   os.checkForDocType();
   return os.isContentXHTML();
 }
 /**
  * Turns the outputStream into a string.
  *
  * @return outputStream into a string
  * @throws java.io.IOException if stream is broken
  */
 public String outputStreamToString() throws IOException {
   pw.flush();
   return os.outputStreamToString();
 }
 /**
  * Main constructor that calls super(response) then creates an output object.
  *
  * @param response the response to fallback to when content is not xhtml
  * @param dtdCheckEnabled enable check for xhtml dtd
  */
 public ResponseWrapper(HttpServletResponse response, boolean dtdCheckEnabled) {
   super(response);
   os.setResponse(response);
   os.setCheckForDtdDefnEnabled(dtdCheckEnabled);
 }