This is an automated email from the ASF dual-hosted git repository.
ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git
The following commit(s) were added to refs/heads/master by this push:
new a4a4b18160 ISIS-3275: class-path resource loading fixes
a4a4b18160 is described below
commit a4a4b18160269f48c9163abfe168f88e68ad36cd
Author: Andi Huber <[email protected]>
AuthorDate: Fri Nov 18 08:38:21 2022 +0100
ISIS-3275: class-path resource loading fixes
(cherry picked)
---
.../commons/internal/resources/_Resources.java | 19 +++++++++++++------
.../userreg/EmailNotificationServiceDefault.java | 10 +++++-----
2 files changed, 18 insertions(+), 11 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 b6f6ceb7b3..485ec10ef5 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;
@@ -41,6 +39,7 @@ 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;
@@ -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());
}
}