Skip to content

Commit a9efa96

Browse files
committed
Merge branch '3.1.x'
Closes gh-38770
2 parents b4a4e91 + da31137 commit a9efa96

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/StaticResourceJars.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ private void addUrlConnection(List<URL> urls, URL url, URLConnection connection)
115115

116116
private boolean isResourcesJar(JarURLConnection connection) {
117117
try {
118-
return isResourcesJar(connection.getJarFile());
118+
return isResourcesJar(connection.getJarFile(), !connection.getUseCaches());
119119
}
120120
catch (IOException ex) {
121121
return false;
@@ -124,16 +124,21 @@ private boolean isResourcesJar(JarURLConnection connection) {
124124

125125
private boolean isResourcesJar(File file) {
126126
try {
127-
return isResourcesJar(new JarFile(file));
127+
return isResourcesJar(new JarFile(file), true);
128128
}
129129
catch (IOException | InvalidPathException ex) {
130130
return false;
131131
}
132132
}
133133

134-
private boolean isResourcesJar(JarFile jar) throws IOException {
135-
try (jar) {
136-
return jar.getName().endsWith(".jar") && (jar.getJarEntry("META-INF/resources") != null);
134+
private boolean isResourcesJar(JarFile jarFile, boolean closeJarFile) throws IOException {
135+
try {
136+
return jarFile.getName().endsWith(".jar") && (jarFile.getJarEntry("META-INF/resources") != null);
137+
}
138+
finally {
139+
if (closeJarFile) {
140+
jarFile.close();
141+
}
137142
}
138143
}
139144

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/StaticResourceJarsTests.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
import java.io.File;
2020
import java.io.FileOutputStream;
2121
import java.io.IOException;
22+
import java.net.JarURLConnection;
2223
import java.net.URL;
24+
import java.net.URLConnection;
25+
import java.net.URLStreamHandler;
2326
import java.util.List;
2427
import java.util.function.Consumer;
2528
import java.util.jar.JarEntry;
@@ -29,6 +32,8 @@
2932
import org.junit.jupiter.api.io.TempDir;
3033

3134
import static org.assertj.core.api.Assertions.assertThat;
35+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
36+
import static org.assertj.core.api.Assertions.assertThatNoException;
3237

3338
/**
3439
* Tests for {@link StaticResourceJars}.
@@ -86,6 +91,27 @@ void ignoreWildcardUrls() throws Exception {
8691
assertThat(staticResourceJarUrls).isEmpty();
8792
}
8893

94+
@Test
95+
void doesNotCloseJarFromCachedConnection() throws Exception {
96+
File jarFile = createResourcesJar("test-resources.jar");
97+
TrackedURLStreamHandler handler = new TrackedURLStreamHandler(true);
98+
URL url = new URL("jar", null, 0, jarFile.toURI().toURL().toString() + "!/", handler);
99+
new StaticResourceJars().getUrlsFrom(url);
100+
assertThatNoException()
101+
.isThrownBy(() -> ((JarURLConnection) handler.getConnection()).getJarFile().getComment());
102+
}
103+
104+
@Test
105+
void closesJarFromNonCachedConnection() throws Exception {
106+
File jarFile = createResourcesJar("test-resources.jar");
107+
TrackedURLStreamHandler handler = new TrackedURLStreamHandler(false);
108+
URL url = new URL("jar", null, 0, jarFile.toURI().toURL().toString() + "!/", handler);
109+
new StaticResourceJars().getUrlsFrom(url);
110+
assertThatIllegalStateException()
111+
.isThrownBy(() -> ((JarURLConnection) handler.getConnection()).getJarFile().getComment())
112+
.withMessageContaining("closed");
113+
}
114+
89115
private File createResourcesJar(String name) throws IOException {
90116
return createJar(name, (output) -> {
91117
JarEntry jarEntry = new JarEntry("META-INF/resources");
@@ -113,4 +139,27 @@ private File createJar(String name, Consumer<JarOutputStream> customizer) throws
113139
return jarFile;
114140
}
115141

142+
private static class TrackedURLStreamHandler extends URLStreamHandler {
143+
144+
private final boolean useCaches;
145+
146+
private URLConnection connection;
147+
148+
TrackedURLStreamHandler(boolean useCaches) {
149+
this.useCaches = useCaches;
150+
}
151+
152+
@Override
153+
protected URLConnection openConnection(URL u) throws IOException {
154+
this.connection = new URL(u.toExternalForm()).openConnection();
155+
this.connection.setUseCaches(this.useCaches);
156+
return this.connection;
157+
}
158+
159+
URLConnection getConnection() {
160+
return this.connection;
161+
}
162+
163+
}
164+
116165
}

0 commit comments

Comments
 (0)