1 11 12 package org.jivesoftware.messenger.net; 13 14 import org.jivesoftware.util.Log; 15 16 import javax.naming.directory.Attributes ; 17 import javax.naming.directory.InitialDirContext ; 18 import javax.naming.directory.DirContext ; 19 import java.util.Hashtable ; 20 21 26 public class DNSUtil { 27 28 private static DirContext context; 29 30 static { 31 try { 32 Hashtable env = new Hashtable (); 33 env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory"); 34 context = new InitialDirContext (env); 35 } 36 catch (Exception e) { 37 Log.error(e); 38 } 39 } 40 41 58 public static HostAddress resolveXMPPServerDomain(String domain) { 59 if (context == null) { 60 return new HostAddress(domain, 5269); 61 } 62 String host = domain; 63 int port = 5269; 64 try { 65 Attributes dnsLookup = context.getAttributes("_xmpp-server._tcp." + domain); 66 String srvRecord = (String )dnsLookup.get("SRV").get(); 67 String [] srvRecordEntries = srvRecord.split(" "); 68 port = Integer.parseInt(srvRecordEntries[srvRecordEntries.length-2]); 69 host = srvRecordEntries[srvRecordEntries.length-1]; 70 } 71 catch (Exception e) { 72 try { 74 Attributes dnsLookup = context.getAttributes("_jabber._tcp." + domain); 75 String srvRecord = (String )dnsLookup.get("SRV").get(); 76 String [] srvRecordEntries = srvRecord.split(" "); 77 port = Integer.parseInt(srvRecordEntries[srvRecordEntries.length-2]); 78 host = srvRecordEntries[srvRecordEntries.length-1]; 79 } 80 catch (Exception e2) { } 81 } 82 if (host.endsWith(".")) { 84 host = host.substring(0, host.length()-1); 85 } 86 return new HostAddress(host, port); 87 } 88 89 92 public static class HostAddress { 93 94 private String host; 95 private int port; 96 97 private HostAddress(String host, int port) { 98 this.host = host; 99 this.port = port; 100 } 101 102 107 public String getHost() { 108 return host; 109 } 110 111 116 public int getPort() { 117 return port; 118 } 119 120 public String toString() { 121 return host + ":" + port; 122 } 123 } 124 } | Popular Tags |