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:
Norman Maurer 2017-07-24 12:03:25 +02:00
parent 486f962252
commit 34fdc7a33e

View File

@ -66,16 +66,27 @@ public final class DefaultDnsServerAddressStreamProvider implements DnsServerAdd
try { try {
DirContext ctx = new InitialDirContext(env); DirContext ctx = new InitialDirContext(env);
String dnsUrls = (String) ctx.getEnvironment().get("java.naming.provider.url"); 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(" "); String[] servers = dnsUrls.split(" ");
for (String server : servers) { for (String server : servers) {
try { try {
URI uri = new URI(server); 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(); int port = uri.getPort();
defaultNameServers.add(SocketUtils.socketAddress(uri.getHost(), port == -1 ? DNS_PORT : port)); defaultNameServers.add(SocketUtils.socketAddress(uri.getHost(), port == -1 ? DNS_PORT : port));
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
logger.debug("Skipping a malformed nameserver URI: {}", server, e); logger.debug("Skipping a malformed nameserver URI: {}", server, e);
} }
} }
}
} catch (NamingException ignore) { } catch (NamingException ignore) {
// Will try reflection if this fails. // Will try reflection if this fails.
} }