Skip to content

Commit 5c9cbdf

Browse files
committed
Introduce QuarkusClassLoader.visitRuntimeResources(name)
1 parent e4c5c4f commit 5c9cbdf

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/classloading/QuarkusClassLoader.java

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@
2323
import java.util.Set;
2424
import java.util.concurrent.ConcurrentHashMap;
2525
import java.util.concurrent.ConcurrentMap;
26+
import java.util.function.Consumer;
2627

2728
import org.jboss.logging.Logger;
2829

2930
import io.quarkus.commons.classloading.ClassLoaderHelper;
3031
import io.quarkus.paths.ManifestAttributes;
32+
import io.quarkus.paths.PathVisit;
3133

3234
/**
3335
* The ClassLoader used for non production Quarkus applications (i.e. dev and test mode).
@@ -48,14 +50,42 @@ public class QuarkusClassLoader extends ClassLoader implements Closeable {
4850
registerAsParallelCapable();
4951
}
5052

53+
private static RuntimeException nonQuarkusClassLoaderError() {
54+
return new IllegalStateException("The current classloader is not an instance of "
55+
+ QuarkusClassLoader.class.getName() + " but "
56+
+ Thread.currentThread().getContextClassLoader().getClass().getName());
57+
}
58+
59+
/**
60+
* Visits every found runtime resource with a given name. If a resource is not found, the visitor will
61+
* simply not be called.
62+
* <p>
63+
* IMPORTANT: this method works only when the current class loader is an instance of {@link QuarkusClassLoader},
64+
* otherwise it throws an error with the corresponding message.
65+
*
66+
* @param resourceName runtime resource name to visit
67+
* @param visitor runtime resource visitor
68+
*/
69+
public static void visitRuntimeResources(String resourceName, Consumer<PathVisit> visitor) {
70+
if (Thread.currentThread().getContextClassLoader() instanceof QuarkusClassLoader classLoader) {
71+
for (var element : classLoader.getElementsWithResource(resourceName)) {
72+
if (element.isRuntime()) {
73+
element.apply(tree -> {
74+
tree.accept(resourceName, visitor);
75+
return null;
76+
});
77+
}
78+
}
79+
} else {
80+
throw nonQuarkusClassLoaderError();
81+
}
82+
}
83+
5184
public static List<ClassPathElement> getElements(String resourceName, boolean onlyFromCurrentClassLoader) {
5285
if (Thread.currentThread().getContextClassLoader() instanceof QuarkusClassLoader classLoader) {
5386
return classLoader.getElementsWithResource(resourceName, onlyFromCurrentClassLoader);
5487
}
55-
56-
throw new IllegalStateException("The current classloader is not an instance of "
57-
+ QuarkusClassLoader.class.getName() + " but "
58-
+ Thread.currentThread().getContextClassLoader().getClass().getName());
88+
throw nonQuarkusClassLoaderError();
5989
}
6090

6191
/**
@@ -78,10 +108,7 @@ public static boolean isApplicationClass(String className) {
78108

79109
return classPathResourceIndex.getFirstClassPathElement(resourceName) != null;
80110
}
81-
82-
throw new IllegalStateException("The current classloader is not an instance of "
83-
+ QuarkusClassLoader.class.getName() + " but "
84-
+ Thread.currentThread().getContextClassLoader().getClass().getName());
111+
throw nonQuarkusClassLoaderError();
85112
}
86113

87114
/**

0 commit comments

Comments
 (0)