Beispiel #1
0
  public boolean statusUpdated() {
    // check status
    // return true if status changed
    String oldStatus = status;
    String newStatus;
    String output = null;
    try {
      output = util.callGET(new URL("http://" + address + ":" + port), null, null);
      // Logger.getLogger(Server.class.getName()).log(Level.INFO, output);
      if (output == null || output.equalsIgnoreCase("zero")) newStatus = "inactive";
      else newStatus = "active";
    } catch (MalformedURLException ex) {
      Logger.getLogger(Server.class.getName()).log(Level.SEVERE, output, ex);
      newStatus = "inactive";
    }

    if (oldStatus.equals(newStatus)) return false;

    status = newStatus;

    return true;
  }
  private boolean createJavaService(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    boolean success = false;

    String author = request.getParameter("tmpl-author");
    String componentID = request.getParameter("tmpl-componentID");
    String description = request.getParameter("tmpl-description");

    boolean hasInput = request.getParameter("tmpl-hasInput") != null;
    int numOutputs = Integer.parseInt(request.getParameter("tmpl-numOutputs"));
    boolean queueLogging = request.getParameter("tmpl-queueLogging") != null;
    boolean usesMongo = request.getParameter("tmpl-usesMongo") != null;
    boolean isBroker = request.getParameter("tmpl-workerType") == null;
    HashSet<String> requiredParameters = null;
    String s_req = request.getParameter("tmpl-requiredParameters");
    if (s_req != null && !s_req.trim().isEmpty()) {
      String[] split = s_req.split(",");
      String trimmed;
      requiredParameters = new HashSet<>();
      for (int i = 0; i < split.length; i++) {
        trimmed = split[i].trim();
        if (!trimmed.isEmpty()) requiredParameters.add(trimmed);
      }
    }

    Map<String, Object> root = new HashMap<>();

    root.put("Author", author);
    root.put("ComponentID", componentID);
    root.put("Description", description);
    if (hasInput) root.put("HasInput", true);
    root.put("NumOutputs", numOutputs);
    if (queueLogging) root.put("QueueLogging", true);
    if (usesMongo) root.put("UsesMongo", true);
    if (isBroker) root.put("IsBroker", true);
    if (requiredParameters != null && !requiredParameters.isEmpty())
      root.put("RequiredParameters", requiredParameters);

    String filename;
    Template tmpl;
    File dir;
    String templDir = "templates/java";
    // String OZServiceDir = webAppPath + "/templates/OpenZooService";
    String outputBaseDir = localRepository + "/templates/" + UUID.randomUUID().toString();
    String outputComponentDir = outputBaseDir + "/" + componentID;
    // String outputOZServiceDir = outputBaseDir + "/OpenZooService";
    String s_ZipFile = localRepository + "/templates/" + componentID + ".zip";

    dir = new File(outputComponentDir);
    if (dir.exists()) FileUtils.deleteQuietly(dir);
    dir.mkdirs();

    //        dir = new File(outputOZServiceDir);
    //        if (dir.exists())
    //            FileUtils.deleteQuietly(dir);
    //        dir.mkdirs();

    dir = new File(outputBaseDir);
    System.out.println("Output base dir will be: " + dir.getAbsolutePath());

    for (int i = 0; i < dirs.length; i++) {
      dir = new File(outputComponentDir + "/" + dirs[i]);
      dir.mkdirs();
    }

    // first create the component files using the templates
    for (int i = 0; i < files.length; i++) {
      filename = files[i];

      if (!(new File(webAppPath + "/" + templDir + "/" + filename + ".ftl")).exists()) continue;

      tmpl = cfg.getTemplate(templDir + "/" + filename + ".ftl");

      filename = filename.replace("_COMPONENTID_", componentID);

      try (BufferedWriter writer =
          new BufferedWriter(new FileWriter(outputComponentDir + "/" + filename))) {
        tmpl.process(root, writer);
      } catch (TemplateException ex) {
        System.err.println(
            "TemplateException during creating template for file " + filename + ": " + ex);
        err("TemplateException during creating template for file " + filename + ": " + ex);
      }
    }

    // then copy the OpenZooService files
    //        System.out.println("Copying OpenZooService files from " + new
    // File(OZServiceDir).getAbsolutePath());
    //        FileUtils.copyDirectory(new File(OZServiceDir), new File(outputOZServiceDir));

    // finally, create zip file with both directories
    // compressDirectory, apart from compressing, also renames .jav_ to .java files

    if (Utilities.compressDirectory(outputBaseDir, s_ZipFile)) {
      File downloadFile = new File(s_ZipFile);
      ServletContext context = request.getServletContext();
      String mimeType = context.getMimeType(s_ZipFile);
      if (mimeType == null) {
        // set to binary type if MIME mapping not found
        mimeType = "application/octet-stream";
      }
      System.out.println("MIME type: " + mimeType);
      response.setContentType(mimeType);
      response.setContentLength((int) downloadFile.length());

      String headerKey = "Content-Disposition";
      String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName());
      response.setHeader(headerKey, headerValue);
      byte[] buffer = new byte[4096];
      int bytesRead;
      try (FileInputStream inStream = new FileInputStream(downloadFile);
          OutputStream outStream = response.getOutputStream()) {
        while ((bytesRead = inStream.read(buffer)) != -1) {
          outStream.write(buffer, 0, bytesRead);
        }
      }

      success = true;
    } else {
      System.err.println("Directory could not be compressed");
      err("Directory could not be compressed");
    }

    dir = new File(outputBaseDir);
    if (dir.exists()) FileUtils.deleteQuietly(dir);

    return success;
  }