Skip to content

Commit a266e1b

Browse files
committed
Close JarFile only in case of useCaches=false
Closes gh-34955
1 parent 4d09eb5 commit a266e1b

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

spring-core/src/main/java/org/springframework/core/io/AbstractFileResolvingResource.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
*/
4646
public abstract class AbstractFileResolvingResource extends AbstractResource {
4747

48-
@SuppressWarnings("try")
4948
@Override
5049
public boolean exists() {
5150
try {
@@ -90,9 +89,15 @@ else if (code == HttpURLConnection.HTTP_NOT_FOUND) {
9089
// existence of the entry (or the jar root in case of no entryName).
9190
// getJarFile() called for enforced presence check of the jar file,
9291
// throwing a NoSuchFileException otherwise (turned to false below).
93-
try (JarFile jarFile = jarCon.getJarFile()) {
92+
JarFile jarFile = jarCon.getJarFile();
93+
try {
9494
return (jarCon.getEntryName() == null || jarCon.getJarEntry() != null);
9595
}
96+
finally {
97+
if (!jarCon.getUseCaches()) {
98+
jarFile.close();
99+
}
100+
}
96101
}
97102
else if (con.getContentLengthLong() > 0) {
98103
return true;

spring-core/src/test/java/org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
import java.util.zip.ZipEntry;
4545

4646
import org.apache.commons.logging.LogFactory;
47+
import org.junit.jupiter.api.AfterAll;
48+
import org.junit.jupiter.api.BeforeAll;
4749
import org.junit.jupiter.api.Nested;
4850
import org.junit.jupiter.api.Test;
4951
import org.junit.jupiter.api.io.TempDir;
@@ -298,8 +300,8 @@ void classpathStarWithPatternInJar() {
298300
@Test
299301
void rootPatternRetrievalInJarFiles() throws IOException {
300302
assertThat(resolver.getResources("classpath*:aspectj*.dtd")).extracting(Resource::getFilename)
301-
.as("Could not find aspectj_1_5_0.dtd in the root of the aspectjweaver jar")
302-
.containsExactly("aspectj_1_5_0.dtd");
303+
.as("Could not find aspectj_1_5_0.dtd in the root of the aspectjweaver jar")
304+
.containsExactly("aspectj_1_5_0.dtd");
303305
}
304306
}
305307

@@ -310,6 +312,16 @@ class ClassPathManifestEntries {
310312
@TempDir
311313
Path temp;
312314

315+
@BeforeAll
316+
static void suppressJarCaches() {
317+
URLConnection.setDefaultUseCaches("jar", false);
318+
}
319+
320+
@AfterAll
321+
static void restoreJarCaches() {
322+
URLConnection.setDefaultUseCaches("jar", true);
323+
}
324+
313325
@Test
314326
void javaDashJarFindsClassPathManifestEntries() throws Exception {
315327
Path lib = this.temp.resolve("lib");
@@ -333,13 +345,22 @@ private void writeAssetJar(Path path) throws Exception {
333345
StreamUtils.copy("test", StandardCharsets.UTF_8, jar);
334346
jar.closeEntry();
335347
}
348+
336349
assertThat(new FileSystemResource(path).exists()).isTrue();
337350
assertThat(new UrlResource(ResourceUtils.JAR_URL_PREFIX + ResourceUtils.FILE_URL_PREFIX + path + ResourceUtils.JAR_URL_SEPARATOR).exists()).isTrue();
338351
assertThat(new UrlResource(ResourceUtils.JAR_URL_PREFIX + ResourceUtils.FILE_URL_PREFIX + path + ResourceUtils.JAR_URL_SEPARATOR + "assets/file.txt").exists()).isTrue();
339352
assertThat(new UrlResource(ResourceUtils.JAR_URL_PREFIX + ResourceUtils.FILE_URL_PREFIX + path + ResourceUtils.JAR_URL_SEPARATOR + "assets/none.txt").exists()).isFalse();
340353
assertThat(new UrlResource(ResourceUtils.JAR_URL_PREFIX + ResourceUtils.FILE_URL_PREFIX + "X" + path + ResourceUtils.JAR_URL_SEPARATOR).exists()).isFalse();
341354
assertThat(new UrlResource(ResourceUtils.JAR_URL_PREFIX + ResourceUtils.FILE_URL_PREFIX + "X" + path + ResourceUtils.JAR_URL_SEPARATOR + "assets/file.txt").exists()).isFalse();
342355
assertThat(new UrlResource(ResourceUtils.JAR_URL_PREFIX + ResourceUtils.FILE_URL_PREFIX + "X" + path + ResourceUtils.JAR_URL_SEPARATOR + "assets/none.txt").exists()).isFalse();
356+
357+
Resource resource = new UrlResource(ResourceUtils.JAR_URL_PREFIX + ResourceUtils.FILE_URL_PREFIX + path + ResourceUtils.JAR_URL_SEPARATOR + "assets/file.txt");
358+
try (InputStream is = resource.getInputStream()) {
359+
assertThat(resource.exists()).isTrue();
360+
assertThat(resource.createRelative("file.txt").exists()).isTrue();
361+
assertThat(new UrlResource(ResourceUtils.JAR_URL_PREFIX + ResourceUtils.FILE_URL_PREFIX + path + ResourceUtils.JAR_URL_SEPARATOR).exists()).isTrue();
362+
is.readAllBytes();
363+
}
343364
}
344365

345366
private void writeApplicationJar(Path path) throws Exception {

0 commit comments

Comments
 (0)