X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.util%2Fsrc%2Forg%2Fargeo%2Futil%2Fnaming%2Fdns%2FDnsBrowser.java;h=4fba4340540f1f15fdb5707cd5fec9fa356f8919;hb=3c1cdc594d954520b14646102b366290bdad58c7;hp=4bd05268cc8096ce53d631d4ae351f32dd032d0c;hpb=f520af45449b203a879392e8a0aeda6703abadfa;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.util/src/org/argeo/util/naming/dns/DnsBrowser.java b/org.argeo.util/src/org/argeo/util/naming/dns/DnsBrowser.java index 4bd05268c..4fba43405 100644 --- a/org.argeo.util/src/org/argeo/util/naming/dns/DnsBrowser.java +++ b/org.argeo.util/src/org/argeo/util/naming/dns/DnsBrowser.java @@ -9,11 +9,15 @@ import java.util.Collections; import java.util.Hashtable; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.SortedSet; +import java.util.StringJoiner; import java.util.TreeMap; import java.util.TreeSet; import javax.naming.Binding; +import javax.naming.Context; +import javax.naming.NameNotFoundException; import javax.naming.NamingEnumeration; import javax.naming.NamingException; import javax.naming.directory.Attribute; @@ -24,62 +28,91 @@ import javax.naming.directory.InitialDirContext; public class DnsBrowser implements Closeable { private final DirContext initialCtx; - public DnsBrowser() throws NamingException { - this(null); + public DnsBrowser() { + this(new ArrayList<>()); } - public DnsBrowser(String dnsServerUrls) throws NamingException { - Hashtable 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 dnsServerUrls) { + try { + Objects.requireNonNull(dnsServerUrls); + Hashtable 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); + } } - public Map> getAllRecords(String name) throws NamingException { - Map> res = new TreeMap<>(); - Attributes attrs = initialCtx.getAttributes(name); - NamingEnumeration ids = attrs.getIDs(); - while (ids.hasMore()) { - String recordType = ids.next(); - List lst = new ArrayList(); - res.put(recordType, lst); - Attribute attr = attrs.get(recordType); - addValues(attr, lst); + public Map> getAllRecords(String name) { + try { + Map> res = new TreeMap<>(); + Attributes attrs = initialCtx.getAttributes(name); + NamingEnumeration ids = attrs.getIDs(); + while (ids.hasMore()) { + String recordType = ids.next(); + List lst = new ArrayList(); + res.put(recordType, lst); + Attribute attr = attrs.get(recordType); + addValues(attr, lst); + } + return Collections.unmodifiableMap(res); + } catch (NamingException e) { + throw new IllegalStateException("Cannot get allrecords of " + name, e); } - return Collections.unmodifiableMap(res); } /** * Return a single record (typically A, AAAA, etc. or null if not available. * Will fail if multiple records. */ - public String getRecord(String name, String recordType) throws NamingException { - Attributes attrs = initialCtx.getAttributes(name, new String[] { recordType }); - if (attrs.size() == 0) + public String getRecord(String name, String recordType) { + try { + Attributes attrs = initialCtx.getAttributes(name, new String[] { recordType }); + if (attrs.size() == 0) + return null; + Attribute attr = attrs.get(recordType); + if (attr.size() > 1) + throw new IllegalArgumentException("Multiple record type " + recordType); + assert attr.size() != 0; + Object value = attr.get(); + assert value != null; + return value.toString(); + } catch (NameNotFoundException e) { return null; - Attribute attr = attrs.get(recordType); - if (attr.size() > 1) - throw new IllegalArgumentException("Multiple record type " + recordType); - assert attr.size() != 0; - Object value = attr.get(); - assert value != null; - return value.toString(); + } catch (NamingException e) { + throw new IllegalStateException("Cannot get DNS entry " + recordType + " of " + name, e); + } } /** * Return records of a given type. */ - public List getRecords(String name, String recordType) throws NamingException { - List res = new ArrayList(); - Attributes attrs = initialCtx.getAttributes(name, new String[] { recordType }); - Attribute attr = attrs.get(recordType); - addValues(attr, res); - return res; + public List getRecords(String name, String recordType) { + try { + List res = new ArrayList(); + Attributes attrs = initialCtx.getAttributes(name, new String[] { recordType }); + Attribute attr = attrs.get(recordType); + addValues(attr, res); + return res; + } catch (NamingException e) { + throw new IllegalStateException("Cannot get records " + recordType + " of " + name, e); + } } /** Ordered, with preferred first. */ - public List getSrvRecordsAsHosts(String name, boolean withPort) throws NamingException { + public List getSrvRecordsAsHosts(String name, boolean withPort) { List raw = getRecords(name, "SRV"); if (raw.size() == 0) return null; @@ -116,14 +149,18 @@ public class DnsBrowser implements Closeable { } - public List listEntries(String name) throws NamingException { - List res = new ArrayList(); - NamingEnumeration ne = initialCtx.listBindings(name); - while (ne.hasMore()) { - Binding b = ne.next(); - res.add(b.getName()); + public List listEntries(String name) { + try { + List res = new ArrayList(); + NamingEnumeration ne = initialCtx.listBindings(name); + while (ne.hasMore()) { + Binding b = ne.next(); + res.add(b.getName()); + } + return Collections.unmodifiableList(res); + } catch (NamingException e) { + throw new IllegalStateException("Cannot list entries of " + name, e); } - return Collections.unmodifiableList(res); } @Override