Skip to content

Commit ac9d2e7

Browse files
committed
Merge branch '3.2.x'
Closes gh-40643
2 parents 0f8062f + 7708ec7 commit ac9d2e7

File tree

4 files changed

+28
-25
lines changed

4 files changed

+28
-25
lines changed

spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/launch/Archive.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 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.
@@ -20,6 +20,7 @@
2020
import java.io.IOException;
2121
import java.net.URI;
2222
import java.net.URL;
23+
import java.nio.file.Path;
2324
import java.security.CodeSource;
2425
import java.security.ProtectionDomain;
2526
import java.util.Set;
@@ -107,11 +108,10 @@ static Archive create(Class<?> target) throws Exception {
107108
static Archive create(ProtectionDomain protectionDomain) throws Exception {
108109
CodeSource codeSource = protectionDomain.getCodeSource();
109110
URI location = (codeSource != null) ? codeSource.getLocation().toURI() : null;
110-
String path = (location != null) ? location.getSchemeSpecificPart() : null;
111-
if (path == null) {
111+
if (location == null) {
112112
throw new IllegalStateException("Unable to determine code source archive");
113113
}
114-
return create(new File(path));
114+
return create(Path.of(location).toFile());
115115
}
116116

117117
/**

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

+22-18
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@
5353
*/
5454
public record NestedLocation(Path path, String nestedEntryName) {
5555

56-
private static final Map<String, NestedLocation> cache = new ConcurrentHashMap<>();
56+
private static final Map<String, NestedLocation> locationCache = new ConcurrentHashMap<>();
57+
58+
private static final Map<String, Path> pathCache = new ConcurrentHashMap<>();
5759

5860
public NestedLocation(Path path, String nestedEntryName) {
5961
if (path == null) {
@@ -89,35 +91,37 @@ public static NestedLocation fromUri(URI uri) {
8991
return parse(uri.getSchemeSpecificPart());
9092
}
9193

92-
static NestedLocation parse(String path) {
93-
if (path == null || path.isEmpty()) {
94-
throw new IllegalArgumentException("'path' must not be empty");
94+
static NestedLocation parse(String location) {
95+
if (location == null || location.isEmpty()) {
96+
throw new IllegalArgumentException("'location' must not be empty");
9597
}
96-
int index = path.lastIndexOf("/!");
97-
return cache.computeIfAbsent(path, (l) -> create(index, l));
98+
return locationCache.computeIfAbsent(location, (key) -> create(location));
9899
}
99100

100-
private static NestedLocation create(int index, String location) {
101+
private static NestedLocation create(String location) {
102+
int index = location.lastIndexOf("/!");
101103
String locationPath = (index != -1) ? location.substring(0, index) : location;
102-
if (isWindows() && !isUncPath(location)) {
103-
while (locationPath.startsWith("/")) {
104-
locationPath = locationPath.substring(1, locationPath.length());
105-
}
106-
}
107104
String nestedEntryName = (index != -1) ? location.substring(index + 2) : null;
108-
return new NestedLocation((!locationPath.isEmpty()) ? Path.of(locationPath) : null, nestedEntryName);
105+
return new NestedLocation((!locationPath.isEmpty()) ? asPath(locationPath) : null, nestedEntryName);
109106
}
110107

111-
private static boolean isWindows() {
112-
return File.separatorChar == '\\';
108+
private static Path asPath(String locationPath) {
109+
return pathCache.computeIfAbsent(locationPath, (key) -> {
110+
if (isWindows() && locationPath.length() > 2 && locationPath.charAt(2) == ':') {
111+
// Use the same logic as Java's internal WindowsUriSupport class
112+
return Path.of(locationPath.substring(1));
113+
}
114+
return Path.of(locationPath);
115+
});
113116
}
114117

115-
private static boolean isUncPath(String input) {
116-
return !input.contains(":");
118+
private static boolean isWindows() {
119+
return File.separatorChar == '\\';
117120
}
118121

119122
static void clearCache() {
120-
cache.clear();
123+
locationCache.clear();
124+
pathCache.clear();
121125
}
122126

123127
}

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import java.util.Map;
3939
import java.util.Map.Entry;
4040

41-
import org.springframework.boot.loader.net.util.UrlDecoder;
4241
import org.springframework.boot.loader.ref.Cleaner;
4342

4443
/**
@@ -77,7 +76,7 @@ class NestedUrlConnection extends URLConnection {
7776

7877
private NestedLocation parseNestedLocation(URL url) throws MalformedURLException {
7978
try {
80-
return NestedLocation.parse(UrlDecoder.decode(url.getPath()));
79+
return NestedLocation.fromUrl(url);
8180
}
8281
catch (IllegalArgumentException ex) {
8382
throw new MalformedURLException(ex.getMessage());

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ void fromUrlWhenNotNestedProtocolThrowsException() {
8585
@Test
8686
void fromUrlWhenNoPathThrowsException() {
8787
assertThatIllegalArgumentException().isThrownBy(() -> NestedLocation.fromUrl(new URL("nested:")))
88-
.withMessageContaining("'path' must not be empty");
88+
.withMessageContaining("'location' must not be empty");
8989
}
9090

9191
@Test

0 commit comments

Comments
 (0)