Skip to content

Commit

Permalink
Defensively close jar files from non-cached JarURLConnections
Browse files Browse the repository at this point in the history
Issue: SPR-6295
  • Loading branch information
jhoeller committed Feb 25, 2016
1 parent 1bc1df2 commit 4c96447
Showing 1 changed file with 4 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ protected Set<Resource> 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.
Expand All @@ -562,6 +562,7 @@ protected Set<Resource> 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.
Expand All @@ -581,7 +582,7 @@ protected Set<Resource> doFindPathMatchingJarResources(Resource rootDirResource,
jarFileUrl = urlFile;
rootEntryPath = "";
}
newJarFile = true;
closeJarFile = true;
}
catch (ZipException ex) {
if (logger.isDebugEnabled()) {
Expand Down Expand Up @@ -614,9 +615,7 @@ protected Set<Resource> 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();
}
}
Expand Down

0 comments on commit 4c96447

Please sign in to comment.