Skip to content

Commit 9a02f81

Browse files
authored
Use InetAddressResolverProvider and add tests (#1353)
JAVA-5390
1 parent bb013a0 commit 9a02f81

File tree

11 files changed

+204
-10
lines changed

11 files changed

+204
-10
lines changed

driver-core/src/main/com/mongodb/MongoClientSettings.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,10 @@ public DnsClient getDnsClient() {
184184
}
185185

186186
/**
187-
* Gets the {@link InetAddressResolver} to use for looking up the {@link java.net.InetAddress} instances for each host.
188-
*
189-
* <p>If set, it will be used to look up the {@link java.net.InetAddress} for each host, via
190-
* {@link InetAddressResolver#lookupByName(String)}. Otherwise, {@link java.net.InetAddress#getAllByName(String)} will be used.
187+
* Gets the explicitly set {@link InetAddressResolver} to use for looking up the {@link java.net.InetAddress} instances for each host.
191188
*
192189
* @return the {@link java.net.InetAddress} resolver
190+
* @see Builder#inetAddressResolver(InetAddressResolver)
193191
* @since 4.10
194192
*/
195193
@Nullable
@@ -660,6 +658,7 @@ public Builder dnsClient(@Nullable final DnsClient dnsClient) {
660658
*
661659
* @param inetAddressResolver the InetAddress provider
662660
* @return the {@link java.net.InetAddress} resolver
661+
* @see #getInetAddressResolver()
663662
* @since 4.10
664663
*/
665664
public Builder inetAddressResolver(@Nullable final InetAddressResolver inetAddressResolver) {

driver-core/src/main/com/mongodb/internal/connection/ServerAddressHelper.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,27 @@
2121
import com.mongodb.MongoSocketException;
2222
import com.mongodb.ServerAddress;
2323
import com.mongodb.UnixServerAddress;
24+
import com.mongodb.lang.Nullable;
2425
import com.mongodb.spi.dns.InetAddressResolver;
26+
import com.mongodb.spi.dns.InetAddressResolverProvider;
2527

2628
import java.net.InetSocketAddress;
2729
import java.net.UnknownHostException;
2830
import java.util.List;
31+
import java.util.ServiceLoader;
2932
import java.util.stream.Collectors;
33+
import java.util.stream.StreamSupport;
3034

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

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

4858
public static InetAddressResolver getInetAddressResolver(final MongoClientSettings settings) {
49-
InetAddressResolver inetAddressResolver = settings.getInetAddressResolver();
50-
return inetAddressResolver == null ? new DefaultInetAddressResolver() : inetAddressResolver;
59+
InetAddressResolver explicitInetAddressResolver = settings.getInetAddressResolver();
60+
if (explicitInetAddressResolver != null) {
61+
return explicitInetAddressResolver;
62+
} else if (LOCATED_INET_ADDRESS_RESOLVER != null) {
63+
return LOCATED_INET_ADDRESS_RESOLVER;
64+
} else {
65+
return new DefaultInetAddressResolver();
66+
}
5167
}
5268

5369
public static List<InetSocketAddress> getSocketAddresses(final ServerAddress serverAddress, final InetAddressResolver resolver) {

driver-core/src/main/resources/META-INF/native-image/org.mongodb/bson/native-image.properties

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,9 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
#
16-
Args = --initialize-at-run-time=com.mongodb.UnixServerAddress,com.mongodb.internal.connection.SnappyCompressor,org.bson.types.ObjectId,com.mongodb.internal.connection.ClientMetadataHelper
16+
Args =\
17+
--initialize-at-run-time=\
18+
com.mongodb.UnixServerAddress,\
19+
com.mongodb.internal.connection.SnappyCompressor,\
20+
com.mongodb.internal.connection.ClientMetadataHelper,\
21+
com.mongodb.internal.connection.ServerAddressHelper
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"resources":{
3+
"includes":[{
4+
"pattern":"\\QMETA-INF/services/com.mongodb.spi.dns.InetAddressResolverProvider\\E"
5+
}]},
6+
"bundles":[]
7+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.mongodb.connection;
17+
18+
import com.mongodb.MongoClientSettings;
19+
import com.mongodb.internal.connection.DefaultInetAddressResolver;
20+
import com.mongodb.internal.connection.ServerAddressHelper;
21+
import com.mongodb.spi.dns.InetAddressResolver;
22+
import org.junit.jupiter.api.Test;
23+
24+
import static org.junit.jupiter.api.Assertions.assertAll;
25+
import static org.junit.jupiter.api.Assertions.assertEquals;
26+
import static org.junit.jupiter.api.Assertions.assertSame;
27+
28+
final class ServerAddressHelperTest {
29+
@Test
30+
void getInetAddressResolver() {
31+
assertAll(
32+
() -> assertEquals(
33+
DefaultInetAddressResolver.class,
34+
ServerAddressHelper.getInetAddressResolver(MongoClientSettings.builder().build()).getClass()),
35+
() -> {
36+
InetAddressResolver resolver = new DefaultInetAddressResolver();
37+
assertSame(
38+
resolver,
39+
ServerAddressHelper.getInetAddressResolver(MongoClientSettings.builder().inetAddressResolver(resolver).build()));
40+
}
41+
);
42+
}
43+
}

graalvm-native-image-app/build.gradle

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ graalvmNative {
5151
configureEach {
5252
buildArgs.add('--strict-image-heap')
5353
buildArgs.add('-H:+UnlockExperimentalVMOptions')
54-
// see class initialization report is generated at `graalvm/build/native/nativeCompile/reports`,
55-
// informing us on the kind of initialization for each Java class
56-
buildArgs.add('-H:+PrintClassInitialization')
54+
// see class initialization and other reports in `graalvm/build/native/nativeCompile/reports`
55+
buildArgs.add('--diagnostics-mode')
5756
// see the "registerResource" entries in the `native-image` built-time output,
5857
// informing us on the resources included in the native image being built
5958
buildArgs.add('-H:Log=registerResource:5')
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.mongodb.internal.graalvm;
17+
18+
import com.mongodb.internal.connection.DefaultInetAddressResolver;
19+
import com.mongodb.spi.dns.InetAddressResolver;
20+
import com.mongodb.spi.dns.InetAddressResolverProvider;
21+
22+
import java.net.InetAddress;
23+
import java.net.UnknownHostException;
24+
import java.util.List;
25+
26+
import static java.lang.String.format;
27+
28+
public final class CustomInetAddressResolverProvider implements InetAddressResolverProvider {
29+
private static volatile boolean used = false;
30+
31+
public CustomInetAddressResolverProvider() {
32+
}
33+
34+
@Override
35+
public InetAddressResolver create() {
36+
return new CustomInetAddressResolver();
37+
}
38+
39+
static void assertUsed() throws AssertionError {
40+
if (!used) {
41+
throw new AssertionError(format("%s is not used", CustomInetAddressResolverProvider.class.getSimpleName()));
42+
}
43+
}
44+
45+
private static void markUsed() {
46+
used = true;
47+
}
48+
49+
private static final class CustomInetAddressResolver implements InetAddressResolver {
50+
private final DefaultInetAddressResolver wrapped;
51+
52+
CustomInetAddressResolver() {
53+
wrapped = new DefaultInetAddressResolver();
54+
}
55+
56+
@Override
57+
public List<InetAddress> lookupByName(final String host) throws UnknownHostException {
58+
markUsed();
59+
return wrapped.lookupByName(host);
60+
}
61+
}
62+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.mongodb.internal.graalvm;
17+
18+
import com.mongodb.client.MongoClient;
19+
import com.mongodb.client.MongoClients;
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
22+
23+
import java.util.ArrayList;
24+
25+
final class DnsSpi {
26+
private static final Logger LOGGER = LoggerFactory.getLogger(DnsSpi.class);
27+
28+
public static void main(final String... args) {
29+
LOGGER.info("Begin");
30+
try (MongoClient client = args.length == 0 ? MongoClients.create() : MongoClients.create(args[0])) {
31+
LOGGER.info("Database names: {}", client.listDatabaseNames().into(new ArrayList<>()));
32+
}
33+
CustomInetAddressResolverProvider.assertUsed();
34+
LOGGER.info("End");
35+
}
36+
37+
private DnsSpi() {
38+
}
39+
}

graalvm-native-image-app/src/main/com/mongodb/internal/graalvm/NativeImageApp.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,14 @@ final class NativeImageApp {
2929
private static final Logger LOGGER = LoggerFactory.getLogger(NativeImageApp.class);
3030

3131
public static void main(final String[] args) {
32+
LOGGER.info("Begin");
3233
LOGGER.info("java.vendor={}, java.vm.name={}, java.version={}",
3334
System.getProperty("java.vendor"), System.getProperty("java.vm.name"), System.getProperty("java.version"));
3435
String[] arguments = new String[] {getConnectionStringSystemPropertyOrDefault()};
3536
LOGGER.info("proper args={}, tour/example arguments={}", Arrays.toString(args), Arrays.toString(arguments));
3637
List<Throwable> errors = Stream.<ThrowingRunnable>of(
38+
new ThrowingRunnable.Named(DnsSpi.class,
39+
() -> DnsSpi.main(arguments)),
3740
new ThrowingRunnable.Named(gridfs.GridFSTour.class,
3841
() -> gridfs.GridFSTour.main(arguments)),
3942
new ThrowingRunnable.Named(documentation.CausalConsistencyExamples.class,
@@ -85,6 +88,7 @@ public static void main(final String[] args) {
8588
errors.forEach(error::addSuppressed);
8689
throw error;
8790
}
91+
LOGGER.info("End");
8892
}
8993

9094
private NativeImageApp() {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
@NonNullApi
17+
package com.mongodb.internal.graalvm;
18+
19+
import com.mongodb.lang.NonNullApi;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.mongodb.internal.graalvm.CustomInetAddressResolverProvider

0 commit comments

Comments
 (0)