1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.archive.util;
24
25 import java.net.InetAddress;
26
27 import org.xbill.DNS.ARecord;
28 import org.xbill.DNS.DClass;
29 import org.xbill.DNS.Record;
30 import org.xbill.DNS.TextParseException;
31 import org.xbill.DNS.Type;
32 import org.xbill.DNS.Lookup;;
33
34 /***
35 * Utility methods based on DNSJava.
36 * Use these utilities to avoid having to use the native InetAddress lookup.
37 * @author stack
38 * @version $Date: 2007-01-06 05:17:35 +0000 (Sat, 06 Jan 2007) $, $Revision: 4837 $
39 */
40 public class DNSJavaUtil {
41 private DNSJavaUtil() {
42 super();
43 }
44
45 /***
46 * Return an InetAddress for passed <code>host</code>.
47 *
48 * If passed host is an IPv4 address, we'll not do a DNSJava
49 * lookup.
50 *
51 * @param host Host to lookup in dnsjava.
52 * @return A host address or null if not found.
53 */
54 public static InetAddress getHostAddress(String host) {
55 InetAddress hostAddress = InetAddressUtil.getIPHostAddress(host);
56 if (hostAddress != null) {
57 return hostAddress;
58 }
59
60
61 Record[] rrecordSet;
62 try {
63 rrecordSet = (new Lookup(host, Type.A, DClass.IN)).run();
64 } catch (TextParseException e) {
65 rrecordSet = null;
66 }
67 if (rrecordSet != null) {
68
69
70 for (int i = 0; i < rrecordSet.length; i++) {
71 if (rrecordSet[i].getType() != Type.A) {
72 continue;
73 }
74 hostAddress = ((ARecord)rrecordSet[i]).getAddress();
75 break;
76 }
77 }
78 return hostAddress;
79 }
80 }