org.xbill.DNS.config.SunJvmResolverConfigProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of org.apache.servicemix.bundles.dnsjava
Show all versions of org.apache.servicemix.bundles.dnsjava
This OSGi bundle wraps ${pkgArtifactId} ${pkgVersion} jar file.
// SPDX-License-Identifier: BSD-3-Clause
package org.xbill.DNS.config;
import java.lang.reflect.Method;
import java.net.InetSocketAddress;
import java.util.List;
/**
* Resolver config provider that queries the traditional class {@code
* sun.net.dns.ResolverConfiguration} via reflection.
*
*
* - As of Java 9, generates an illegal reflective access exception.
*
- As of Java 16, adding the JVM flag {@code --add-opens java.base/sun.net.dns=ALL-UNNAMED} is
* required.
*
- On Windows, may return invalid nameservers of disconnected NICs before Java 15, JDK-7006496.
*
*/
public class SunJvmResolverConfigProvider extends BaseResolverConfigProvider {
@Override
public void initialize() throws InitializationException {
reset();
try {
Class> resConfClass = Class.forName("sun.net.dns.ResolverConfiguration");
Method open = resConfClass.getDeclaredMethod("open");
Object resConf = open.invoke(null);
Method nameserversMethod = resConfClass.getMethod("nameservers");
@SuppressWarnings("unchecked")
List jvmNameservers = (List) nameserversMethod.invoke(resConf);
for (String ns : jvmNameservers) {
addNameserver(new InetSocketAddress(ns, 53));
}
Method searchlistMethod = resConfClass.getMethod("searchlist");
@SuppressWarnings("unchecked")
List jvmSearchlist = (List) searchlistMethod.invoke(resConf);
for (String n : jvmSearchlist) {
addSearchPath(n);
}
} catch (Exception e) {
throw new InitializationException(e);
}
}
@Override
public boolean isEnabled() {
return Boolean.getBoolean("dnsjava.configprovider.sunjvm.enabled");
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy