Skip invalid hostnames when construct default dns servers to use.
Motivation: When the hostname portion can not be extracted we should just skip the server as otherwise we will produce and exception when trying to create the InetSocketAddress. This was happing when trying to run the test-suite on a system and using java7: java.lang.IllegalArgumentException: hostname can't be null at java.net.InetSocketAddress.checkHost(InetSocketAddress.java:149) at java.net.InetSocketAddress.<init>(InetSocketAddress.java:216) at io.netty.util.internal.SocketUtils$10.run(SocketUtils.java:171) at io.netty.util.internal.SocketUtils$10.run(SocketUtils.java:168) at java.security.AccessController.doPrivileged(Native Method) at io.netty.util.internal.SocketUtils.socketAddress(SocketUtils.java:168) at io.netty.resolver.dns.DefaultDnsServerAddressStreamProvider.<clinit>(DefaultDnsServerAddressStreamProvider.java:74) at io.netty.resolver.dns.DnsServerAddressesTest.testDefaultAddresses(DnsServerAddressesTest.java:39) Modifications: Skip if hostname can not be extracted. Result: No more java.lang.ExceptionInInitializerError.
This commit is contained in:
parent
486f962252
commit
34fdc7a33e
@ -66,16 +66,27 @@ public final class DefaultDnsServerAddressStreamProvider implements DnsServerAdd
|
||||
try {
|
||||
DirContext ctx = new InitialDirContext(env);
|
||||
String dnsUrls = (String) ctx.getEnvironment().get("java.naming.provider.url");
|
||||
// Only try if not empty as otherwise we will produce an exception
|
||||
if (dnsUrls != null && !dnsUrls.isEmpty()) {
|
||||
String[] servers = dnsUrls.split(" ");
|
||||
for (String server : servers) {
|
||||
try {
|
||||
URI uri = new URI(server);
|
||||
String host = new URI(server).getHost();
|
||||
|
||||
if (host == null || host.isEmpty()) {
|
||||
logger.debug(
|
||||
"Skipping a nameserver URI as host portion could not be extracted: {}", server);
|
||||
// If the host portion can not be parsed we should just skip this entry.
|
||||
continue;
|
||||
}
|
||||
int port = uri.getPort();
|
||||
defaultNameServers.add(SocketUtils.socketAddress(uri.getHost(), port == -1 ? DNS_PORT : port));
|
||||
} catch (URISyntaxException e) {
|
||||
logger.debug("Skipping a malformed nameserver URI: {}", server, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (NamingException ignore) {
|
||||
// Will try reflection if this fails.
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user