Skip to content

Use InetAddressResolverProvider and add tests #1354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,27 @@
import com.mongodb.MongoSocketException;
import com.mongodb.ServerAddress;
import com.mongodb.UnixServerAddress;
import com.mongodb.lang.Nullable;
import com.mongodb.spi.dns.InetAddressResolver;
import com.mongodb.spi.dns.InetAddressResolverProvider;

import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.List;
import java.util.ServiceLoader;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

/**
* <p>This class is not part of the public API and may be removed or changed at any time</p>
*/
public final class ServerAddressHelper {
@Nullable
private static final InetAddressResolver LOCATED_INET_ADDRESS_RESOLVER = StreamSupport.stream(
ServiceLoader.load(InetAddressResolverProvider.class).spliterator(), false)
.findFirst()
.map(InetAddressResolverProvider::create)
.orElse(null);

public static ServerAddress createServerAddress(final String host) {
return createServerAddress(host, ServerAddress.defaultPort());
Expand All @@ -46,8 +56,14 @@ public static ServerAddress createServerAddress(final String host, final int por
}

public static InetAddressResolver getInetAddressResolver(final MongoClientSettings settings) {
InetAddressResolver inetAddressResolver = settings.getInetAddressResolver();
return inetAddressResolver == null ? new DefaultInetAddressResolver() : inetAddressResolver;
InetAddressResolver explicitInetAddressResolver = settings.getInetAddressResolver();
if (explicitInetAddressResolver != null) {
return explicitInetAddressResolver;
} else if (LOCATED_INET_ADDRESS_RESOLVER != null) {
return LOCATED_INET_ADDRESS_RESOLVER;
} else {
return new DefaultInetAddressResolver();
}
}

public static List<InetSocketAddress> getSocketAddresses(final ServerAddress serverAddress, final InetAddressResolver resolver) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
Args = --initialize-at-run-time=com.mongodb.UnixServerAddress,com.mongodb.internal.connection.SnappyCompressor,org.bson.types.ObjectId,com.mongodb.internal.connection.ClientMetadataHelper
Args =\
--initialize-at-run-time=\
com.mongodb.UnixServerAddress,\
com.mongodb.internal.connection.SnappyCompressor,\
com.mongodb.internal.connection.ClientMetadataHelper,\
com.mongodb.internal.connection.ServerAddressHelper
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"resources":{
"includes":[{
"pattern":"\\QMETA-INF/services/com.mongodb.spi.dns.InetAddressResolverProvider\\E"
}]},
"bundles":[]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mongodb.connection;

import com.mongodb.MongoClientSettings;
import com.mongodb.internal.connection.DefaultInetAddressResolver;
import com.mongodb.internal.connection.ServerAddressHelper;
import com.mongodb.spi.dns.InetAddressResolver;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;

final class ServerAddressHelperTest {
@Test
void getInetAddressResolver() {
assertAll(
() -> assertEquals(
DefaultInetAddressResolver.class,
ServerAddressHelper.getInetAddressResolver(MongoClientSettings.builder().build()).getClass()),
() -> {
InetAddressResolver resolver = new DefaultInetAddressResolver();
assertSame(
resolver,
ServerAddressHelper.getInetAddressResolver(MongoClientSettings.builder().inetAddressResolver(resolver).build()));
}
);
}
}