From 4c964473b19590780b9ddad34c832f0b2b4f47d8 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 25 Feb 2016 10:25:13 +0100 Subject: [PATCH] Defensively close jar files from non-cached JarURLConnections Issue: SPR-6295 --- .../io/support/PathMatchingResourcePatternResolver.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java b/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java index fd4ae4707613..845a05731ade 100644 --- a/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java +++ b/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java @@ -552,7 +552,7 @@ protected Set doFindPathMatchingJarResources(Resource rootDirResource, JarFile jarFile; String jarFileUrl; String rootEntryPath; - boolean newJarFile = false; + boolean closeJarFile; if (con instanceof JarURLConnection) { // Should usually be the case for traditional JAR files. @@ -562,6 +562,7 @@ protected Set doFindPathMatchingJarResources(Resource rootDirResource, jarFileUrl = jarCon.getJarFileURL().toExternalForm(); JarEntry jarEntry = jarCon.getJarEntry(); rootEntryPath = (jarEntry != null ? jarEntry.getName() : ""); + closeJarFile = !jarCon.getUseCaches(); } else { // No JarURLConnection -> need to resort to URL file parsing. @@ -581,7 +582,7 @@ protected Set doFindPathMatchingJarResources(Resource rootDirResource, jarFileUrl = urlFile; rootEntryPath = ""; } - newJarFile = true; + closeJarFile = true; } catch (ZipException ex) { if (logger.isDebugEnabled()) { @@ -614,9 +615,7 @@ protected Set doFindPathMatchingJarResources(Resource rootDirResource, return result; } finally { - // Close jar file, but only if freshly obtained - - // not from JarURLConnection, which might cache the file reference. - if (newJarFile) { + if (closeJarFile) { jarFile.close(); } }