Exemplo n.º 1
0
  public boolean checkWPSProcess(Document document) throws XPathExpressionException, IOException {

    ProcessStatus procStat = new ProcessStatus(document);

    if (procStat.isSuccess()) {
      return true;
    } else if (procStat.isFailed()) {
      throw new IOException("Process failed");
    }
    return false;
  }
Exemplo n.º 2
0
  /**
   * @param alg Algorithm to run
   * @param wpsRequest XML representing WPS request
   * @param uuid UUID for output
   * @param metaObj RunMetadata associated with user
   * @return Web Accessible File with process results
   * @throws IOException
   * @throws ParserConfigurationException
   * @throws SAXException
   * @throws XPathExpressionException
   * @throws InterruptedException
   */
  private String runNamedAlgorithm(String alg, String wpsRequest, UUID uuid, RunMetadata metaObj)
      throws IOException, ParserConfigurationException, SAXException, XPathExpressionException,
          InterruptedException {

    // String wpsRequest = WPSImpl.createNahatStatsRequest(sosEndpoint, info.stations,
    // info.properties);
    String wpsResponse = postToWPS(props.getProperty("watersmart.wps.url"), wpsRequest);
    log.debug(wpsResponse);
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    Document wpsResponseDoc =
        factory.newDocumentBuilder().parse(new ByteArrayInputStream(wpsResponse.getBytes()));
    ProcessStatus procStat = new ProcessStatus(wpsResponseDoc);
    String wpsCheckPoint = procStat.getStatusLocation();
    log.debug(wpsCheckPoint);
    String contextPath = props.getProperty("watersmart.external.mapping.url");

    InputStream is = null;
    InputStream resultIs = null;

    try {
      boolean completed = false;
      Document document = null;
      int checks = 0;
      while (!completed) {
        checks++;
        // TODO-
        // http://stackoverflow.com/questions/3535754/netbeans-java-new-hint-thread-sleep-called-in-loop
        Thread.sleep(CHECK_WAIT);
        log.debug("Checking: " + checks);
        is = HTTPUtils.sendPacket(new URL(wpsCheckPoint), "GET");
        document = CheckProcessCompletion.parseDocument(is);
        completed = checkWPSProcess(document);
        if (checks == CHECKS_UNTIL_NOTIFY) {
          sendMaybeEmail(metaObj.getEmail());
        }
        if (checks > CHECKS_UNTIL_FAIL) {
          throw new IOException("R Statistics never returned");
        }
      }

      ProcessStatus resultStatus = new ProcessStatus(document);
      String outputReference = resultStatus.getOutputReference();
      resultIs = HTTPUtils.sendPacket(new URL(outputReference), "GET");
      String resultStr = IOUtils.toString(resultIs, "UTF-8");
      // copy results to persistant location // switch to completed document above

      log.debug(resultStr);

      File destinationDir =
          new File(
              props.getProperty("watersmart.file.location")
                  + props.getProperty("watersmart.file.location.wps.repository")
                  + File.separatorChar
                  + uuid);
      if (!destinationDir.exists()) {
        FileUtils.forceMkdir(destinationDir);
      }
      String filename =
          metaObj.getTypeString()
              + "-"
              + metaObj.getScenario()
              + "-"
              + metaObj.getModelVersion()
              + "."
              + metaObj.getRunIdent()
              + "-"
              + alg
              + ".txt";
      File destinationFile =
          new File(destinationDir.getCanonicalPath() + File.separatorChar + filename);
      FileUtils.write(destinationFile, resultStr, "UTF-8");
      String destinationFileName = destinationFile.getName();
      String webAccessibleFile =
          contextPath
              + props.getProperty("watersmart.file.location.wps.repository")
              + "/"
              + uuid
              + "/"
              + destinationFileName;

      return webAccessibleFile;
    } finally {
      IOUtils.closeQuietly(is);
      IOUtils.closeQuietly(resultIs);
    }
  }