|
15 | 15 |
|
16 | 16 | package software.amazon.awssdk.core.internal.http.loader;
|
17 | 17 |
|
18 |
| -import java.util.Iterator; |
| 18 | +import java.util.List; |
19 | 19 | import java.util.Optional;
|
20 | 20 | import java.util.ServiceLoader;
|
| 21 | +import java.util.stream.Collectors; |
| 22 | +import java.util.stream.StreamSupport; |
21 | 23 | import software.amazon.awssdk.annotations.SdkInternalApi;
|
22 | 24 | import software.amazon.awssdk.annotations.SdkTestInternalApi;
|
23 | 25 | import software.amazon.awssdk.core.SdkSystemSetting;
|
@@ -46,22 +48,33 @@ final class ClasspathSdkHttpServiceProvider<T> implements SdkHttpServiceProvider
|
46 | 48 |
|
47 | 49 | @Override
|
48 | 50 | 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()) { |
51 | 57 | return Optional.empty();
|
52 | 58 | }
|
53 |
| - T httpService = httpServices.next(); |
54 | 59 |
|
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 | + |
56 | 67 | throw SdkClientException.builder().message(
|
57 | 68 | String.format(
|
58 | 69 | "Multiple HTTP implementations were found on the classpath. To avoid non-deterministic loading " +
|
59 | 70 | "implementations, please explicitly provide an HTTP client via the client builders, set the %s " +
|
60 | 71 | "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)) |
62 | 74 | .build();
|
63 | 75 | }
|
64 |
| - return Optional.of(httpService); |
| 76 | + |
| 77 | + return impls.stream().findFirst(); |
65 | 78 | }
|
66 | 79 |
|
67 | 80 | /**
|
|
0 commit comments