Skip to content

Commit

Permalink
Make sure CDI and BuildServicesResolves both guard against repeated i…
Browse files Browse the repository at this point in the history
…nvocations of set method
  • Loading branch information
manovotn committed Jun 6, 2022
1 parent 004f748 commit 82ec5d7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
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

0 comments on commit 82ec5d7

Please sign in to comment.