Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add BuildServicesResolver.setBuildServices() #617

Merged
merged 1 commit into from
May 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 <em>not</em> be used by applications.
*
* @since 4.0
*/
public final class BuildServicesResolver {
Ladicek marked this conversation as resolved.
Show resolved Hide resolved
private static final Object lock = new Object();
private static volatile Set<BuildServices> discoveredBuildServices;
private static volatile BuildServices configuredBuildServices;
Expand Down Expand Up @@ -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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new IllegalStateException("Unable to locate BuildServices implementation");
throw new IllegalStateException("Unable to locate a BuildServices implementation");

}

try {
Expand All @@ -61,4 +68,16 @@ private static void discoverFactories() {

BuildServicesResolver.discoveredBuildServices = Collections.unmodifiableSet(factories);
}

/**
* This method should <em>not</em> 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.
Ladicek marked this conversation as resolved.
Show resolved Hide resolved
* 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");
}
}