- public DnsBrowser(String dnsServerUrls) throws NamingException {
- Hashtable<String, Object> env = new Hashtable<>();
- env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
- if (dnsServerUrls != null)
- env.put("java.naming.provider.url", dnsServerUrls);
- initialCtx = new InitialDirContext(env);
+ public DnsBrowser(List<String> dnsServerUrls) {
+ try {
+ Objects.requireNonNull(dnsServerUrls);
+ Hashtable<String, Object> env = new Hashtable<>();
+ env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
+ if (!dnsServerUrls.isEmpty()) {
+ boolean specified = false;
+ StringJoiner providerUrl = new StringJoiner(" ");
+ for (String dnsUrl : dnsServerUrls) {
+ if (dnsUrl != null) {
+ providerUrl.add(dnsUrl);
+ specified = true;
+ }
+ }
+ if (specified)
+ env.put(Context.PROVIDER_URL, providerUrl.toString());
+ }
+ initialCtx = new InitialDirContext(env);
+ } catch (NamingException e) {
+ throw new IllegalStateException("Cannot initialise DNS borowser.", e);
+ }