Example usage for java.net UnknownHostException initCause

List of usage examples for java.net UnknownHostException initCause

Introduction

In this page you can find the example usage for java.net UnknownHostException initCause.

Prototype

public synchronized Throwable initCause(Throwable cause) 

Source Link

Document

Initializes the cause of this throwable to the specified value.

Usage

From source file:org.magnum.dataup.VideoController.java

private static InetAddress getLocalHostLANAddress() throws UnknownHostException {
    try {//from  ww w  .  j  a v  a2 s.  co  m
        InetAddress candidateAddress = null;
        // Iterate all NICs (network interface cards)...
        for (Enumeration ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements();) {
            NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
            // Iterate all IP addresses assigned to each card...
            for (Enumeration inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
                InetAddress inetAddr = (InetAddress) inetAddrs.nextElement();
                if (!inetAddr.isLoopbackAddress()) {

                    if (inetAddr.isSiteLocalAddress()) {
                        // Found non-loopback site-local address. Return it immediately...
                        return inetAddr;
                    } else if (candidateAddress == null) {
                        // Found non-loopback address, but not necessarily site-local.
                        // Store it as a candidate to be returned if site-local address is not subsequently found...
                        candidateAddress = inetAddr;
                        // Note that we don't repeatedly assign non-loopback non-site-local addresses as candidates,
                        // only the first. For subsequent iterations, candidate will be non-null.
                    }
                }
            }
        }
        if (candidateAddress != null) {
            // We did not find a site-local address, but we found some other non-loopback address.
            // Server might have a non-site-local address assigned to its NIC (or it might be running
            // IPv6 which deprecates the "site-local" concept).
            // Return this non-loopback candidate address...
            return candidateAddress;
        }
        // At this point, we did not find a non-loopback address.
        // Fall back to returning whatever InetAddress.getLocalHost() returns...
        InetAddress jdkSuppliedAddress = InetAddress.getLocalHost();
        if (jdkSuppliedAddress == null) {
            throw new UnknownHostException(
                    "The JDK InetAddress.getLocalHost() method unexpectedly returned null.");
        }
        return jdkSuppliedAddress;
    } catch (Exception e) {
        UnknownHostException unknownHostException = new UnknownHostException(
                "Failed to determine LAN address: " + e);
        unknownHostException.initCause(e);
        throw unknownHostException;
    }
}

From source file:de.pawlidi.openaletheia.utils.AletheiaUtils.java

/**
 * /*from  ww w.  ja  va 2 s .co  m*/
 * @return
 * @throws UnknownHostException
 */
public static InetAddress getLocalIpAddress() throws UnknownHostException {
    try {
        InetAddress localAddress = null;
        // load all existed network interfaces
        for (Enumeration<?> networkInterfaces = NetworkInterface.getNetworkInterfaces(); networkInterfaces
                .hasMoreElements();) {
            NetworkInterface networkInterface = (NetworkInterface) networkInterfaces.nextElement();

            for (Enumeration<?> ipAddresses = networkInterface.getInetAddresses(); ipAddresses
                    .hasMoreElements();) {
                InetAddress ipAddress = (InetAddress) ipAddresses.nextElement();
                if (!ipAddress.isLoopbackAddress()) {
                    if (ipAddress.isSiteLocalAddress()) {
                        return ipAddress;
                    } else if (localAddress == null) {
                        localAddress = ipAddress;
                    }
                }
            }
        }
        if (localAddress != null) {
            return localAddress;
        }
        // try to get localhost address
        localAddress = InetAddress.getLocalHost();
        if (localAddress == null) {
            throw new UnknownHostException("Could not load localhost ip address");
        }
        return localAddress;
    } catch (Exception e) {
        UnknownHostException unknownHostException = new UnknownHostException(
                "Could not load localhost ip address " + e);
        unknownHostException.initCause(e);
        throw unknownHostException;
    }
}