KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > net > DNSUtil


1 /**
2  * $RCSfile: DNSUtil.java,v $
3  * $Revision: 1.2 $
4  * $Date: 2005/06/02 05:04:54 $
5  *
6  * Copyright (C) 2004-2005 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger.net;
13
14 import org.jivesoftware.util.Log;
15
16 import javax.naming.directory.Attributes JavaDoc;
17 import javax.naming.directory.InitialDirContext JavaDoc;
18 import javax.naming.directory.DirContext JavaDoc;
19 import java.util.Hashtable JavaDoc;
20
21 /**
22  * Utilty class to perform DNS lookups for XMPP services.
23  *
24  * @author Matt Tucker
25  */

26 public class DNSUtil {
27
28     private static DirContext JavaDoc context;
29
30     static {
31         try {
32             Hashtable JavaDoc env = new Hashtable JavaDoc();
33             env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
34             context = new InitialDirContext JavaDoc(env);
35         }
36         catch (Exception JavaDoc e) {
37             Log.error(e);
38         }
39     }
40
41     /**
42      * Returns the host name and port that the specified XMPP server can be
43      * reached at for server-to-server communication. A DNS lookup for a SRV
44      * record in the form "_xmpp-server._tcp.example.com" is attempted, according
45      * to section 14.4 of RFC 3920. If that lookup fails, a lookup in the older form
46      * of "_jabber._tcp.example.com" is attempted since servers that implement an
47      * older version of the protocol may be listed using that notation. If that
48      * lookup fails as well, it's assumed that the XMPP server lives at the
49      * host resolved by a DNS lookup at the specified domain on the default port
50      * of 5269.<p>
51      *
52      * As an example, a lookup for "example.com" may return "im.example.com:5269".
53      *
54      * @param domain the domain.
55      * @return a HostAddress, which encompasses the hostname and port that the XMPP
56      * server can be reached at for the specified domain.
57      */

58     public static HostAddress resolveXMPPServerDomain(String JavaDoc domain) {
59         if (context == null) {
60             return new HostAddress(domain, 5269);
61         }
62         String JavaDoc host = domain;
63         int port = 5269;
64         try {
65             Attributes JavaDoc dnsLookup = context.getAttributes("_xmpp-server._tcp." + domain);
66             String JavaDoc srvRecord = (String JavaDoc)dnsLookup.get("SRV").get();
67             String JavaDoc [] srvRecordEntries = srvRecord.split(" ");
68             port = Integer.parseInt(srvRecordEntries[srvRecordEntries.length-2]);
69             host = srvRecordEntries[srvRecordEntries.length-1];
70         }
71         catch (Exception JavaDoc e) {
72             // Attempt lookup with older "jabber" name.
73
try {
74                 Attributes JavaDoc dnsLookup = context.getAttributes("_jabber._tcp." + domain);
75                 String JavaDoc srvRecord = (String JavaDoc)dnsLookup.get("SRV").get();
76                 String JavaDoc [] srvRecordEntries = srvRecord.split(" ");
77                 port = Integer.parseInt(srvRecordEntries[srvRecordEntries.length-2]);
78                 host = srvRecordEntries[srvRecordEntries.length-1];
79             }
80             catch (Exception JavaDoc e2) { }
81         }
82         // Host entries in DNS should end with a ".".
83
if (host.endsWith(".")) {
84             host = host.substring(0, host.length()-1);
85         }
86         return new HostAddress(host, port);
87     }
88
89     /**
90      * Encapsulates a hostname and port.
91      */

92     public static class HostAddress {
93
94         private String JavaDoc host;
95         private int port;
96
97         private HostAddress(String JavaDoc host, int port) {
98             this.host = host;
99             this.port = port;
100         }
101
102         /**
103          * Returns the hostname.
104          *
105          * @return the hostname.
106          */

107         public String JavaDoc getHost() {
108             return host;
109         }
110
111         /**
112          * Returns the port.
113          *
114          * @return the port.
115          */

116         public int getPort() {
117             return port;
118         }
119
120         public String JavaDoc toString() {
121             return host + ":" + port;
122         }
123     }
124 }
Popular Tags