From c316211fafe5244a80738c1fc3cf44af67c8a80e Mon Sep 17 00:00:00 2001 From: Eric Zhao Date: Mon, 22 Apr 2019 11:13:38 +0800 Subject: [PATCH] Add loadInstanceList support for SpiLoader Signed-off-by: Eric Zhao --- .../alibaba/csp/sentinel/util/SpiLoader.java | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/SpiLoader.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/SpiLoader.java index c4c20a55..6632b792 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/SpiLoader.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/SpiLoader.java @@ -92,7 +92,37 @@ public final class SpiLoader { } /** - * Load and sorted SPI instance list. + * Load the SPI instance list for provided SPI interface. + * + * @param clazz class of the SPI + * @param SPI type + * @return sorted SPI instance list + * @since 1.6.0 + */ + public static List loadInstanceList(Class clazz) { + try { + String key = clazz.getName(); + // Not thread-safe, as it's expected to be resolved in a thread-safe context. + ServiceLoader serviceLoader = SERVICE_LOADER_MAP.get(key); + if (serviceLoader == null) { + serviceLoader = ServiceLoader.load(clazz); + SERVICE_LOADER_MAP.put(key, serviceLoader); + } + + List list = new ArrayList<>(); + for (T spi : serviceLoader) { + list.add(spi); + } + return list; + } catch (Throwable t) { + RecordLog.warn("[SpiLoader] ERROR: loadInstanceListSorted failed", t); + t.printStackTrace(); + return new ArrayList<>(); + } + } + + /** + * Load the sorted SPI instance list for provided SPI interface. * * @param clazz class of the SPI * @param SPI type