Skip to content

Commit 169aabc

Browse files
shawkinsmanusa
authored andcommitted
fix #5100: lessening the level of the non-conflicting warning
1 parent 5091017 commit 169aabc

File tree

3 files changed

+23
-31
lines changed

3 files changed

+23
-31
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Fix #5102: wait on scale to 0 was not completing
77

88
#### Improvements
9+
* Fix #5100: lessened the level of the non-conflicting httpclient implementation warning
910

1011
#### Dependency Upgrade
1112

kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/utils/HttpClientUtils.java

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@
3030
import java.net.URL;
3131
import java.nio.charset.StandardCharsets;
3232
import java.time.Instant;
33+
import java.util.ArrayList;
3334
import java.util.Base64;
34-
import java.util.HashSet;
35+
import java.util.Collections;
3536
import java.util.LinkedHashMap;
37+
import java.util.List;
3638
import java.util.Locale;
3739
import java.util.Map;
3840
import java.util.Optional;
3941
import java.util.ServiceLoader;
40-
import java.util.Set;
4142
import java.util.concurrent.TimeUnit;
4243
import java.util.regex.Matcher;
4344
import java.util.regex.Pattern;
@@ -150,37 +151,25 @@ public static HttpClient.Factory getHttpClientFactory() {
150151
}
151152

152153
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;
175158
}
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+
}
182171
}
183-
return selected;
172+
return factory;
184173
}
185174

186175
public static void applyCommonConfiguration(Config config, HttpClient.Builder builder, HttpClient.Factory factory) {

kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/utils/HttpClientUtilsTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.junit.jupiter.params.provider.MethodSource;
3232
import org.junit.jupiter.params.provider.ValueSource;
3333
import org.mockito.MockedStatic;
34+
import org.mockito.Mockito;
3435

3536
import java.net.MalformedURLException;
3637
import java.net.URL;
@@ -215,6 +216,7 @@ void setUp() {
215216
mockedStaticServiceLoader = mockStatic(ServiceLoader.class);
216217
@SuppressWarnings("unchecked")
217218
final ServiceLoader<HttpClient.Factory> mockServiceLoader = mock(ServiceLoader.class);
219+
Mockito.doCallRealMethod().when(mockServiceLoader).forEach(Mockito.any());
218220
when(mockServiceLoader.iterator()).thenAnswer(i -> new ArrayList<>(factories).iterator());
219221
when(ServiceLoader.load(eq(HttpClient.Factory.class), any())).thenReturn(mockServiceLoader);
220222
mockedStaticServiceLoader.when(() -> ServiceLoader.load(eq(HttpClient.Factory.class)))

0 commit comments

Comments
 (0)