Skip to content

Commit 8a68ef9

Browse files
authored
Issue #3761 - add culprit classnames to provider error messages (#3762)
* Issue #3761 - add culprit classnames to provider error messages * Fix checkstyle errors
1 parent 38d02dd commit 8a68ef9

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

core/sdk-core/src/main/java/software/amazon/awssdk/core/internal/http/loader/ClasspathSdkHttpServiceProvider.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515

1616
package software.amazon.awssdk.core.internal.http.loader;
1717

18-
import java.util.Iterator;
18+
import java.util.List;
1919
import java.util.Optional;
2020
import java.util.ServiceLoader;
21+
import java.util.stream.Collectors;
22+
import java.util.stream.StreamSupport;
2123
import software.amazon.awssdk.annotations.SdkInternalApi;
2224
import software.amazon.awssdk.annotations.SdkTestInternalApi;
2325
import software.amazon.awssdk.core.SdkSystemSetting;
@@ -46,22 +48,33 @@ final class ClasspathSdkHttpServiceProvider<T> implements SdkHttpServiceProvider
4648

4749
@Override
4850
public Optional<T> loadService() {
49-
Iterator<T> httpServices = serviceLoader.loadServices(serviceClass);
50-
if (!httpServices.hasNext()) {
51+
Iterable<T> iterable = () -> serviceLoader.loadServices(serviceClass);
52+
List<T> impls = StreamSupport
53+
.stream(iterable.spliterator(), false)
54+
.collect(Collectors.toList());
55+
56+
if (impls.isEmpty()) {
5157
return Optional.empty();
5258
}
53-
T httpService = httpServices.next();
5459

55-
if (httpServices.hasNext()) {
60+
if (impls.size() > 1) {
61+
62+
String implText =
63+
impls.stream()
64+
.map(clazz -> clazz.getClass().getName())
65+
.collect(Collectors.joining(",", "[", "]"));
66+
5667
throw SdkClientException.builder().message(
5768
String.format(
5869
"Multiple HTTP implementations were found on the classpath. To avoid non-deterministic loading " +
5970
"implementations, please explicitly provide an HTTP client via the client builders, set the %s " +
6071
"system property with the FQCN of the HTTP service to use as the default, or remove all but one " +
61-
"HTTP implementation from the classpath", implSystemProperty.property()))
72+
"HTTP implementation from the classpath. The multiple implementations found were: %s",
73+
implSystemProperty.property(), implText))
6274
.build();
6375
}
64-
return Optional.of(httpService);
76+
77+
return impls.stream().findFirst();
6578
}
6679

6780
/**

0 commit comments

Comments
 (0)