diff options
author | Vitaly Takmazov | 2016-12-19 16:04:54 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2016-12-19 16:04:54 +0300 |
commit | 953b84213a97f541b112b46b41afffae1b26e5e1 (patch) | |
tree | f2f42e6c00e17eee141dec8a75d9d903f1c44c9d /juick-xmpp/src | |
parent | 75cb488222629a36d20765f5dada5fea1c83b6cf (diff) |
juick-xmpp: DNSQueries refactoring
Diffstat (limited to 'juick-xmpp/src')
-rw-r--r-- | juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionOut.java | 4 | ||||
-rw-r--r-- | juick-xmpp/src/main/java/com/juick/components/s2s/DNSQueries.java | 34 |
2 files changed, 17 insertions, 21 deletions
diff --git a/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionOut.java b/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionOut.java index 87e3cdbd..e39c143d 100644 --- a/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionOut.java +++ b/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionOut.java @@ -12,6 +12,7 @@ import javax.net.ssl.SSLException; import javax.net.ssl.SSLSocket; import java.io.EOFException; import java.io.IOException; +import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketException; import java.security.KeyManagementException; @@ -64,7 +65,8 @@ public class ConnectionOut extends Connection implements Runnable { logger.info("stream to {} start", to); try { socket = new Socket(); - socket.connect(DNSQueries.getServerAddress(to)); + InetSocketAddress address = DNSQueries.getServerAddress(to); + socket.connect(address); restartParser(); sendOpenStream(); diff --git a/juick-xmpp/src/main/java/com/juick/components/s2s/DNSQueries.java b/juick-xmpp/src/main/java/com/juick/components/s2s/DNSQueries.java index 6de9e15d..887dfc5c 100644 --- a/juick-xmpp/src/main/java/com/juick/components/s2s/DNSQueries.java +++ b/juick-xmpp/src/main/java/com/juick/components/s2s/DNSQueries.java @@ -1,5 +1,7 @@ package com.juick.components.s2s; +import org.apache.commons.lang3.math.NumberUtils; + import java.net.InetSocketAddress; import java.net.UnknownHostException; import java.util.Hashtable; @@ -17,31 +19,23 @@ public class DNSQueries { private static Random rand = new Random(); - public static InetSocketAddress getServerAddress(String hostname) throws UnknownHostException { + public static InetSocketAddress getServerAddress(String hostname) throws UnknownHostException, NamingException { String host = hostname; int port = 5269; - try { - Hashtable<String, String> env = new Hashtable<String, String>(5); - env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory"); - DirContext ctx = new InitialDirContext(env); - Attribute att = ctx.getAttributes("_xmpp-server._tcp." + hostname, new String[]{"SRV"}).get("SRV"); - - if (att != null && att.size() > 0) { - int i = rand.nextInt(att.size()); - try { - String srv[] = att.get(i).toString().split(" "); - port = Integer.parseInt(srv[2]); - host = srv[3]; - } catch (Exception e) { - } - } - - ctx.close(); - } catch (NamingException e) { - } + Hashtable<String, String> env = new Hashtable<>(5); + env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory"); + DirContext ctx = new InitialDirContext(env); + Attribute att = ctx.getAttributes("_xmpp-server._tcp." + hostname, new String[]{"SRV"}).get("SRV"); + if (att != null && att.size() > 0) { + int i = rand.nextInt(att.size()); + String srv[] = att.get(i).toString().split(" "); + port = NumberUtils.toInt(srv[2], 5269); + host = srv[3]; + } + ctx.close(); return new InetSocketAddress(host, port); } } |