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=376c51edc4b1fbc0b53cb1f109d7d8fdf25186fc;hpb=8302ed5e76967f1d618b59ebe4ae11223e5037c3;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 376c51edc..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 @@ -28,41 +28,49 @@ import javax.naming.directory.InitialDirContext; public class DnsBrowser implements Closeable { private final DirContext initialCtx; - public DnsBrowser() throws NamingException { + public DnsBrowser() { this(new ArrayList<>()); } - public DnsBrowser(List dnsServerUrls) throws NamingException { - 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; + 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()); } - if (specified) - env.put(Context.PROVIDER_URL, providerUrl.toString()); + initialCtx = new InitialDirContext(env); + } catch (NamingException e) { + throw new IllegalStateException("Cannot initialise DNS borowser.", e); } - initialCtx = new InitialDirContext(env); } - 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); } /** @@ -91,16 +99,20 @@ public class DnsBrowser implements Closeable { /** * 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; @@ -137,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