This is an automated email from the ASF dual-hosted git repository. ahuber pushed a commit to branch spring6 in repository https://gitbox.apache.org/repos/asf/isis.git
The following commit(s) were added to refs/heads/spring6 by this push: new 6f71a936d7 ISIS-3275: class-path resource loading fixes 6f71a936d7 is described below commit 6f71a936d76705c4cc69600fe060b36c603a0b1c Author: Andi Huber <ahu...@apache.org> AuthorDate: Fri Nov 18 08:38:21 2022 +0100 ISIS-3275: class-path resource loading fixes --- .../commons/internal/resources/_Resources.java | 19 +++++++++++++------ .../userreg/EmailNotificationServiceDefault.java | 20 ++++++++++---------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/commons/src/main/java/org/apache/causeway/commons/internal/resources/_Resources.java b/commons/src/main/java/org/apache/causeway/commons/internal/resources/_Resources.java index eca0a573a7..5294c55962 100644 --- a/commons/src/main/java/org/apache/causeway/commons/internal/resources/_Resources.java +++ b/commons/src/main/java/org/apache/causeway/commons/internal/resources/_Resources.java @@ -23,6 +23,7 @@ import java.io.InputStream; import java.net.URL; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.util.Optional; import java.util.function.Predicate; import java.util.regex.Pattern; @@ -62,8 +63,12 @@ public final class _Resources { final @NonNull Class<?> contextClass, final @NonNull String resourceName) { - val absoluteResourceName = resolveName(resourceName, contextClass); - return _Context.getDefaultClassLoader().getResourceAsStream(absoluteResourceName); + val absoluteResourceName = resolveName(contextClass, resourceName); + + return Optional + .ofNullable(contextClass.getResourceAsStream(absoluteResourceName)) + .orElseGet(()->_Context.getDefaultClassLoader() + .getResourceAsStream(absoluteResourceName)); } /** @@ -101,12 +106,14 @@ public final class _Resources { final @NonNull Class<?> contextClass, final @NonNull String resourceName) { - final String absoluteResourceName = resolveName(resourceName, contextClass); + final String absoluteResourceName = resolveName(contextClass, resourceName); - return _Context.getDefaultClassLoader().getResource(absoluteResourceName); + return Optional + .ofNullable(contextClass.getResource(absoluteResourceName)) + .orElseGet(()->_Context.getDefaultClassLoader() + .getResource(absoluteResourceName)); } - // -- LOCAL vs EXTERNAL resource path private static final Predicate<String> externalResourcePattern = @@ -138,7 +145,7 @@ public final class _Resources { * * Adapted copy of JDK 8 Class::resolveName */ - private static String resolveName(String name, final Class<?> contextClass) { + private static String resolveName(final Class<?> contextClass, String name) { if (name == null) { return name; } diff --git a/core/runtimeservices/src/main/java/org/apache/causeway/core/runtimeservices/userreg/EmailNotificationServiceDefault.java b/core/runtimeservices/src/main/java/org/apache/causeway/core/runtimeservices/userreg/EmailNotificationServiceDefault.java index 6a7a040474..ab52c5dbfd 100644 --- a/core/runtimeservices/src/main/java/org/apache/causeway/core/runtimeservices/userreg/EmailNotificationServiceDefault.java +++ b/core/runtimeservices/src/main/java/org/apache/causeway/core/runtimeservices/userreg/EmailNotificationServiceDefault.java @@ -18,8 +18,6 @@ */ package org.apache.causeway.core.runtimeservices.userreg; -import java.io.IOException; -import java.net.URL; import java.util.Collections; import java.util.List; import java.util.regex.Pattern; @@ -27,11 +25,6 @@ import java.util.regex.Pattern; import static java.util.regex.Pattern.compile; import static java.util.regex.Pattern.quote; -import jakarta.annotation.PostConstruct; -import jakarta.annotation.Priority; -import jakarta.inject.Inject; -import jakarta.inject.Named; - import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; @@ -41,9 +34,15 @@ import org.apache.causeway.applib.services.userreg.EmailNotificationService; import org.apache.causeway.applib.services.userreg.events.EmailEventAbstract; import org.apache.causeway.applib.services.userreg.events.EmailRegistrationEvent; import org.apache.causeway.applib.services.userreg.events.PasswordResetEvent; +import org.apache.causeway.commons.internal.exceptions._Exceptions; import org.apache.causeway.commons.internal.resources._Resources; import org.apache.causeway.core.runtimeservices.CausewayModuleCoreRuntimeServices; +import jakarta.annotation.PostConstruct; +import jakarta.annotation.Priority; +import jakarta.inject.Inject; +import jakarta.inject.Named; + @Service @Named(CausewayModuleCoreRuntimeServices.NAMESPACE + ".EmailNotificationServiceDefault") @Priority(PriorityPrecedence.MIDPOINT) @@ -87,9 +86,10 @@ public class EmailNotificationServiceDefault implements EmailNotificationService protected String loadResource(final String resourceName) { try { return _Resources.loadAsStringUtf8(EmailNotificationServiceDefault.class, resourceName); - } catch (IOException e) { - final URL templateUrl = _Resources.getResourceUrl(EmailNotificationServiceDefault.class, resourceName); - throw new IllegalStateException(String.format("Unable to read resource URL '%s'", templateUrl)); + } catch (Exception e) { + throw _Exceptions.illegalState(e, "Unable to read resource '%s' relative to %s", + resourceName, + EmailNotificationServiceDefault.class.getName()); } }