From 0a971cb4953e781d682b84d4a437387722d7d37a Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Wed, 8 Nov 2017 19:17:39 +0100 Subject: [PATCH] Issue #1797 - JEP 238 - Multi-Release JAR files break bytecode scanning. Made MultiReleaseJarFile closeable and using try-with-resources in AnnotationParser to avoid leaking file descriptors. Made few code simplifications. Signed-off-by: Simone Bordet --- .../jetty/annotations/AnnotationParser.java | 22 ++++++++++--------- .../jetty/util/MultiReleaseJarFile.java | 19 +++++++++++----- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/AnnotationParser.java b/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/AnnotationParser.java index dc0e59e06961..145db9500bde 100644 --- a/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/AnnotationParser.java +++ b/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/AnnotationParser.java @@ -916,18 +916,20 @@ protected void parseJar (Set handlers, Resource jarResource, LOG.debug("Scanning jar {}", jarResource); MultiException me = new MultiException(); - MultiReleaseJarFile jarFile = new MultiReleaseJarFile(jarResource.getFile(),_javaPlatform,false); - jarFile.stream().forEach(e-> + try (MultiReleaseJarFile jarFile = new MultiReleaseJarFile(jarResource.getFile(),_javaPlatform,false)) { - try - { - parseJarEntry(handlers, jarResource, e, resolver); - } - catch (Exception ex) + jarFile.stream().forEach(e-> { - me.add(new RuntimeException("Error scanning entry " + e.getName() + " from jar " + jarResource, ex)); - } - }); + try + { + parseJarEntry(handlers, jarResource, e, resolver); + } + catch (Exception ex) + { + me.add(new RuntimeException("Error scanning entry " + e.getName() + " from jar " + jarResource, ex)); + } + }); + } me.ifExceptionThrow(); } } diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/MultiReleaseJarFile.java b/jetty-util/src/main/java/org/eclipse/jetty/util/MultiReleaseJarFile.java index 506731c8f233..3a9c0759e50b 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/MultiReleaseJarFile.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/MultiReleaseJarFile.java @@ -18,11 +18,13 @@ package org.eclipse.jetty.util; +import java.io.Closeable; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.Collections; import java.util.Iterator; +import java.util.Locale; import java.util.Map; import java.util.TreeMap; import java.util.jar.JarEntry; @@ -33,7 +35,7 @@ /** *

Utility class to handle a Multi Release Jar file

*/ -public class MultiReleaseJarFile +public class MultiReleaseJarFile implements Closeable { private static final String META_INF_VERSIONS = "META-INF/versions/"; @@ -84,12 +86,10 @@ public MultiReleaseJarFile(File file, int javaPlatform, boolean includeDirectori { Map.Entry e = i.next(); VersionedJarEntry entry = e.getValue(); - if (entry.inner) { VersionedJarEntry outer = entry.outer==null?null:map.get(entry.outer); - - if (entry.outer==null || outer==null || outer.version!= entry.version) + if (outer==null || outer.version!=entry.version) i.remove(); } } @@ -130,6 +130,13 @@ public VersionedJarEntry getEntry(String name) return entries.get(name); } + @Override + public void close() throws IOException + { + if (jarFile!=null) + jarFile.close(); + } + @Override public String toString() { @@ -172,8 +179,8 @@ public class VersionedJarEntry this.entry = entry; this.name = name; this.version = v; - this.inner = name.contains("$") && name.toLowerCase().endsWith(".class"); - this.outer = inner ? name.substring(0, name.indexOf('$')) + name.substring(name.length() - 6, name.length()) : null; + this.inner = name.contains("$") && name.toLowerCase(Locale.ENGLISH).endsWith(".class"); + this.outer = inner ? name.substring(0, name.indexOf('$')) + ".class" : null; } /**