|
30 | 30 | import java.net.URL; |
31 | 31 | import java.nio.charset.StandardCharsets; |
32 | 32 | import java.time.Instant; |
| 33 | +import java.util.ArrayList; |
33 | 34 | import java.util.Base64; |
34 | | -import java.util.HashSet; |
| 35 | +import java.util.Collections; |
35 | 36 | import java.util.LinkedHashMap; |
| 37 | +import java.util.List; |
36 | 38 | import java.util.Locale; |
37 | 39 | import java.util.Map; |
38 | 40 | import java.util.Optional; |
39 | 41 | import java.util.ServiceLoader; |
40 | | -import java.util.Set; |
41 | 42 | import java.util.concurrent.TimeUnit; |
42 | 43 | import java.util.regex.Matcher; |
43 | 44 | import java.util.regex.Pattern; |
@@ -150,37 +151,25 @@ public static HttpClient.Factory getHttpClientFactory() { |
150 | 151 | } |
151 | 152 |
|
152 | 153 | private static HttpClient.Factory getFactory(ServiceLoader<HttpClient.Factory> loader) { |
153 | | - HttpClient.Factory selected = null; |
154 | | - Set<String> detectedFactories = new HashSet<>(); |
155 | | - Set<String> samePriority = new HashSet<>(); |
156 | | - for (HttpClient.Factory candidate : loader) { |
157 | | - final String candidateClassName = candidate.getClass().getName(); |
158 | | - detectedFactories.add(candidateClassName); |
159 | | - LOGGER.debug("Considering {} httpclient factory", candidateClassName); |
160 | | - |
161 | | - if (selected == null) { |
162 | | - selected = candidate; |
163 | | - LOGGER.debug("Temporarily selected {} as first candidate httpclient factory", candidateClassName); |
164 | | - } else if (selected.priority() < candidate.priority()) { |
165 | | - selected = candidate; |
166 | | - samePriority.clear(); |
167 | | - LOGGER.debug("Temporarily selected {} as httpclient factory, replacing one with lower priority", |
168 | | - candidateClassName); |
169 | | - } else if (selected.priority() == candidate.priority()) { |
170 | | - samePriority.add(candidateClassName); |
171 | | - } else { |
172 | | - LOGGER.debug("Ignoring {} httpclient factory as it doesn't supersede currently selected one", candidateClassName); |
173 | | - } |
174 | | - |
| 154 | + List<HttpClient.Factory> factories = new ArrayList<>(); |
| 155 | + loader.forEach(factories::add); |
| 156 | + if (factories.isEmpty()) { |
| 157 | + return null; |
175 | 158 | } |
176 | | - |
177 | | - if (detectedFactories.size() > 1) { |
178 | | - LOGGER.warn("The following httpclient factories were detected on your classpath: {}, " |
179 | | - + (samePriority.isEmpty() ? "" : "{} of which had the same priority ({}) so one was chosen randomly. ") |
180 | | - + "You should exclude dependencies that aren't needed or use an explicit association of the HttpClient.Factory.", |
181 | | - detectedFactories, samePriority.size(), samePriority); |
| 159 | + Collections.sort(factories, (f1, f2) -> Integer.compare(f2.priority(), f1.priority())); |
| 160 | + HttpClient.Factory factory = factories.get(0); |
| 161 | + if (factories.size() > 1) { |
| 162 | + if (factories.get(1).priority() == factory.priority()) { |
| 163 | + LOGGER.warn("The following httpclient factories were detected on your classpath: {}, " |
| 164 | + + "multiple of which had the same priority ({}) so one was chosen randomly. " |
| 165 | + + "You should exclude dependencies that aren't needed or use an explicit association of the HttpClient.Factory.", |
| 166 | + factories.stream().map(f -> f.getClass().getName()).toArray(), factory.priority()); |
| 167 | + } else if (LOGGER.isDebugEnabled()) { |
| 168 | + LOGGER.debug("The following httpclient factories were detected on your classpath: {}", |
| 169 | + factories.stream().map(f -> f.getClass().getName()).toArray()); |
| 170 | + } |
182 | 171 | } |
183 | | - return selected; |
| 172 | + return factory; |
184 | 173 | } |
185 | 174 |
|
186 | 175 | public static void applyCommonConfiguration(Config config, HttpClient.Builder builder, HttpClient.Factory factory) { |
|
0 commit comments