Skip to content

Commit 4c96447

Browse files
committed
Defensively close jar files from non-cached JarURLConnections
Issue: SPR-6295
1 parent 1bc1df2 commit 4c96447

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ protected Set<Resource> doFindPathMatchingJarResources(Resource rootDirResource,
552552
JarFile jarFile;
553553
String jarFileUrl;
554554
String rootEntryPath;
555-
boolean newJarFile = false;
555+
boolean closeJarFile;
556556

557557
if (con instanceof JarURLConnection) {
558558
// Should usually be the case for traditional JAR files.
@@ -562,6 +562,7 @@ protected Set<Resource> doFindPathMatchingJarResources(Resource rootDirResource,
562562
jarFileUrl = jarCon.getJarFileURL().toExternalForm();
563563
JarEntry jarEntry = jarCon.getJarEntry();
564564
rootEntryPath = (jarEntry != null ? jarEntry.getName() : "");
565+
closeJarFile = !jarCon.getUseCaches();
565566
}
566567
else {
567568
// No JarURLConnection -> need to resort to URL file parsing.
@@ -581,7 +582,7 @@ protected Set<Resource> doFindPathMatchingJarResources(Resource rootDirResource,
581582
jarFileUrl = urlFile;
582583
rootEntryPath = "";
583584
}
584-
newJarFile = true;
585+
closeJarFile = true;
585586
}
586587
catch (ZipException ex) {
587588
if (logger.isDebugEnabled()) {
@@ -614,9 +615,7 @@ protected Set<Resource> doFindPathMatchingJarResources(Resource rootDirResource,
614615
return result;
615616
}
616617
finally {
617-
// Close jar file, but only if freshly obtained -
618-
// not from JarURLConnection, which might cache the file reference.
619-
if (newJarFile) {
618+
if (closeJarFile) {
620619
jarFile.close();
621620
}
622621
}

0 commit comments

Comments
 (0)