You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@streampipes.apache.org by ri...@apache.org on 2023/12/13 13:51:25 UTC

(streampipes) branch dev updated: fix(#2324): Make resource loading more robust (#2325)

This is an automated email from the ASF dual-hosted git repository.

riemer pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/streampipes.git


The following commit(s) were added to refs/heads/dev by this push:
     new c68e2fbd0 fix(#2324): Make resource loading more robust (#2325)
c68e2fbd0 is described below

commit c68e2fbd0da9d31c084963e743ec8151571f5764
Author: Dominik Riemer <do...@gmail.com>
AuthorDate: Wed Dec 13 14:51:19 2023 +0100

    fix(#2324): Make resource loading more robust (#2325)
---
 .../streampipes/commons/resources/Resources.java     | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/streampipes-commons/src/main/java/org/apache/streampipes/commons/resources/Resources.java b/streampipes-commons/src/main/java/org/apache/streampipes/commons/resources/Resources.java
index 32c0bdd2f..dd09856be 100644
--- a/streampipes-commons/src/main/java/org/apache/streampipes/commons/resources/Resources.java
+++ b/streampipes-commons/src/main/java/org/apache/streampipes/commons/resources/Resources.java
@@ -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");
+    }
   }
 }