From 3b56b496d186fc9d2e64e908d429eb627d150355 Mon Sep 17 00:00:00 2001 From: Ladislav Thon Date: Tue, 24 May 2022 10:31:54 +0200 Subject: [PATCH] add BuildServicesResolver.setBuildServices() --- .../compatible/spi/BuildServicesResolver.java | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/api/src/main/java/jakarta/enterprise/inject/build/compatible/spi/BuildServicesResolver.java b/api/src/main/java/jakarta/enterprise/inject/build/compatible/spi/BuildServicesResolver.java index e5178faa..0b819539 100644 --- a/api/src/main/java/jakarta/enterprise/inject/build/compatible/spi/BuildServicesResolver.java +++ b/api/src/main/java/jakarta/enterprise/inject/build/compatible/spi/BuildServicesResolver.java @@ -12,12 +12,19 @@ import java.util.Collections; import java.util.Comparator; +import java.util.Objects; import java.util.ServiceConfigurationError; import java.util.ServiceLoader; import java.util.Set; import java.util.TreeSet; -final class BuildServicesResolver { +/** + * An internal helper to resolve {@link BuildServices} implementations. + * This class is public only for integrators and should not be used by applications. + * + * @since 4.0 + */ +public final class BuildServicesResolver { private static final Object lock = new Object(); private static volatile Set discoveredBuildServices; private static volatile BuildServices configuredBuildServices; @@ -48,7 +55,7 @@ private static void discoverFactories() { BuildServices.class, BuildServicesResolver.class.getClassLoader()); if (!loader.iterator().hasNext()) { - throw new IllegalStateException("Unable to locate AnnotationBuilderFactory implementation"); + throw new IllegalStateException("Unable to locate BuildServices implementation"); } try { @@ -61,4 +68,16 @@ private static void discoverFactories() { BuildServicesResolver.discoveredBuildServices = Collections.unmodifiableSet(factories); } + + /** + * This method should not be used by applications. It is only exposed for integrators + * with complex classloading architectures, where service loader lookup doesn't work out of the box. + * With this method, an integrator may manually provide an instance of {@link BuildServices} and + * this class will no longer attempt to look it up using service loader. + * + * @param instance a {@link BuildServices} instance that should be used, must not be {@code null} + */ + public static void setBuildServices(BuildServices instance) { + configuredBuildServices = Objects.requireNonNull(instance, "BuildServices instance must not be null"); + } }