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

Make sure CDI and BuildServicesResolves both guard against repeated i… #619

Merged
merged 1 commit into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import java.util.Collections;
import java.util.Comparator;
import java.util.Objects;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
import java.util.Set;
Expand Down Expand Up @@ -76,8 +75,17 @@ private static void discoverFactories() {
* 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}
* @throws IllegalArgumentException if the provided argument is null
* @throws IllegalStateException if the {@link BuildServices} are already set
*/
public static void setBuildServices(BuildServices instance) {
configuredBuildServices = Objects.requireNonNull(instance, "BuildServices instance must not be null");
if (instance == null) {
throw new IllegalArgumentException("BuildServices instance must not be null");
}
if (configuredBuildServices != null) {
configuredBuildServices = instance;
} else {
throw new IllegalStateException("BuildServices cannot be set repeatedly. Existing BuildServices are " + configuredBuildServices);
}
}
}
10 changes: 7 additions & 3 deletions api/src/main/java/jakarta/enterprise/inject/spi/CDI.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static CDI<Object> current() {
/**
*
* Obtain the {@link CDIProvider} the user set with {@link #setCDIProvider(CDIProvider)} or the last returned
* {@link CDIProvider} if it returns valid CDI container. Otherwise use the serviceloader to retrieve the
* {@link CDIProvider} if it returns valid CDI container. Otherwise, use service loader mechanism to retrieve the
* {@link CDIProvider} with the highest priority.
*
* @return the {@link CDIProvider} set by user or retrieved by serviceloader
Expand Down Expand Up @@ -118,13 +118,17 @@ private static boolean checkProvider(CDIProvider c) {
*
* @param provider the provider to use
* @throws IllegalStateException if the {@link CDIProvider} is already set
* @throws IllegalArgumentException if the provided argument is null
*/
public static void setCDIProvider(CDIProvider provider) {
if (provider != null) {
if (provider == null) {
throw new IllegalArgumentException("CDIProvider must not be null");
}
if (configuredProvider != null) {
providerSetManually = true;
configuredProvider = provider;
} else {
throw new IllegalStateException("CDIProvider must not be null");
throw new IllegalStateException("CDIProvider cannot be set repeatedly. Existing provider is " + configuredProvider);
}
}

Expand Down