Defensively close jar files from non-cached JarURLConnections

Issue: SPR-6295
This commit is contained in:
Juergen Hoeller 2016-02-25 10:25:13 +01:00
parent 1bc1df2d0f
commit 4c964473b1
1 changed files with 4 additions and 5 deletions

View File

@ -552,7 +552,7 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
JarFile jarFile; JarFile jarFile;
String jarFileUrl; String jarFileUrl;
String rootEntryPath; String rootEntryPath;
boolean newJarFile = false; boolean closeJarFile;
if (con instanceof JarURLConnection) { if (con instanceof JarURLConnection) {
// Should usually be the case for traditional JAR files. // Should usually be the case for traditional JAR files.
@ -562,6 +562,7 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
jarFileUrl = jarCon.getJarFileURL().toExternalForm(); jarFileUrl = jarCon.getJarFileURL().toExternalForm();
JarEntry jarEntry = jarCon.getJarEntry(); JarEntry jarEntry = jarCon.getJarEntry();
rootEntryPath = (jarEntry != null ? jarEntry.getName() : ""); rootEntryPath = (jarEntry != null ? jarEntry.getName() : "");
closeJarFile = !jarCon.getUseCaches();
} }
else { else {
// No JarURLConnection -> need to resort to URL file parsing. // No JarURLConnection -> need to resort to URL file parsing.
@ -581,7 +582,7 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
jarFileUrl = urlFile; jarFileUrl = urlFile;
rootEntryPath = ""; rootEntryPath = "";
} }
newJarFile = true; closeJarFile = true;
} }
catch (ZipException ex) { catch (ZipException ex) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
@ -614,9 +615,7 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
return result; return result;
} }
finally { finally {
// Close jar file, but only if freshly obtained - if (closeJarFile) {
// not from JarURLConnection, which might cache the file reference.
if (newJarFile) {
jarFile.close(); jarFile.close();
} }
} }