Example usage for java.net Inet4Address getHostAddress

List of usage examples for java.net Inet4Address getHostAddress

Introduction

In this page you can find the example usage for java.net Inet4Address getHostAddress.

Prototype

public String getHostAddress() 

Source Link

Document

Returns the IP address string in textual presentation form.

Usage

From source file:org.commonjava.maven.galley.cache.infinispan.FastLocalCacheProvider.java

private String getCurrentNodeIp() throws SocketException {

    final Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();

    Inet4Address top = null;
    while (nis.hasMoreElements()) {
        final NetworkInterface ni = nis.nextElement();
        final Enumeration<InetAddress> ips = ni.getInetAddresses();

        while (ips.hasMoreElements()) {
            final InetAddress ip = ips.nextElement();
            if (ip instanceof Inet4Address && !ip.isLinkLocalAddress()) {
                if (top == null) {
                    top = (Inet4Address) ip;
                } else if (!top.isSiteLocalAddress() && ip.isSiteLocalAddress()) {
                    top = (Inet4Address) ip;
                }/*from ww  w. ja  v  a 2s  .co m*/
            }
        }
    }

    if (top == null) {
        throw new IllegalStateException("[galley] IP not found.");
    }

    return top.getHostAddress();
}