Skip to content

Commit

Permalink
Allow ClassLoader to return multiple resources
Browse files Browse the repository at this point in the history
Previously if the resources were in the same PathTree only one would be
returned.

Fixes #40371
  • Loading branch information
stuartwdouglas committed Apr 30, 2024
1 parent db219a8 commit 9daa467
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ public void accept(String relativePath, Consumer<PathVisit> func) {
delegate.accept(relativePath, func);
}

@Override
public void acceptAll(String relativePath, Consumer<PathVisit> func) {
delegate.acceptAll(relativePath, func);
}

@Override
public boolean contains(String relativePath) {
final LinkedHashMap<String, PathVisitSnapshot> snapshot = walkSnapshot;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ public void accept(String relativePath, Consumer<PathVisit> consumer) {
}
}

@Override
public void acceptAll(String relativePath, Consumer<PathVisit> consumer) {
if (!PathFilter.isVisible(filter, relativePath)) {
consumer.accept(null);
} else {
original.acceptAll(relativePath, consumer);
}
}

@Override
public boolean contains(String relativePath) {
return PathFilter.isVisible(filter, relativePath) && original.contains(relativePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,26 @@ public void accept(PathVisit t) {
}
}

@Override
public void acceptAll(String relativePath, Consumer<PathVisit> func) {
final AtomicBoolean consumed = new AtomicBoolean();
final Consumer<PathVisit> wrapper = new Consumer<>() {
@Override
public void accept(PathVisit t) {
if (t != null) {
func.accept(t);
consumed.set(true);
}
}
};
for (PathTree tree : trees) {
tree.accept(relativePath, wrapper);
}
if (!consumed.get()) {
func.accept(null);
}
}

@Override
public boolean contains(String relativePath) {
for (PathTree tree : trees) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,20 @@ default boolean isEmpty() {
*/
void accept(String relativePath, Consumer<PathVisit> consumer);

/**
* Consumes a given path relative to the root of the tree.
* If the path isn't found in the tree, the {@link PathVisit} argument
* passed to the consumer will be {@code null}.
*
* If multiple items match then the consumer will be called multiple times.
*
* @param relativePath relative path to consume
* @param consumer path consumer
*/
default void acceptAll(String relativePath, Consumer<PathVisit> consumer) {
accept(relativePath, consumer);
}

/**
* Checks whether the tree contains a relative path.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ public void accept(String relativePath, Consumer<PathVisit> consumer) {
delegate.accept(relativePath, consumer);
}

@Override
public void acceptAll(String relativePath, Consumer<PathVisit> consumer) {
delegate.acceptAll(relativePath, consumer);
}

@Override
public boolean contains(String relativePath) {
return delegate.contains(relativePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.nio.file.Path;
import java.security.ProtectionDomain;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.jar.Manifest;
Expand Down Expand Up @@ -141,4 +142,8 @@ public void close() {

}
};

default List<ClassPathResource> getResources(String name) {
return List.of(getResource(name));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.security.cert.Certificate;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.locks.ReadWriteLock;
Expand Down Expand Up @@ -106,6 +108,26 @@ public ClassPathResource getResource(String name) {
return apply(tree -> tree.apply(sanitized, visit -> visit == null ? null : new Resource(visit)));
}

@Override
public List<ClassPathResource> getResources(String name) {
final String sanitized = sanitize(name);
final Set<String> resources = this.resources;
if (resources != null && !resources.contains(sanitized)) {
return null;
}
List<ClassPathResource> ret = new ArrayList<>();
apply(tree -> {
tree.acceptAll(sanitized, visit -> {
if (visit != null) {
ret.add(new Resource(visit));

}
});
return null;
});
return ret;
}

@Override
public <T> T apply(Function<OpenPathTree, T> func) {
lock.readLock().lock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.sql.Driver;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
Expand Down Expand Up @@ -246,27 +247,31 @@ public Enumeration<URL> getResources(String unsanitisedName, boolean parentAlrea
if (providers != null) {
boolean endsWithTrailingSlash = unsanitisedName.endsWith("/");
for (ClassPathElement element : providers) {
ClassPathResource res = element.getResource(name);
Collection<ClassPathResource> resList = element.getResources(name);
//if the requested name ends with a trailing / we make sure
//that the resource is a directory, and return a URL that ends with a /
//this matches the behaviour of URLClassLoader
if (endsWithTrailingSlash) {
if (res.isDirectory()) {
try {
resources.add(new URL(res.getUrl().toString() + "/"));
} catch (MalformedURLException e) {
throw new RuntimeException(e);
for (var res : resList) {
if (endsWithTrailingSlash) {
if (res.isDirectory()) {
try {
resources.add(new URL(res.getUrl().toString() + "/"));
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
} else {
resources.add(res.getUrl());
}
} else {
resources.add(res.getUrl());
}
}
} else if (name.isEmpty()) {
for (ClassPathElement i : elements) {
ClassPathResource res = i.getResource("");
if (res != null) {
resources.add(res.getUrl());
List<ClassPathResource> resList = i.getResources("");
for (var res : resList) {
if (res != null) {
resources.add(res.getUrl());
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.quarkus.it.extension;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
public class ClassLoaderTest {

@Test
void testClassLoaderResources() throws IOException {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
ArrayList<URL> resources = Collections.list(contextClassLoader.getResources("io/quarkus/it/extension"));
Assertions.assertEquals(2, resources.size());
}

@Test
void testClassLoaderSingleResource() throws IOException {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
URL resource = contextClassLoader.getResource("io/quarkus/it/extension/my_resource.txt");
Assertions.assertNotNull(resource);
}
}

0 comments on commit 9daa467

Please sign in to comment.