Skip to content

Commit 30eb794

Browse files
authored
Only override Context.PROVIDER_URL if the default isn't supported (#853)
JAVA-4423
1 parent 1edd296 commit 30eb794

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

driver-core/src/main/com/mongodb/internal/dns/DefaultDnsResolver.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,18 @@ public String resolveAdditionalQueryParametersFromTxtRecords(final String host)
147147
private static InitialDirContext createDnsDirContext() {
148148
Hashtable<String, String> envProps = new Hashtable<String, String>();
149149
envProps.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
150-
envProps.put(Context.PROVIDER_URL, "dns:");
150+
151151
try {
152152
return new InitialDirContext(envProps);
153153
} catch (NamingException e) {
154-
throw new MongoClientException("Unable to support mongodb+srv// style connections as the 'com.sun.jndi.dns.DnsContextFactory' "
155-
+ "class is not available in this JRE. A JNDI context is required for resolving SRV records.", e);
154+
// Just in case the provider url default has been changed to a non-dns pseudo url, fallback to the JDK default
155+
envProps.put(Context.PROVIDER_URL, "dns:");
156+
try {
157+
return new InitialDirContext(envProps);
158+
} catch (NamingException ex) {
159+
throw new MongoClientException("Unable to support mongodb+srv// style connections as the 'com.sun.jndi.dns.DnsContextFactory' "
160+
+ "class is not available in this JRE. A JNDI context is required for resolving SRV records.", e);
161+
}
156162
}
157163
}
158164
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
17+
package com.mongodb.internal.dns;
18+
19+
import com.mongodb.MongoConfigurationException;
20+
import org.junit.jupiter.api.AfterEach;
21+
import org.junit.jupiter.api.Test;
22+
23+
import javax.naming.Context;
24+
25+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
26+
import static org.junit.jupiter.api.Assertions.assertThrows;
27+
28+
public class DefaultDnsResolverTest {
29+
private static final String TEST_HOST = "test1.test.build.10gen.cc";
30+
private static final String DEFAULT_PROVIDER_URL_VALUE = System.getProperty(Context.PROVIDER_URL);
31+
32+
@AfterEach
33+
public void resetDefaultProviderUrl() {
34+
if (DEFAULT_PROVIDER_URL_VALUE != null) {
35+
System.setProperty(Context.PROVIDER_URL, DEFAULT_PROVIDER_URL_VALUE);
36+
}
37+
}
38+
39+
@Test
40+
public void nonDnsProviderUrlShouldBeIgnored() {
41+
System.setProperty(Context.PROVIDER_URL, "file:///tmp/provider.txt");
42+
assertDoesNotThrow(() -> new DefaultDnsResolver().resolveHostFromSrvRecords(TEST_HOST, "mongodb"));
43+
}
44+
45+
@Test
46+
public void dnsProviderUrlShouldNotBeIgnored() {
47+
System.setProperty(Context.PROVIDER_URL, "dns:///mongodb.unknown.server.com");
48+
assertThrows(MongoConfigurationException.class, () -> new DefaultDnsResolver().resolveHostFromSrvRecords(TEST_HOST, "mongodb"));
49+
}
50+
}

0 commit comments

Comments
 (0)