Skip to content

Commit 4b495ca

Browse files
committed
Change NestedLocation to hold a Path rather than a File
Refactor `NestedLocation` so that it holds a `Path` rather than a `File`. See gh-37668
1 parent ec6415f commit 4b495ca

File tree

5 files changed

+28
-18
lines changed

5 files changed

+28
-18
lines changed

spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/net/protocol/jar/UrlJarFileFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private boolean isNestedUrl(URL url) {
8282
private JarFile createJarFileForNested(URL url, Runtime.Version version, Consumer<JarFile> closeAction)
8383
throws IOException {
8484
NestedLocation location = NestedLocation.fromUrl(url);
85-
return new UrlNestedJarFile(location.file(), location.nestedEntryName(), version, closeAction);
85+
return new UrlNestedJarFile(location.path().toFile(), location.nestedEntryName(), version, closeAction);
8686
}
8787

8888
private JarFile createJarFileForStream(URL url, Version version, Consumer<JarFile> closeAction) throws IOException {

spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/net/protocol/nested/NestedLocation.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616

1717
package org.springframework.boot.loader.net.protocol.nested;
1818

19-
import java.io.File;
19+
import java.net.URI;
2020
import java.net.URL;
21+
import java.nio.file.Path;
2122
import java.util.Map;
2223
import java.util.concurrent.ConcurrentHashMap;
2324

@@ -43,18 +44,18 @@
4344
* uncompressed entry that contains the nested jar, or a directory entry. The entry must
4445
* not start with a {@code '/'}.
4546
*
46-
* @param file the zip file that contains the nested entry
47+
* @param path the path to the zip that contains the nested entry
4748
* @param nestedEntryName the nested entry name
4849
* @author Phillip Webb
4950
* @since 3.2.0
5051
*/
51-
public record NestedLocation(File file, String nestedEntryName) {
52+
public record NestedLocation(Path path, String nestedEntryName) {
5253

5354
private static final Map<String, NestedLocation> cache = new ConcurrentHashMap<>();
5455

5556
public NestedLocation {
56-
if (file == null) {
57-
throw new IllegalArgumentException("'file' must not be null");
57+
if (path == null) {
58+
throw new IllegalArgumentException("'path' must not be null");
5859
}
5960
if (nestedEntryName == null || nestedEntryName.trim().isEmpty()) {
6061
throw new IllegalArgumentException("'nestedEntryName' must not be empty");
@@ -86,9 +87,9 @@ static NestedLocation parse(String path) {
8687
}
8788

8889
private static NestedLocation create(int index, String location) {
89-
String file = location.substring(0, index);
90+
String path = location.substring(0, index);
9091
String nestedEntryName = location.substring(index + 2);
91-
return new NestedLocation((!file.isEmpty()) ? new File(file) : null, nestedEntryName);
92+
return new NestedLocation((!path.isEmpty()) ? Path.of(path) : null, nestedEntryName);
9293
}
9394

9495
static void clearCache() {

spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/net/protocol/nested/NestedUrlConnection.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.loader.net.protocol.nested;
1818

19+
import java.io.File;
1920
import java.io.FilePermission;
2021
import java.io.FilterInputStream;
2122
import java.io.IOException;
@@ -25,6 +26,7 @@
2526
import java.net.MalformedURLException;
2627
import java.net.URL;
2728
import java.net.URLConnection;
29+
import java.nio.file.Files;
2830
import java.security.Permission;
2931

3032
import org.springframework.boot.loader.ref.Cleaner;
@@ -43,7 +45,7 @@ class NestedUrlConnection extends URLConnection {
4345

4446
private final Cleanable cleanup;
4547

46-
private long lastModified;
48+
private long lastModified = -1;
4749

4850
private FilePermission permission;
4951

@@ -91,16 +93,22 @@ public String getContentType() {
9193

9294
@Override
9395
public long getLastModified() {
94-
if (this.lastModified == 0) {
95-
this.lastModified = this.resources.getLocation().file().lastModified();
96+
if (this.lastModified == -1) {
97+
try {
98+
this.lastModified = Files.getLastModifiedTime(this.resources.getLocation().path()).toMillis();
99+
}
100+
catch (IOException ex) {
101+
this.lastModified = 0;
102+
}
96103
}
97104
return this.lastModified;
98105
}
99106

100107
@Override
101108
public Permission getPermission() throws IOException {
102109
if (this.permission == null) {
103-
this.permission = new FilePermission(this.resources.getLocation().file().getCanonicalPath(), "read");
110+
File file = this.resources.getLocation().path().toFile();
111+
this.permission = new FilePermission(file.getCanonicalPath(), "read");
104112
}
105113
return this.permission;
106114
}

spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/net/protocol/nested/NestedUrlConnectionResources.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ NestedLocation getLocation() {
5151
void connect() throws IOException {
5252
synchronized (this) {
5353
if (this.zipContent == null) {
54-
this.zipContent = ZipContent.open(this.location.file().toPath(), this.location.nestedEntryName());
54+
this.zipContent = ZipContent.open(this.location.path(), this.location.nestedEntryName());
5555
try {
5656
connectData();
5757
}

spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/net/protocol/nested/NestedLocationTests.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.File;
2020
import java.net.URL;
21+
import java.nio.file.Path;
2122

2223
import org.junit.jupiter.api.BeforeAll;
2324
import org.junit.jupiter.api.Test;
@@ -44,20 +45,20 @@ static void registerHandlers() {
4445
}
4546

4647
@Test
47-
void createWhenFileIsNullThrowsException() {
48+
void createWhenPathIsNullThrowsException() {
4849
assertThatIllegalArgumentException().isThrownBy(() -> new NestedLocation(null, "nested.jar"))
49-
.withMessageContaining("'file' must not be null");
50+
.withMessageContaining("'path' must not be null");
5051
}
5152

5253
@Test
5354
void createWhenNestedEntryNameIsNullThrowsException() {
54-
assertThatIllegalArgumentException().isThrownBy(() -> new NestedLocation(new File("test.jar"), null))
55+
assertThatIllegalArgumentException().isThrownBy(() -> new NestedLocation(Path.of("test.jar"), null))
5556
.withMessageContaining("'nestedEntryName' must not be empty");
5657
}
5758

5859
@Test
5960
void createWhenNestedEntryNameIsEmptyThrowsException() {
60-
assertThatIllegalArgumentException().isThrownBy(() -> new NestedLocation(new File("test.jar"), null))
61+
assertThatIllegalArgumentException().isThrownBy(() -> new NestedLocation(Path.of("test.jar"), null))
6162
.withMessageContaining("'nestedEntryName' must not be empty");
6263
}
6364

@@ -91,7 +92,7 @@ void fromUrlReturnsNestedLocation() throws Exception {
9192
File file = new File(this.temp, "test.jar");
9293
NestedLocation location = NestedLocation
9394
.fromUrl(new URL("nested:" + file.getAbsolutePath() + "/!lib/nested.jar"));
94-
assertThat(location.file()).isEqualTo(file);
95+
assertThat(location.path()).isEqualTo(file.toPath());
9596
assertThat(location.nestedEntryName()).isEqualTo("lib/nested.jar");
9697
}
9798

0 commit comments

Comments
 (0)