Looking to enable Java applications to have finer control over internet addressing, plans are afoot in the OpenJDK community to develop a service provider interface (SPI) for host name and address resolution.
A JDK Enhancement Proposal (JEP) under consideration calls for development of such an SPI for host name resolution so
java.net.InetAddress can use resolvers other than the operating platform’s built-in resolver.
In explaining the motivation for this proposal, proponents said the
java.net.INetAddress API resolves host names to IP addresses and vice versa. The API currently uses the operating system’s native resolver, which typically is configured to use a combination of a local
hosts file and the Domain Name System (DNS). Motivations for defining an SPI for name and address resolution include:
- For customization, a resolver SPI would enable frameworks and applications to have finer control over resolution results and allow existing libraries to be retrofitted with a custom resolver.
- Project Loom explores Java VM features and APIs built on them. A resolution operation within the
InetAddressAPI currently blocks an OS call, which is a problem for Loom’s user-mode virtual threads, with platform threads unable to service other virtual threads while waiting for a resolution operation to complete. An alternative resolver could implement the DNS client protocol directly without blocking.
- For emerging network protocols, a resolver SPI would allow seamless integration of new protocols such as DNS over Quick UDP Internet Connections (QUIC), Transport Layer Security (TLS), or HTTPS.
- Prototyping and testing often require control of host name and address resolution results.
Oracle said the proposal was part of an effort to offer asynchronous programming in Java, because legacy DNS lookups are synchronous. The current implementation of INetAddress relies on the OS in a blocking call. The plan is to make it pluggable.
The proposal is not being targeted to any specific version of standard Java as of September 9. Without an SPI such as the one being proposed, applications will continue having to use workarounds. An application can use the Java Naming and Directory Interface (JNDI) and its DNS provider to look up network addresses and names. Although this approach can be useful for applications that require fine control of DNS lookups, it is decoupled from
InetAddress, so using the platform’s networking API requires additional effort. Also, an application can use the OS’s resolver libraries directly via Java Native Interface (JNI) or the foreign function API from Project Panama. As with JNDI, this approach is decoupled from
InetAddress and awkward to use.