KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > util > Localhost


1 /*
2  * Localhost.java
3  *
4  * Created on August 18, 2005, 8:57 AM
5  *
6  */

7
8 package org.sapia.ubik.util;
9
10 import java.net.InetAddress JavaDoc;
11 import java.net.NetworkInterface JavaDoc;
12 import java.net.SocketException JavaDoc;
13 import java.net.UnknownHostException JavaDoc;
14 import java.util.Enumeration JavaDoc;
15 import java.util.regex.Pattern JavaDoc;
16
17 import org.sapia.ubik.rmi.Consts;
18 import org.sapia.ubik.rmi.server.Log;
19
20 /**
21  * This class provides a utility method that can be used to retrieve the
22  * IP address of this host.
23  * <p>
24  * This class internaly uses the <code>NetworkInterface</code> class to
25  * retrieve that address. If an address can't be found that way, the
26  * following code is used:
27  * <pre>
28  * InetAddress.getLocalHost()
29  * </pre>
30  * The above code is know to return 0.0.0.0 or the loopback address under some
31  * Linux distributions, which is what this class first attempts to use
32  * the <code>NetworkInterface</code>.
33  *
34  * @author yduchesne
35  */

36 public class Localhost {
37   
38   private static String JavaDoc LOCALHOST = "0.0.0.0";
39   private static String JavaDoc LOOPBACK = "127.0.0.1";
40   private static Pattern JavaDoc DEFAULT_IP_PATTERN = Pattern.compile("\\d+\\.\\d+\\.\\d+\\.\\d+");
41   private static Pattern JavaDoc _pattern = DEFAULT_IP_PATTERN;
42   
43   static{
44     String JavaDoc patternStr = System.getProperty(Consts.IP_PATTERN_KEY);
45     if(patternStr != null){
46       if(Log.isDebug()){
47         Log.debug(Localhost.class, "Got local network address pattern: " + patternStr);
48       }
49       _pattern = Pattern.compile(patternStr);
50     }
51   }
52   
53   /**
54    * @return the <code>InetAddress</code> of this host.
55    */

56   public static InetAddress JavaDoc getLocalAddress() throws UnknownHostException JavaDoc{
57     NetworkInterface JavaDoc iface;
58     try{
59       for(Enumeration JavaDoc ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements();){
60         iface = (NetworkInterface JavaDoc)ifaces.nextElement();
61         InetAddress JavaDoc ia = null;
62         for(Enumeration JavaDoc ips = iface.getInetAddresses(); ips.hasMoreElements();){
63           ia = (InetAddress JavaDoc)ips.nextElement();
64           String JavaDoc addr = ia.getHostAddress();
65           if(addr.equals(LOCALHOST) || addr.equals(LOOPBACK)){
66             continue;
67           }
68           if(isLocalAddress(_pattern, addr)){
69             if(Log.isDebug()){
70               Log.debug(Localhost.class, "Address " + addr + " matches: " + _pattern.toString());
71             }
72             return ia;
73           }
74         }
75       }
76     }catch(SocketException JavaDoc e){}
77     return InetAddress.getLocalHost();
78   }
79   
80   static boolean isLocalAddress(Pattern JavaDoc pattern, String JavaDoc addr){
81     return pattern.matcher(addr).matches();
82   }
83   
84 }
85
Popular Tags