KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* InetAddressUtil
2  *
3  * Created on Nov 19, 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 import java.net.NetworkInterface JavaDoc;
27 import java.net.SocketException JavaDoc;
28 import java.net.UnknownHostException JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.logging.Logger JavaDoc;
33 import java.util.regex.Matcher JavaDoc;
34 import java.util.regex.Pattern JavaDoc;
35
36 /**
37  * InetAddress utility.
38  * @author stack
39  * @version $Date: 2007/01/13 01:31:39 $, $Revision: 1.2.16.1 $
40  */

41 public class InetAddressUtil {
42     private static Logger JavaDoc logger =
43         Logger.getLogger(InetAddressUtil.class.getName());
44     
45     /**
46      * ipv4 address.
47      */

48     public static Pattern JavaDoc IPV4_QUADS = Pattern.compile(
49         "([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})");
50     
51     private InetAddressUtil () {
52         super();
53     }
54     
55     /**
56      * Returns InetAddress for passed <code>host</code> IF its in
57      * IPV4 quads format (e.g. 128.128.128.128).
58      * <p>TODO: Move to an AddressParsingUtil class.
59      * @param host Host name to examine.
60      * @return InetAddress IF the passed name was an IP address, else null.
61      */

62     public static InetAddress JavaDoc getIPHostAddress(String JavaDoc host) {
63         InetAddress JavaDoc result = null;
64         Matcher JavaDoc matcher = IPV4_QUADS.matcher(host);
65         if (matcher == null || !matcher.matches()) {
66             return result;
67         }
68         try {
69             // Doing an Inet.getByAddress() avoids a lookup.
70
result = InetAddress.getByAddress(host,
71                     new byte[] {
72                     (byte)(new Integer JavaDoc(matcher.group(1)).intValue()),
73                     (byte)(new Integer JavaDoc(matcher.group(2)).intValue()),
74                     (byte)(new Integer JavaDoc(matcher.group(3)).intValue()),
75                     (byte)(new Integer JavaDoc(matcher.group(4)).intValue())});
76         } catch (NumberFormatException JavaDoc e) {
77             logger.warning(e.getMessage());
78         } catch (UnknownHostException JavaDoc e) {
79             logger.warning(e.getMessage());
80         }
81         return result;
82     }
83     
84     /**
85      * @return All known local names for this host or null if none found.
86      */

87     public static List JavaDoc<String JavaDoc> getAllLocalHostNames() {
88         List JavaDoc<String JavaDoc> localNames = new ArrayList JavaDoc<String JavaDoc>();
89         Enumeration JavaDoc e = null;
90         try {
91             e = NetworkInterface.getNetworkInterfaces();
92         } catch(SocketException JavaDoc exception) {
93             throw new RuntimeException JavaDoc(exception);
94         }
95         for (; e.hasMoreElements();) {
96             for (Enumeration JavaDoc ee =
97                 ((NetworkInterface JavaDoc)e.nextElement()).getInetAddresses();
98                     ee.hasMoreElements();) {
99                 InetAddress JavaDoc ia = (InetAddress JavaDoc)ee.nextElement();
100                 if (ia != null) {
101                     if (ia.getHostName() != null) {
102                         localNames.add(ia.getHostName());
103                     }
104                     if (ia.getHostAddress() != null) {
105                         localNames.add(ia.getHostAddress());
106                     }
107                 }
108             }
109         }
110         final String JavaDoc localhost = "localhost";
111         if (!localNames.contains(localhost)) {
112             localNames.add(localhost);
113         }
114         final String JavaDoc localhostLocaldomain = "localhost.localdomain";
115         if (!localNames.contains(localhostLocaldomain)) {
116             localNames.add(localhostLocaldomain);
117         }
118         return localNames;
119     }
120 }
Popular Tags