From b8a4e55037a5de72a5e9546a89a3f67c5ce50ad7 Mon Sep 17 00:00:00 2001 From: tzjan Date: Fri, 9 Feb 2024 07:56:56 +0100 Subject: [PATCH 1/3] Use versioned extension cache entry, if we have... a version. Otherwise the extension JNLP will be downloaded a 2nd time and a second entry will be created: ::i=0\13::l=http://localhost/myapp/jnlp.extension/common.jnlp::a=1706698483984:: ::i=0\0::l=http://localhost/myapp/jnlp.extension/common.jnlp::v=6.2.3::a=1706698483198:: Because the cache lookup is called twice on extensions. The second time without the version so the versioned entry is not used. Instead a new entry i=0\13 without a version is created. This works fine as long as not a new version of common.jnlp::v=6.2.4 is lookuped. Then the sorting in line 246 of net/adoptopenjdk/icedteaweb/resources/cache/CacheImpl.java fails withe a NPE, because it's missing the version. --- .../jnlp/element/resource/ExtensionDesc.java | 2 +- .../net/sourceforge/jnlp/JNLPFileFactory.java | 27 ++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/net/adoptopenjdk/icedteaweb/jnlp/element/resource/ExtensionDesc.java b/core/src/main/java/net/adoptopenjdk/icedteaweb/jnlp/element/resource/ExtensionDesc.java index 305c2322a..2ac1137bb 100644 --- a/core/src/main/java/net/adoptopenjdk/icedteaweb/jnlp/element/resource/ExtensionDesc.java +++ b/core/src/main/java/net/adoptopenjdk/icedteaweb/jnlp/element/resource/ExtensionDesc.java @@ -131,7 +131,7 @@ public URL getLocation() { */ public void resolve() throws ParseException, IOException { if (file == null) { - file = new JNLPFileFactory().create(location); + file = new JNLPFileFactory().create(location, version); LOG.debug("Resolve: {}", file.getInformation().getTitle()); diff --git a/core/src/main/java/net/sourceforge/jnlp/JNLPFileFactory.java b/core/src/main/java/net/sourceforge/jnlp/JNLPFileFactory.java index 952ad1516..2acf62511 100644 --- a/core/src/main/java/net/sourceforge/jnlp/JNLPFileFactory.java +++ b/core/src/main/java/net/sourceforge/jnlp/JNLPFileFactory.java @@ -49,6 +49,31 @@ public JNLPFile create(final URL location) throws IOException, ParseException { return create(location, new ParserSettings()); } + /** + * Generate unique key from file URL + * @param location + */ + private String createUniqueKey(final URL location) { + return Calendar.getInstance().getTimeInMillis() + "-" + ((int) (Math.random() * Integer.MAX_VALUE)) + "-" + location; + } + + /** + * Create a JNLPFile from a URL and parent URLm a version and checking for + * updates using the specified policy. + * + * @param location the location of the JNLP file + * @param version the version of the JNLP file + * @throws IOException if an IO exception occurred + * @throws ParseException if the JNLP file was invalid + */ + public JNLPFile create(final URL location, final VersionString version) throws IOException, ParseException { + return create(location, + createUniqueKey(location), + version, + new ParserSettings(), + JNLPRuntime.getDefaultUpdatePolicy()); + } + /** * Create a JNLPFile from a URL checking for updates using the * default policy. @@ -59,7 +84,7 @@ public JNLPFile create(final URL location) throws IOException, ParseException { * @throws ParseException if the JNLP file was invalid */ public JNLPFile create(final URL location, final ParserSettings settings) throws IOException, ParseException { - final String uniqueKey = Calendar.getInstance().getTimeInMillis() + "-" + ((int) (Math.random() * Integer.MAX_VALUE)) + "-" + location; + final String uniqueKey = createUniqueKey(location); return create(location, uniqueKey, null, settings, JNLPRuntime.getDefaultUpdatePolicy()); } From adbcacb0054912095a7322fc272fc5fb17023508 Mon Sep 17 00:00:00 2001 From: tzjan Date: Fri, 9 Feb 2024 08:01:42 +0100 Subject: [PATCH 2/3] Do not sort the result, because it's not required... in this context. This fixes a NPE on loading the cache which contains versioned and unversioned entries of a file looking up a new version eg. 6.2.4 ::i=0\13::l=http://localhost/myapp/jnlp.extension/common.jnlp::a=1706711261663:: ::i=0\0::l=http://localhost/myapp/jnlp.extension/common.jnlp::v=6.2.3::a=1706710902351:: --- .../icedteaweb/resources/cache/CacheImpl.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/net/adoptopenjdk/icedteaweb/resources/cache/CacheImpl.java b/core/src/main/java/net/adoptopenjdk/icedteaweb/resources/cache/CacheImpl.java index fe144bc5a..8c5217dee 100644 --- a/core/src/main/java/net/adoptopenjdk/icedteaweb/resources/cache/CacheImpl.java +++ b/core/src/main/java/net/adoptopenjdk/icedteaweb/resources/cache/CacheImpl.java @@ -238,14 +238,14 @@ Optional getBestMatchingEntryInCache(final URL resourceHref, fi .findFirst(); } + /** + * Return all valid cache entries with this URL, regardless of whether they have a version or not. They are not sorted. + * @param resourceHref + * @return + */ List getAllEntriesInCache(final URL resourceHref) { final List all = new ArrayList<>(cacheIndex.getSynchronized(idx -> idx.findAllEntries(resourceHref))); - if (all.size() > 1) { - final Comparator versionComparator = comparing(CacheIndexEntry::getVersion); - all.sort(versionComparator); - } - return all.stream() .filter(entry -> getInfoFile(entry).isCached()) .collect(Collectors.toList()); From d3e2008f5d86b1f34c16367300abcdb12128b1ff Mon Sep 17 00:00:00 2001 From: sclassen Date: Fri, 9 Feb 2024 09:26:57 +0100 Subject: [PATCH 3/3] Improve JavaDoc --- core/src/main/java/net/sourceforge/jnlp/JNLPFileFactory.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/net/sourceforge/jnlp/JNLPFileFactory.java b/core/src/main/java/net/sourceforge/jnlp/JNLPFileFactory.java index 2acf62511..37a109c21 100644 --- a/core/src/main/java/net/sourceforge/jnlp/JNLPFileFactory.java +++ b/core/src/main/java/net/sourceforge/jnlp/JNLPFileFactory.java @@ -58,8 +58,8 @@ private String createUniqueKey(final URL location) { } /** - * Create a JNLPFile from a URL and parent URLm a version and checking for - * updates using the specified policy. + * Create a JNLPFile from a URL and version and checking for + * updates using the default policy. * * @param location the location of the JNLP file * @param version the version of the JNLP file