From 953b84213a97f541b112b46b41afffae1b26e5e1 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 19 Dec 2016 16:04:54 +0300 Subject: juick-xmpp: DNSQueries refactoring --- .../com/juick/components/s2s/ConnectionOut.java | 4 ++- .../java/com/juick/components/s2s/DNSQueries.java | 34 +++++++++------------- 2 files changed, 17 insertions(+), 21 deletions(-) (limited to 'juick-xmpp/src') 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 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()); - 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 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); } } -- cgit v1.2.3