Skip to content

Commit bd74344

Browse files
committed
Revert "Don't close jar files early"
This reverts commit b42f056.
1 parent 674022d commit bd74344

File tree

9 files changed

+28
-158
lines changed

9 files changed

+28
-158
lines changed

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

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2021 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.
@@ -26,11 +26,8 @@
2626
import java.net.URLStreamHandler;
2727
import java.net.URLStreamHandlerFactory;
2828
import java.security.Permission;
29-
import java.util.ArrayList;
30-
import java.util.Collections;
3129
import java.util.Enumeration;
3230
import java.util.Iterator;
33-
import java.util.List;
3431
import java.util.Spliterator;
3532
import java.util.Spliterators;
3633
import java.util.function.Supplier;
@@ -96,8 +93,6 @@ public class JarFile extends AbstractJarFile implements Iterable<java.util.jar.J
9693

9794
private volatile JarFileWrapper wrapper;
9895

99-
private final List<JarFile> nestedJars = Collections.synchronizedList(new ArrayList<>());
100-
10196
/**
10297
* Create a new {@link JarFile} backed by the specified file.
10398
* @param file the root jar file
@@ -133,6 +128,9 @@ private JarFile(RandomAccessDataFile rootFile, String pathFromRoot, RandomAccess
133128
private JarFile(RandomAccessDataFile rootFile, String pathFromRoot, RandomAccessData data, JarEntryFilter filter,
134129
JarFileType type, Supplier<Manifest> manifestSupplier) throws IOException {
135130
super(rootFile.getFile());
131+
if (System.getSecurityManager() == null) {
132+
super.close();
133+
}
136134
this.rootFile = rootFile;
137135
this.pathFromRoot = pathFromRoot;
138136
CentralDirectoryParser parser = new CentralDirectoryParser();
@@ -144,7 +142,8 @@ private JarFile(RandomAccessDataFile rootFile, String pathFromRoot, RandomAccess
144142
}
145143
catch (RuntimeException ex) {
146144
try {
147-
close();
145+
this.rootFile.close();
146+
super.close();
148147
}
149148
catch (IOException ioex) {
150149
}
@@ -189,13 +188,8 @@ public void visitEnd() {
189188
JarFileWrapper getWrapper() throws IOException {
190189
JarFileWrapper wrapper = this.wrapper;
191190
if (wrapper == null) {
192-
synchronized (this) {
193-
if (this.wrapper != null) {
194-
return this.wrapper;
195-
}
196-
wrapper = new JarFileWrapper(this);
197-
this.wrapper = wrapper;
198-
}
191+
wrapper = new JarFileWrapper(this);
192+
this.wrapper = wrapper;
199193
}
200194
return wrapper;
201195
}
@@ -340,10 +334,8 @@ private JarFile createJarFileFromFileEntry(JarEntry entry) throws IOException {
340334
+ "mechanism used to create your executable jar file");
341335
}
342336
RandomAccessData entryData = this.entries.getEntryData(entry.getName());
343-
JarFile nestedJar = new JarFile(this.rootFile, this.pathFromRoot + "!/" + entry.getName(), entryData,
337+
return new JarFile(this.rootFile, this.pathFromRoot + "!/" + entry.getName(), entryData,
344338
JarFileType.NESTED_JAR);
345-
this.nestedJars.add(nestedJar);
346-
return nestedJar;
347339
}
348340

349341
@Override
@@ -363,19 +355,11 @@ public void close() throws IOException {
363355
if (this.closed) {
364356
return;
365357
}
366-
synchronized (this) {
367-
super.close();
368-
if (this.type == JarFileType.DIRECT) {
369-
this.rootFile.close();
370-
}
371-
if (this.wrapper != null) {
372-
this.wrapper.close();
373-
}
374-
for (JarFile nestedJar : this.nestedJars) {
375-
nestedJar.close();
376-
}
377-
this.closed = true;
358+
super.close();
359+
if (this.type == JarFileType.DIRECT) {
360+
this.rootFile.close();
378361
}
362+
this.closed = true;
379363
}
380364

381365
private void ensureOpen() {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2021 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.
@@ -40,6 +40,9 @@ class JarFileWrapper extends AbstractJarFile {
4040
JarFileWrapper(JarFile parent) throws IOException {
4141
super(parent.getRootJarFile().getFile());
4242
this.parent = parent;
43+
if (System.getSecurityManager() == null) {
44+
super.close();
45+
}
4346
}
4447

4548
@Override

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

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2021 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.
@@ -18,7 +18,6 @@
1818

1919
import java.io.ByteArrayOutputStream;
2020
import java.io.FileNotFoundException;
21-
import java.io.FilterInputStream;
2221
import java.io.IOException;
2322
import java.io.InputStream;
2423
import java.io.UnsupportedEncodingException;
@@ -166,7 +165,7 @@ public InputStream getInputStream() throws IOException {
166165
if (inputStream == null) {
167166
throwFileNotFound(this.jarEntryName, this.jarFile);
168167
}
169-
return new ConnectionInputStream(inputStream);
168+
return inputStream;
170169
}
171170

172171
private void throwFileNotFound(Object entry, AbstractJarFile jarFile) throws FileNotFoundException {
@@ -291,19 +290,6 @@ private static JarURLConnection notFound(JarFile jarFile, JarEntryName jarEntryN
291290
return new JarURLConnection(null, jarFile, jarEntryName);
292291
}
293292

294-
private class ConnectionInputStream extends FilterInputStream {
295-
296-
ConnectionInputStream(InputStream in) {
297-
super(in);
298-
}
299-
300-
@Override
301-
public void close() throws IOException {
302-
JarURLConnection.this.jarFile.close();
303-
}
304-
305-
}
306-
307293
/**
308294
* A JarEntryName parsed from a URL String.
309295
*/

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 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.
@@ -61,7 +61,6 @@ void setup(@TempDir File temp) throws Exception {
6161
@AfterEach
6262
void cleanup() throws Exception {
6363
this.parent.close();
64-
this.wrapper.close();
6564
}
6665

6766
private File createTempJar(File temp) throws IOException {

spring-boot-tests/spring-boot-integration-tests/spring-boot-loader-tests/build.gradle

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ dependencies {
1414
app project(path: ":spring-boot-project:spring-boot-dependencies", configuration: "mavenRepository")
1515
app project(path: ":spring-boot-project:spring-boot-tools:spring-boot-gradle-plugin", configuration: "mavenRepository")
1616
app project(path: ":spring-boot-project:spring-boot-starters:spring-boot-starter-web", configuration: "mavenRepository")
17-
app project(path: ":spring-boot-project:spring-boot-starters:spring-boot-starter", configuration: "mavenRepository")
18-
app("org.bouncycastle:bcprov-jdk15on:1.70")
1917

2018
intTestImplementation(enforcedPlatform(project(":spring-boot-project:spring-boot-parent")))
2119
intTestImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support"))
@@ -41,18 +39,6 @@ task buildApp(type: GradleBuild) {
4139
tasks = ["build"]
4240
}
4341

44-
task syncSignedJarUnpackAppSource(type: org.springframework.boot.build.SyncAppSource) {
45-
sourceDirectory = file("spring-boot-loader-tests-signed-jar-unpack-app")
46-
destinationDirectory = file("${buildDir}/spring-boot-loader-tests-signed-jar-unpack-app")
47-
}
48-
49-
task buildSignedJarUnpackApp(type: GradleBuild) {
50-
dependsOn syncSignedJarUnpackAppSource, syncMavenRepository
51-
dir = "${buildDir}/spring-boot-loader-tests-signed-jar-unpack-app"
52-
startParameter.buildCacheEnabled = false
53-
tasks = ["build"]
54-
}
55-
5642
intTest {
57-
dependsOn buildApp, buildSignedJarUnpackApp
43+
dependsOn buildApp
5844
}

spring-boot-tests/spring-boot-integration-tests/spring-boot-loader-tests/spring-boot-loader-tests-signed-jar-unpack-app/build.gradle

Lines changed: 0 additions & 22 deletions
This file was deleted.

spring-boot-tests/spring-boot-integration-tests/spring-boot-loader-tests/spring-boot-loader-tests-signed-jar-unpack-app/settings.gradle

Lines changed: 0 additions & 15 deletions
This file was deleted.

spring-boot-tests/spring-boot-integration-tests/spring-boot-loader-tests/spring-boot-loader-tests-signed-jar-unpack-app/src/main/java/org/springframework/boot/loaderapp/LoaderSignedJarTestApplication.java

Lines changed: 0 additions & 36 deletions
This file was deleted.

spring-boot-tests/spring-boot-integration-tests/spring-boot-loader-tests/src/intTest/java/org/springframework/boot/loader/LoaderIntegrationTests.java

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,40 +51,25 @@ class LoaderIntegrationTests {
5151
@ParameterizedTest
5252
@MethodSource("javaRuntimes")
5353
void readUrlsWithoutWarning(JavaRuntime javaRuntime) {
54-
try (GenericContainer<?> container = createContainer(javaRuntime, "spring-boot-loader-tests-app")) {
54+
try (GenericContainer<?> container = createContainer(javaRuntime)) {
5555
container.start();
5656
System.out.println(this.output.toUtf8String());
5757
assertThat(this.output.toUtf8String()).contains(">>>>> 287649 BYTES from").doesNotContain("WARNING:")
5858
.doesNotContain("illegal").doesNotContain("jar written to temp");
5959
}
6060
}
6161

62-
@ParameterizedTest
63-
@MethodSource("javaRuntimes")
64-
void runSignedJarWhenUnpacked(JavaRuntime javaRuntime) {
65-
try (GenericContainer<?> container = createContainer(javaRuntime,
66-
"spring-boot-loader-tests-signed-jar-unpack-app")) {
67-
container.start();
68-
System.out.println(this.output.toUtf8String());
69-
assertThat(this.output.toUtf8String()).contains("Legion of the Bouncy Castle");
70-
}
71-
}
72-
73-
private GenericContainer<?> createContainer(JavaRuntime javaRuntime, String name) {
62+
private GenericContainer<?> createContainer(JavaRuntime javaRuntime) {
7463
return javaRuntime.getContainer().withLogConsumer(this.output)
75-
.withCopyFileToContainer(findApplication(name), "/app.jar")
64+
.withCopyFileToContainer(MountableFile.forHostPath(findApplication().toPath()), "/app.jar")
7665
.withStartupCheckStrategy(new OneShotStartupCheckStrategy().withTimeout(Duration.ofMinutes(5)))
7766
.withCommand("java", "-jar", "app.jar");
7867
}
7968

80-
private MountableFile findApplication(String name) {
81-
return MountableFile.forHostPath(findJarFile(name).toPath());
82-
}
83-
84-
private File findJarFile(String name) {
85-
String path = String.format("build/%1$s/build/libs/%1$s.jar", name);
86-
File jar = new File(path);
87-
Assert.state(jar.isFile(), () -> "Could not find " + path + ". Have you built it?");
69+
private File findApplication() {
70+
String name = String.format("build/%1$s/build/libs/%1$s.jar", "spring-boot-loader-tests-app");
71+
File jar = new File(name);
72+
Assert.state(jar.isFile(), () -> "Could not find " + name + ". Have you built it?");
8873
return jar;
8974
}
9075

0 commit comments

Comments
 (0)