Exemplo n.º 1
0
  /** Returns a JarDiff for the given request */
  public synchronized DownloadResponse getJarDiffEntry(
      ResourceCatalog catalog, DownloadRequest dreq, JnlpResource res) {
    if (dreq.getCurrentVersionId() == null) return null;

    // check whether the request is from javaws 1.0/1.0.1
    // do not generate minimal jardiff if it is from 1.0/1.0.1
    boolean doJarDiffWorkAround = isJavawsVersion(dreq, "1.0*");

    // First do a lookup to find a match
    JarDiffKey key =
        new JarDiffKey(
            res.getName(),
            dreq.getCurrentVersionId(),
            res.getReturnVersionId(),
            !doJarDiffWorkAround);

    JarDiffEntry entry = (JarDiffEntry) _jarDiffEntries.get(key);
    // If entry is not found, then the querty has not been made.
    if (entry == null) {
      if (_log.isInformationalLevel()) {
        _log.addInformational(
            "servlet.log.info.jardiff.gen",
            res.getName(),
            dreq.getCurrentVersionId(),
            res.getReturnVersionId());
      }
      File f = generateJarDiff(catalog, dreq, res, doJarDiffWorkAround);
      if (f == null) {
        _log.addWarning(
            "servlet.log.warning.jardiff.failed",
            res.getName(),
            dreq.getCurrentVersionId(),
            res.getReturnVersionId());
      }
      // Store entry in table
      entry = new JarDiffEntry(f);
      _jarDiffEntries.put(key, entry);
    }

    // Check for no JarDiff to return
    if (entry.getJarDiffFile() == null) {
      return null;
    } else {
      return DownloadResponse.getFileDownloadResponse(
          entry.getJarDiffFile(),
          _jarDiffMimeType,
          entry.getJarDiffFile().lastModified(),
          res.getReturnVersionId());
    }
  }
Exemplo n.º 2
0
  private File generateJarDiff(
      ResourceCatalog catalog,
      DownloadRequest dreq,
      JnlpResource res,
      boolean doJarDiffWorkAround) {
    boolean del_old = false;
    boolean del_new = false;

    // Lookup up file for request version
    DownloadRequest fromDreq = dreq.getFromDownloadRequest();
    try {
      JnlpResource fromRes = catalog.lookupResource(fromDreq);

      /* Get file locations */
      String newFilePath = _servletContext.getRealPath(res.getPath());
      String oldFilePath = _servletContext.getRealPath(fromRes.getPath());

      // fix for 4720897
      if (newFilePath == null) {
        newFilePath = getRealPath(res.getPath());
        if (newFilePath != null) del_new = true;
      }

      if (oldFilePath == null) {
        oldFilePath = getRealPath(fromRes.getPath());
        if (oldFilePath != null) del_old = true;
      }

      if (newFilePath == null || oldFilePath == null) {
        return null;
      }

      // Create temp. file to store JarDiff file in
      File tempDir = (File) _servletContext.getAttribute("javax.servlet.context.tempdir");

      // fix for 4653036: JarDiffHandler() should use javax.servlet.context.tempdir to store the
      // jardiff
      File outputFile = File.createTempFile("jnlp", ".jardiff", tempDir);

      _log.addDebug(
          "Generating Jardiff between "
              + oldFilePath
              + " and "
              + newFilePath
              + " Store in "
              + outputFile);

      // Generate JarDiff
      OutputStream os = new FileOutputStream(outputFile);

      JarDiff.createPatch(oldFilePath, newFilePath, os, !doJarDiffWorkAround);
      os.close();

      try {

        // Check that Jardiff is smaller, or return null
        if (outputFile.length() >= (new File(newFilePath).length())) {
          _log.addDebug("JarDiff discarded - since it is bigger");
          return null;
        }

        // Check that Jardiff is smaller than the packed version of
        // the new file, if the file exists at all
        File newFilePacked = new File(newFilePath + ".pack.gz");
        if (newFilePacked.exists()) {
          _log.addDebug("generated jardiff size: " + outputFile.length());
          _log.addDebug("packed requesting file size: " + newFilePacked.length());
          if (outputFile.length() >= newFilePacked.length()) {
            _log.addDebug("JarDiff discarded - packed version of requesting file is smaller");
            return null;
          }
        }

        _log.addDebug("JarDiff generation succeeded");
        return outputFile;

      } finally {
        // delete the temporarily downloaded file
        if (del_new) {
          new File(newFilePath).delete();
        }

        if (del_old) {
          new File(oldFilePath).delete();
        }
      }
    } catch (IOException ioe) {
      _log.addDebug("Failed to genereate jardiff", ioe);
      return null;
    } catch (ErrorResponseException ere) {
      _log.addDebug("Failed to genereate jardiff", ere);
      return null;
    }
  }