01: package liquibase.util;
02:
03: import java.net.InetAddress;
04: import java.net.UnknownHostException;
05: import java.net.SocketException;
06: import java.net.NetworkInterface;
07: import java.util.Enumeration;
08:
09: public class NetUtil {
10:
11: /**
12: * Smarter way to get localhost than InetAddress.getLocalHost. See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4665037
13: */
14: public static InetAddress getLocalHost()
15: throws UnknownHostException, SocketException {
16: InetAddress lch = null;
17: Enumeration<NetworkInterface> e = NetworkInterface
18: .getNetworkInterfaces();
19:
20: while (e.hasMoreElements()) {
21: NetworkInterface i = e.nextElement();
22:
23: Enumeration<InetAddress> ie = i.getInetAddresses();
24: if (!ie.hasMoreElements()) {
25: break;
26: }
27: lch = ie.nextElement();
28: if (!lch.isLoopbackAddress())
29: break;
30: }
31: return lch;
32: }
33:
34: }
|