Coverage Report - liquibase.util.NetUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
NetUtil
0%
0/16
0%
0/10
7
 
 1  
 package liquibase.util;
 2  
 
 3  
 import java.net.InetAddress;
 4  
 import java.net.NetworkInterface;
 5  
 import java.net.SocketException;
 6  
 import java.net.UnknownHostException;
 7  
 import java.util.Enumeration;
 8  
 
 9  0
 public class NetUtil {
 10  
 
 11  
     /**
 12  
      * Smarter way to get localhost than InetAddress.getLocalHost. See
 13  
      * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4665037
 14  
      */
 15  
     public static InetAddress getLocalHost() throws UnknownHostException, SocketException {
 16  
         // Windows Vista returns the IPv6 InetAddress using this method, which is not
 17  
         // particularly useful as it has no name or useful address, just "0:0:0:0:0:0:0:1".
 18  
         // That is why windows should be treated differently to linux/unix and use the
 19  
         // default way of getting the localhost.
 20  0
         String osName = System.getProperty("os.name");
 21  0
         if (osName != null && osName.toLowerCase().contains("windows")) {
 22  0
             return InetAddress.getLocalHost();
 23  
         }
 24  
 
 25  0
         InetAddress lch = null;
 26  0
         Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
 27  
 
 28  0
         while (e.hasMoreElements()) {
 29  0
             NetworkInterface i = e.nextElement();
 30  
 
 31  0
             Enumeration<InetAddress> ie = i.getInetAddresses();
 32  0
             if (!ie.hasMoreElements()) {
 33  0
                 break;
 34  
             }
 35  0
             lch = ie.nextElement();
 36  0
             if (!lch.isLoopbackAddress())
 37  0
                 break;
 38  0
         }
 39  0
         return lch;
 40  
     }
 41  
 
 42  
 }