Skip to content

Commit cc33e23

Browse files
committed
Merge branch '2.2.x'
Closes gh-21074
2 parents 0699116 + 6bf9332 commit cc33e23

File tree

4 files changed

+59
-18
lines changed

4 files changed

+59
-18
lines changed

spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFile.java

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public class JarFile extends java.util.jar.JarFile implements Iterable<java.util
6060

6161
private static final AsciiBytes SIGNATURE_FILE_EXTENSION = new AsciiBytes(".SF");
6262

63+
private final JarFile parent;
64+
6365
private final RandomAccessDataFile rootFile;
6466

6567
private final String pathFromRoot;
@@ -100,6 +102,27 @@ public JarFile(File file) throws IOException {
100102
this(file, "", file, JarFileType.DIRECT);
101103
}
102104

105+
/**
106+
* Create a new JarFile copy based on a given parent.
107+
* @param parent the parent jar
108+
* @throws IOException if the file cannot be read
109+
*/
110+
JarFile(JarFile parent) throws IOException {
111+
super(parent.rootFile.getFile());
112+
this.parent = parent;
113+
this.rootFile = parent.rootFile;
114+
this.pathFromRoot = parent.pathFromRoot;
115+
this.data = parent.data;
116+
this.type = parent.type;
117+
this.url = parent.url;
118+
this.urlString = parent.urlString;
119+
this.entries = parent.entries;
120+
this.manifestSupplier = parent.manifestSupplier;
121+
this.manifest = parent.manifest;
122+
this.signed = parent.signed;
123+
this.comment = parent.comment;
124+
}
125+
103126
/**
104127
* Private constructor used to create a new {@link JarFile} either directly or from a
105128
* nested entry.
@@ -111,12 +134,13 @@ public JarFile(File file) throws IOException {
111134
*/
112135
private JarFile(RandomAccessDataFile rootFile, String pathFromRoot, RandomAccessData data, JarFileType type)
113136
throws IOException {
114-
this(rootFile, pathFromRoot, data, null, type, null);
137+
this(null, rootFile, pathFromRoot, data, null, type, null);
115138
}
116139

117-
private JarFile(RandomAccessDataFile rootFile, String pathFromRoot, RandomAccessData data, JarEntryFilter filter,
118-
JarFileType type, Supplier<Manifest> manifestSupplier) throws IOException {
140+
private JarFile(JarFile parent, RandomAccessDataFile rootFile, String pathFromRoot, RandomAccessData data,
141+
JarEntryFilter filter, JarFileType type, Supplier<Manifest> manifestSupplier) throws IOException {
119142
super(rootFile.getFile());
143+
this.parent = parent;
120144
this.rootFile = rootFile;
121145
this.pathFromRoot = pathFromRoot;
122146
CentralDirectoryParser parser = new CentralDirectoryParser();
@@ -166,6 +190,10 @@ public void visitEnd() {
166190
};
167191
}
168192

193+
JarFile getParent() {
194+
return this.parent;
195+
}
196+
169197
protected final RandomAccessDataFile getRootJarFile() {
170198
return this.rootFile;
171199
}
@@ -275,8 +303,9 @@ private JarFile createJarFileFromDirectoryEntry(JarEntry entry) throws IOExcepti
275303
}
276304
return null;
277305
};
278-
return new JarFile(this.rootFile, this.pathFromRoot + "!/" + entry.getName().substring(0, name.length() - 1),
279-
this.data, filter, JarFileType.NESTED_DIRECTORY, this.manifestSupplier);
306+
return new JarFile(this, this.rootFile,
307+
this.pathFromRoot + "!/" + entry.getName().substring(0, name.length() - 1), this.data, filter,
308+
JarFileType.NESTED_DIRECTORY, this.manifestSupplier);
280309
}
281310

282311
private JarFile createJarFileFromFileEntry(JarEntry entry) throws IOException {
@@ -304,7 +333,7 @@ public int size() {
304333
@Override
305334
public void close() throws IOException {
306335
super.close();
307-
if (this.type == JarFileType.DIRECT) {
336+
if (this.type == JarFileType.DIRECT && this.parent == null) {
308337
this.rootFile.close();
309338
}
310339
}
@@ -324,10 +353,9 @@ String getUrlString() throws MalformedURLException {
324353
*/
325354
public URL getUrl() throws MalformedURLException {
326355
if (this.url == null) {
327-
Handler handler = new Handler(this);
328356
String file = this.rootFile.getFile().toURI() + this.pathFromRoot + "!/";
329357
file = file.replace("file:////", "file://"); // Fix UNC paths
330-
this.url = new URL("jar", "", -1, file, handler);
358+
this.url = new URL("jar", "", -1, file, new Handler(this));
331359
}
332360
return this.url;
333361
}

spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarURLConnection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -286,7 +286,7 @@ static JarURLConnection get(URL url, JarFile jarFile) throws IOException {
286286
}
287287
return NOT_FOUND_CONNECTION;
288288
}
289-
return new JarURLConnection(url, connectionJarFile, jarEntryName,
289+
return new JarURLConnection(url, new JarFile(connectionJarFile), jarEntryName,
290290
(connectionJarFile != jarFile) ? connectionJarFile::close : null);
291291
}
292292

spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarFileTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,10 @@ void getUrl() throws Exception {
218218
URL url = this.jarFile.getUrl();
219219
assertThat(url.toString()).isEqualTo("jar:" + this.rootJarFile.toURI() + "!/");
220220
JarURLConnection jarURLConnection = (JarURLConnection) url.openConnection();
221-
assertThat(jarURLConnection.getJarFile()).isSameAs(this.jarFile);
221+
assertThat(jarURLConnection.getJarFile().getParent()).isSameAs(this.jarFile);
222222
assertThat(jarURLConnection.getJarEntry()).isNull();
223223
assertThat(jarURLConnection.getContentLength()).isGreaterThan(1);
224-
assertThat(jarURLConnection.getContent()).isSameAs(this.jarFile);
224+
assertThat(((JarFile) jarURLConnection.getContent()).getParent()).isSameAs(this.jarFile);
225225
assertThat(jarURLConnection.getContentType()).isEqualTo("x-java/jar");
226226
assertThat(jarURLConnection.getJarFileURL().toURI()).isEqualTo(this.rootJarFile.toURI());
227227
}
@@ -231,7 +231,7 @@ void createEntryUrl() throws Exception {
231231
URL url = new URL(this.jarFile.getUrl(), "1.dat");
232232
assertThat(url.toString()).isEqualTo("jar:" + this.rootJarFile.toURI() + "!/1.dat");
233233
JarURLConnection jarURLConnection = (JarURLConnection) url.openConnection();
234-
assertThat(jarURLConnection.getJarFile()).isSameAs(this.jarFile);
234+
assertThat(jarURLConnection.getJarFile().getParent()).isSameAs(this.jarFile);
235235
assertThat(jarURLConnection.getJarEntry()).isSameAs(this.jarFile.getJarEntry("1.dat"));
236236
assertThat(jarURLConnection.getContentLength()).isEqualTo(1);
237237
assertThat(jarURLConnection.getContent()).isInstanceOf(InputStream.class);
@@ -285,7 +285,7 @@ void getNestedJarFile() throws Exception {
285285
URL url = nestedJarFile.getUrl();
286286
assertThat(url.toString()).isEqualTo("jar:" + this.rootJarFile.toURI() + "!/nested.jar!/");
287287
JarURLConnection conn = (JarURLConnection) url.openConnection();
288-
assertThat(conn.getJarFile()).isSameAs(nestedJarFile);
288+
assertThat(conn.getJarFile().getParent()).isSameAs(nestedJarFile);
289289
assertThat(conn.getJarFileURL().toString()).isEqualTo("jar:" + this.rootJarFile.toURI() + "!/nested.jar");
290290
assertThat(conn.getInputStream()).isNotNull();
291291
JarInputStream jarInputStream = new JarInputStream(conn.getInputStream());
@@ -314,7 +314,7 @@ void getNestedJarDirectory() throws Exception {
314314

315315
URL url = nestedJarFile.getUrl();
316316
assertThat(url.toString()).isEqualTo("jar:" + this.rootJarFile.toURI() + "!/d!/");
317-
assertThat(((JarURLConnection) url.openConnection()).getJarFile()).isSameAs(nestedJarFile);
317+
assertThat(((JarURLConnection) url.openConnection()).getJarFile().getParent()).isSameAs(nestedJarFile);
318318
}
319319
}
320320

spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarURLConnectionTests.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121
import java.io.FileNotFoundException;
2222
import java.io.InputStream;
2323
import java.net.URL;
24+
import java.util.zip.ZipFile;
2425

2526
import org.junit.jupiter.api.AfterEach;
2627
import org.junit.jupiter.api.BeforeEach;
@@ -29,6 +30,7 @@
2930

3031
import org.springframework.boot.loader.TestJarCreator;
3132
import org.springframework.boot.loader.jar.JarURLConnection.JarEntryName;
33+
import org.springframework.test.util.ReflectionTestUtils;
3234

3335
import static org.assertj.core.api.Assertions.assertThat;
3436
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -61,13 +63,15 @@ void tearDown() throws Exception {
6163
@Test
6264
void connectionToRootUsingAbsoluteUrl() throws Exception {
6365
URL url = new URL("jar:" + this.rootJarFile.toURI().toURL() + "!/");
64-
assertThat(JarURLConnection.get(url, this.jarFile).getContent()).isSameAs(this.jarFile);
66+
Object content = JarURLConnection.get(url, this.jarFile).getContent();
67+
assertThat(((JarFile) content).getParent()).isSameAs(this.jarFile);
6568
}
6669

6770
@Test
6871
void connectionToRootUsingRelativeUrl() throws Exception {
6972
URL url = new URL("jar:file:" + getRelativePath() + "!/");
70-
assertThat(JarURLConnection.get(url, this.jarFile).getContent()).isSameAs(this.jarFile);
73+
Object content = JarURLConnection.get(url, this.jarFile).getContent();
74+
assertThat(((JarFile) content).getParent()).isSameAs(this.jarFile);
7175
}
7276

7377
@Test
@@ -220,6 +224,15 @@ void jarEntryNameWithMixtureOfEncodedAndUnencodedDoubleByteCharacters() {
220224
.isEqualTo("\u00e1/b/\u00c7.class");
221225
}
222226

227+
@Test
228+
void openConnectionCanBeClosedWithoutClosingSourceJar() throws Exception {
229+
URL url = new URL("jar:" + this.rootJarFile.toURI().toURL() + "!/");
230+
JarURLConnection connection = JarURLConnection.get(url, this.jarFile);
231+
JarFile connectionJarFile = connection.getJarFile();
232+
connectionJarFile.close();
233+
assertThat((Boolean) ReflectionTestUtils.getField(this.jarFile, ZipFile.class, "closeRequested")).isFalse();
234+
}
235+
223236
private String getRelativePath() {
224237
return this.rootJarFile.getPath().replace('\\', '/');
225238
}

0 commit comments

Comments
 (0)