forked from mongodb/mongo-java-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use
InetAddressResolverProvider
and add tests (mongodb#1353)
JAVA-5390
- Loading branch information
Showing
11 changed files
with
204 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
driver-core/src/main/resources/META-INF/native-image/resource-config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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":[] | ||
} |
43 changes: 43 additions & 0 deletions
43
driver-core/src/test/unit/com/mongodb/connection/ServerAddressHelperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...ve-image-app/src/main/com/mongodb/internal/graalvm/CustomInetAddressResolverProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* 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.internal.graalvm; | ||
|
||
import com.mongodb.internal.connection.DefaultInetAddressResolver; | ||
import com.mongodb.spi.dns.InetAddressResolver; | ||
import com.mongodb.spi.dns.InetAddressResolverProvider; | ||
|
||
import java.net.InetAddress; | ||
import java.net.UnknownHostException; | ||
import java.util.List; | ||
|
||
import static java.lang.String.format; | ||
|
||
public final class CustomInetAddressResolverProvider implements InetAddressResolverProvider { | ||
private static volatile boolean used = false; | ||
|
||
public CustomInetAddressResolverProvider() { | ||
} | ||
|
||
@Override | ||
public InetAddressResolver create() { | ||
return new CustomInetAddressResolver(); | ||
} | ||
|
||
static void assertUsed() throws AssertionError { | ||
if (!used) { | ||
throw new AssertionError(format("%s is not used", CustomInetAddressResolverProvider.class.getSimpleName())); | ||
} | ||
} | ||
|
||
private static void markUsed() { | ||
used = true; | ||
} | ||
|
||
private static final class CustomInetAddressResolver implements InetAddressResolver { | ||
private final DefaultInetAddressResolver wrapped; | ||
|
||
CustomInetAddressResolver() { | ||
wrapped = new DefaultInetAddressResolver(); | ||
} | ||
|
||
@Override | ||
public List<InetAddress> lookupByName(final String host) throws UnknownHostException { | ||
markUsed(); | ||
return wrapped.lookupByName(host); | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
graalvm-native-image-app/src/main/com/mongodb/internal/graalvm/DnsSpi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* 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.internal.graalvm; | ||
|
||
import com.mongodb.client.MongoClient; | ||
import com.mongodb.client.MongoClients; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.ArrayList; | ||
|
||
final class DnsSpi { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(DnsSpi.class); | ||
|
||
public static void main(final String... args) { | ||
LOGGER.info("Begin"); | ||
try (MongoClient client = args.length == 0 ? MongoClients.create() : MongoClients.create(args[0])) { | ||
LOGGER.info("Database names: {}", client.listDatabaseNames().into(new ArrayList<>())); | ||
} | ||
CustomInetAddressResolverProvider.assertUsed(); | ||
LOGGER.info("End"); | ||
} | ||
|
||
private DnsSpi() { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
graalvm-native-image-app/src/main/com/mongodb/internal/graalvm/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* 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. | ||
*/ | ||
@NonNullApi | ||
package com.mongodb.internal.graalvm; | ||
|
||
import com.mongodb.lang.NonNullApi; |
1 change: 1 addition & 0 deletions
1
...-app/src/main/resources/META-INF/services/com.mongodb.spi.dns.InetAddressResolverProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
com.mongodb.internal.graalvm.CustomInetAddressResolverProvider |