KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > archive > util > DNSJavaUtil


1 /* DNSJavaUtil
2  *
3  * Created on Oct 8, 2004
4  *
5  * Copyright (C) 2004 Internet Archive.
6  *
7  * This file is part of the Heritrix web crawler (crawler.archive.org).
8  *
9  * Heritrix is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * any later version.
13  *
14  * Heritrix is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser Public License
20  * along with Heritrix; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */

23 package org.archive.util;
24
25 import java.net.InetAddress JavaDoc;
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/13 01:31:39 $, $Revision: 1.3.20.1 $
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 JavaDoc getHostAddress(String JavaDoc host) {
55         InetAddress JavaDoc hostAddress = InetAddressUtil.getIPHostAddress(host);
56         if (hostAddress != null) {
57             return hostAddress;
58         }
59         
60         // Ask dnsjava for the inetaddress. Should be in its cache.
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             // Get TTL and IP info from the first A record (there may be
69
// multiple, e.g. www.washington.edu).
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 }
81
Popular Tags