Skip to content

Commit

Permalink
Hacking
Browse files Browse the repository at this point in the history
  • Loading branch information
philwebb committed May 8, 2024
1 parent 043720a commit fab8542
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.util.Set;
Expand Down Expand Up @@ -107,11 +108,10 @@ static Archive create(Class<?> target) throws Exception {
static Archive create(ProtectionDomain protectionDomain) throws Exception {
CodeSource codeSource = protectionDomain.getCodeSource();
URI location = (codeSource != null) ? codeSource.getLocation().toURI() : null;
String path = (location != null) ? location.getSchemeSpecificPart() : null;
if (path == null) {
if (location == null) {
throw new IllegalStateException("Unable to determine code source archive");
}
return create(new File(path));
return create(Path.of(location).toFile());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

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

import java.io.File;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
Expand Down Expand Up @@ -52,7 +53,9 @@
*/
public record NestedLocation(Path path, String nestedEntryName) {

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

private static final Map<String, Path> pathCache = new ConcurrentHashMap<>();

public NestedLocation(Path path, String nestedEntryName) {
if (path == null) {
Expand Down Expand Up @@ -88,22 +91,37 @@ public static NestedLocation fromUri(URI uri) {
return parse(uri.getSchemeSpecificPart());
}

static NestedLocation parse(String path) {
if (path == null || path.isEmpty()) {
throw new IllegalArgumentException("'path' must not be empty");
static NestedLocation parse(String location) {
if (location == null || location.isEmpty()) {
throw new IllegalArgumentException("'location' must not be empty");
}
int index = path.lastIndexOf("/!");
return cache.computeIfAbsent(path, (location) -> create(location, index));
return locationCache.computeIfAbsent(location, (key) -> create(location));
}

private static NestedLocation create(String location, int index) {
private static NestedLocation create(String location) {
int index = location.lastIndexOf("/!");
String locationPath = (index != -1) ? location.substring(0, index) : location;
String nestedEntryName = (index != -1) ? location.substring(index + 2) : null;
return new NestedLocation((!locationPath.isEmpty()) ? Path.of(locationPath) : null, nestedEntryName);
return new NestedLocation((!locationPath.isEmpty()) ? asPath(locationPath) : null, nestedEntryName);
}

private static Path asPath(String locationPath) {
return pathCache.computeIfAbsent(locationPath, (key) -> {
if (isWindows() && locationPath.length() > 2 && locationPath.charAt(2) == ':') {
// Use the same logic as Java's internal WindowsUriSupport class
return Path.of(locationPath.substring(1));
}
return Path.of(locationPath);
});
}

private static boolean isWindows() {
return File.separatorChar == '\\';
}

static void clearCache() {
cache.clear();
locationCache.clear();
pathCache.clear();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void fromUrlWhenNotNestedProtocolThrowsException() {
@Test
void fromUrlWhenNoPathThrowsException() {
assertThatIllegalArgumentException().isThrownBy(() -> NestedLocation.fromUrl(new URL("nested:")))
.withMessageContaining("'path' must not be empty");
.withMessageContaining("'location' must not be empty");
}

@Test
Expand Down

0 comments on commit fab8542

Please sign in to comment.