// -creatediff -applydiff -debug -output file public static void main(String[] args) throws IOException { boolean diff = true; boolean minimal = true; String outputFile = "out.jardiff"; for (int counter = 0; counter < args.length; counter++) { // for backward compatibilty with 1.0.1/1.0 if (args[counter].equals("-nonminimal") || args[counter].equals("-n")) { minimal = false; } else if (args[counter].equals("-creatediff") || args[counter].equals("-c")) { diff = true; } else if (args[counter].equals("-applydiff") || args[counter].equals("-a")) { diff = false; } else if (args[counter].equals("-debug") || args[counter].equals("-d")) { _debug = true; } else if (args[counter].equals("-output") || args[counter].equals("-o")) { if (++counter < args.length) { outputFile = args[counter]; } } else if (args[counter].equals("-applydiff") || args[counter].equals("-a")) { diff = false; } else { if ((counter + 2) != args.length) { showHelp(); System.exit(0); } if (diff) { try { OutputStream os = new FileOutputStream(outputFile); JarDiff.createPatch(args[counter], args[counter + 1], os, minimal); os.close(); } catch (IOException ioe) { try { System.out.println(getResources().getString("jardiff.error.create") + " " + ioe); } catch (MissingResourceException mre) { } } } else { try { OutputStream os = new FileOutputStream(outputFile); new JarDiffPatcher().applyPatch(null, args[counter], args[counter + 1], os); os.close(); } catch (IOException ioe) { try { System.out.println(getResources().getString("jardiff.error.apply") + " " + ioe); } catch (MissingResourceException mre) { } } } System.exit(0); } } showHelp(); }
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; } }