--- /dev/null 2017-11-29 10:09:48.676000048 +0000 +++ new/src/java.naming/share/classes/com/sun/jndi/ldap/DefaultLdapDnsProvider.java 2017-11-29 17:13:34.752980564 +0000 @@ -0,0 +1,52 @@ +package com.sun.jndi.ldap; + +import java.util.ArrayList; +import java.util.Hashtable; +import java.util.List; +import javax.naming.NamingException; +import javax.naming.ldap.LdapDnsProvider; +import javax.naming.ldap.LdapDnsProviderResult; + +public class DefaultLdapDnsProvider extends LdapDnsProvider { + @Override + public LdapDnsProviderResult lookupEndpoints(String url, Hashtable env) { + String domainName = ""; + List urls = new ArrayList<>(); + try { + LdapURL ldapUrl = new LdapURL(url); + String dn = ldapUrl.getDN(); + String host = ldapUrl.getHost(); + int port = ldapUrl.getPort(); + String[] hostports; + + // handle a URL with no hostport (ldap:/// or ldaps:///) + // locate the LDAP service using the URL's distinguished name + if (host == null + && port == -1 + && dn != null + && (domainName = ServiceLocator.mapDnToDomainName(dn)) != null + && (hostports = ServiceLocator.getLdapService(domainName, env)) != null) + { + // Generate new URLs that include the discovered hostports. + // Reuse the original URL scheme. + String scheme = ldapUrl.getScheme() + "://"; + String query = ldapUrl.getQuery(); + String urlSuffix = ldapUrl.getPath() + (query != null ? query : ""); + for (int i = 0; i < hostports.length; i++) { + // the hostports come from the DNS SRV records + // we assume the SRV record is scheme aware + urls.add(scheme + hostports[i] + urlSuffix); + } + } else { + // we don't have enough information to set the domain name + // correctly + domainName = ""; + urls.add(url); + } + } catch (NamingException e) { + // leave list of resolved urls empty + } + return new LdapDnsProviderResult(domainName, urls); + } + +}