You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@causeway.apache.org by ah...@apache.org on 2023/03/03 06:43:20 UTC

[causeway] branch master updated: CAUSEWAY-3304: DataSource: provides workarounds for module cannot access calling module's resources

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/causeway.git


The following commit(s) were added to refs/heads/master by this push:
     new 6260181536 CAUSEWAY-3304: DataSource: provides workarounds for module cannot access calling module's resources
6260181536 is described below

commit 6260181536335e903a31fc4d7f710bc12a7d17b0
Author: Andi Huber <ah...@apache.org>
AuthorDate: Fri Mar 3 07:43:15 2023 +0100

    CAUSEWAY-3304: DataSource: provides workarounds for module cannot access
    calling module's resources
---
 .../org/apache/causeway/commons/io/DataSource.java | 45 ++++++++++++++++------
 1 file changed, 34 insertions(+), 11 deletions(-)

diff --git a/commons/src/main/java/org/apache/causeway/commons/io/DataSource.java b/commons/src/main/java/org/apache/causeway/commons/io/DataSource.java
index ac56c99bff..8ddc3c3c0d 100644
--- a/commons/src/main/java/org/apache/causeway/commons/io/DataSource.java
+++ b/commons/src/main/java/org/apache/causeway/commons/io/DataSource.java
@@ -30,6 +30,7 @@ import java.util.function.Function;
 
 import javax.imageio.ImageIO;
 
+import org.springframework.core.io.Resource;
 import org.springframework.lang.Nullable;
 
 import org.apache.causeway.commons.collections.Can;
@@ -43,6 +44,7 @@ import org.apache.causeway.commons.internal.base._Strings;
 import org.apache.causeway.commons.internal.base._Text;
 
 import lombok.NonNull;
+import lombok.SneakyThrows;
 import lombok.val;
 
 /**
@@ -202,25 +204,46 @@ public interface DataSource {
         };
     }
 
+    /**
+     * Creates a {@link DataSource} for given {@link InputStream} eagerly.
+     * That is, it reads the InputStream into a byte array,
+     * which can be later read from repeatedly.
+     * <p>
+     * If reading from given {@link InputStream} throws any exception, it is propagated without catching.
+     */
+    @SneakyThrows
+    static DataSource ofInputStreamEagerly(final @Nullable InputStream inputStream) {
+        return ofBytes(_Bytes.of(inputStream));
+    }
+
     /**
      * Creates a {@link DataSource} for given resource path relative to {@link Class}.
-     * @param cls - required non-null
-     * @param resourcePath - required non-null
-     * @throws NullPointerException - if the any argument is null
+     * <p>
+     * If any of the args is null (or empty), returns an 'empty' {@link DataSource}.
+     * @apiNote may silently fail if this module cannot read resources from the calling module;
+     *      a workaround is to use {@code DataSource.ofInputStreamEagerly(cls.getResourceAsStream(resourcePath))}
+     *      at the call site
      */
-    static DataSource ofResource(final @NonNull Class<?> cls, final @NonNull String resourcePath) {
+    static DataSource ofResource(final @Nullable Class<?> cls, final @Nullable String resourcePath) {
         return cls==null
+                || _Strings.isNullOrEmpty(resourcePath)
                 ? empty()
                 : ofInputStreamSupplier(()->cls.getResourceAsStream(resourcePath));
     }
 
-//    static DataSource ofTestResource(final Class<?> cls, final String path) {
-//        val prefix = "src/test/java/";
-//        val filePath = prefix + cls.getPackageName().replace('.', '/') + "/" + path;
-//        val file = new File(filePath);
-//        _Assert.assertTrue(file.exists(), ()->String.format("could not resolve resource '%s'", file.getAbsolutePath()));
-//        return ofFile(file);
-//    }
+    /**
+     * Creates a {@link DataSource} for given Spring {@link Resource}.
+     * <p>
+     * If the single argument is null, returns an 'empty' {@link DataSource}.
+     * @apiNote may silently fail if this module cannot read resources from the calling module;
+     *      a workaround is to use {@code DataSource.ofInputStreamEagerly(springResource.getInputStream())}
+     *      at the call site
+     */
+    static DataSource ofSpringResource(final @Nullable Resource springResource) {
+        return springResource==null
+                ? empty()
+                : ofInputStreamSupplier(springResource::getInputStream);
+    }
 
     /**
      * Creates a {@link DataSource} for given {@link File}.