From 915cded57d067534fde0573d3196ed9a57dbd6bd Mon Sep 17 00:00:00 2001 From: Bryson Gibbons Date: Thu, 19 Sep 2024 23:45:55 -0700 Subject: [PATCH 1/2] Example default fallback cache loader to handle service loader failures --- .../src/main/java/waffle/util/cache/Cache.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Source/JNA/waffle-jna-jakarta/src/main/java/waffle/util/cache/Cache.java b/Source/JNA/waffle-jna-jakarta/src/main/java/waffle/util/cache/Cache.java index 523ed97a38..fe760bcf62 100644 --- a/Source/JNA/waffle-jna-jakarta/src/main/java/waffle/util/cache/Cache.java +++ b/Source/JNA/waffle-jna-jakarta/src/main/java/waffle/util/cache/Cache.java @@ -26,6 +26,9 @@ import java.util.NoSuchElementException; import java.util.ServiceLoader; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * A semi-persistent mapping from keys to values. * @@ -60,13 +63,22 @@ public interface Cache { */ static Cache newCache(int timeout) throws NoSuchElementException { final NoSuchElementException exception = new NoSuchElementException(); + boolean cacheSupplierFound = false; for (CacheSupplier cacheSupplier : ServiceLoader.load(CacheSupplier.class)) { + cacheSupplierFound = true; try { return cacheSupplier.newCache(timeout); } catch (Exception e) { exception.addSuppressed(e); } } + + if (!cacheSupplierFound) { + Logger logger = LoggerFactory.getLogger(Cache.class); + logger.warn("No CacheSupplier implementation found by ServiceLoader. Please see [site] for possible solutions. Falling back to default CaffeineCache implementation."); + return new CaffeineCache<>(timeout); + } + throw exception; } From 4b30226711ad6eb631011f2d76a20b1cba4b900d Mon Sep 17 00:00:00 2001 From: Bryson Gibbons Date: Fri, 20 Sep 2024 10:49:16 -0700 Subject: [PATCH 2/2] Change logged message, include exception, and add change to waffle-jna as well --- .../src/main/java/waffle/util/cache/Cache.java | 2 +- .../src/main/java/waffle/util/cache/Cache.java | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Source/JNA/waffle-jna-jakarta/src/main/java/waffle/util/cache/Cache.java b/Source/JNA/waffle-jna-jakarta/src/main/java/waffle/util/cache/Cache.java index fe760bcf62..9d977ef53e 100644 --- a/Source/JNA/waffle-jna-jakarta/src/main/java/waffle/util/cache/Cache.java +++ b/Source/JNA/waffle-jna-jakarta/src/main/java/waffle/util/cache/Cache.java @@ -75,7 +75,7 @@ static Cache newCache(int timeout) throws NoSuchElementException { if (!cacheSupplierFound) { Logger logger = LoggerFactory.getLogger(Cache.class); - logger.warn("No CacheSupplier implementation found by ServiceLoader. Please see [site] for possible solutions. Falling back to default CaffeineCache implementation."); + logger.error("No CacheSupplier implementation found by ServiceLoader. Please see https://github.com/Waffle/waffle/blob/master/Docs/faq/CustomCache.md for possible solutions. Falling back to default CaffeineCache implementation.", exception); return new CaffeineCache<>(timeout); } diff --git a/Source/JNA/waffle-jna/src/main/java/waffle/util/cache/Cache.java b/Source/JNA/waffle-jna/src/main/java/waffle/util/cache/Cache.java index da45192ba7..02c2a966ce 100644 --- a/Source/JNA/waffle-jna/src/main/java/waffle/util/cache/Cache.java +++ b/Source/JNA/waffle-jna/src/main/java/waffle/util/cache/Cache.java @@ -26,6 +26,9 @@ import java.util.NoSuchElementException; import java.util.ServiceLoader; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * A semi-persistent mapping from keys to values. * @@ -60,13 +63,22 @@ public interface Cache { */ static Cache newCache(int timeout) throws NoSuchElementException { final NoSuchElementException exception = new NoSuchElementException(); + boolean cacheSupplierFound = false; for (CacheSupplier cacheSupplier : ServiceLoader.load(CacheSupplier.class)) { + cacheSupplierFound = true; try { return cacheSupplier.newCache(timeout); } catch (Exception e) { exception.addSuppressed(e); } } + + if (!cacheSupplierFound) { + Logger logger = LoggerFactory.getLogger(Cache.class); + logger.error("No CacheSupplier implementation found by ServiceLoader. Please see https://github.com/Waffle/waffle/blob/master/Docs/faq/CustomCache.md for possible solutions. Falling back to default CaffeineCache implementation.", exception); + return new CaffeineCache<>(timeout); + } + throw exception; }