Skip to content

Commit

Permalink
fix(#2324): Make resource loading more robust (#2325)
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikriemer authored Dec 13, 2023
1 parent 86ac906 commit c68e2fb
Showing 1 changed file with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,26 @@ public class Resources {

public static String asString(String resourceName,
Charset charset) throws IOException {
return IOUtils.resourceToString(resourceName, charset, ClassLoader.getSystemClassLoader());
var loader = getLoader();
return IOUtils.resourceToString(resourceName, charset, loader);
}

public static URL asUrl(String resourceName) throws IOException {
return IOUtils.resourceToURL(resourceName, ClassLoader.getSystemClassLoader());
var loader = getLoader();
return IOUtils.resourceToURL(resourceName, loader);
}

private static ClassLoader getLoader() {
return firstNonNull(Thread.currentThread().getContextClassLoader(), Resources.class.getClassLoader());
}

private static <T> T firstNonNull(T first, T second) {
if (first != null) {
return first;
} else if (second != null) {
return second;
} else {
throw new NullPointerException("Both parameters are null");
}
}
}

0 comments on commit c68e2fb

Please sign in to comment.