當前位置: 首頁>>代碼示例>>Java>>正文


Java UnknownHostException.initCause方法代碼示例

本文整理匯總了Java中java.net.UnknownHostException.initCause方法的典型用法代碼示例。如果您正苦於以下問題:Java UnknownHostException.initCause方法的具體用法?Java UnknownHostException.initCause怎麽用?Java UnknownHostException.initCause使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.net.UnknownHostException的用法示例。


在下文中一共展示了UnknownHostException.initCause方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: lookup

import java.net.UnknownHostException; //導入方法依賴的package包/類
@Override public List<InetAddress> lookup(String hostname) throws UnknownHostException {
  if (hostname == null) throw new UnknownHostException("hostname == null");
  try {
    return Arrays.asList(InetAddress.getAllByName(hostname));
  } catch (NullPointerException e) {
    UnknownHostException unknownHostException =
        new UnknownHostException("Broken system behaviour for dns lookup of " + hostname);
    unknownHostException.initCause(e);
    throw unknownHostException;
  }
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:12,代碼來源:Dns.java

示例2: getLocalHostLANAddress

import java.net.UnknownHostException; //導入方法依賴的package包/類
/**
 * Returns an <code>InetAddress</code> object encapsulating what is most likely the machine's LAN IP address.
 * <p/>
 * This method is intended for use as a replacement of JDK method <code>InetAddress.getLocalHost</code>, because
 * that method is ambiguous on Linux systems. Linux systems enumerate the loopback network interface the same
 * way as regular LAN network interfaces, but the JDK <code>InetAddress.getLocalHost</code> method does not
 * specify the algorithm used to select the address returned under such circumstances, and will often return the
 * loopback address, which is not valid for network communication. Details
 * <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4665037">here</a>.
 * <p/>
 * This method will scan all IP addresses on all network interfaces on the host machine to determine the IP address
 * most likely to be the machine's LAN address. If the machine has multiple IP addresses, this method will prefer
 * a site-local IP address (e.g. 192.168.x.x or 10.10.x.x, usually IPv4) if the machine has one (and will return the
 * first site-local address if the machine has more than one), but if the machine does not hold a site-local
 * address, this method will return simply the first non-loopback address found (IPv4 or IPv6).
 * <p/>
 * If this method cannot find a non-loopback address using this selection algorithm, it will fall back to
 * calling and returning the result of JDK method <code>InetAddress.getLocalHost</code>.
 * <p/>
 *
 * @throws UnknownHostException If the LAN address of the machine cannot be found.
 *
 * Thanks to https://issues.apache.org/jira/browse/JCS-40 for this code
 */
private static InetAddress getLocalHostLANAddress() throws UnknownHostException {
    try {
        InetAddress candidateAddress = null;
        // Iterate all NICs (network interface cards)...
        for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements();) {
            NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
            // Iterate all IP addresses assigned to each card...
            for (Enumeration<InetAddress> 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;
    }
}
 
開發者ID:ajmath,項目名稱:VASSAL-src,代碼行數:71,代碼來源:ServerAddressBook.java

示例3: search

import java.net.UnknownHostException; //導入方法依賴的package包/類
/**
 * Returns an <code>InetAddress</code> object encapsulating what is most likely the machine's LAN IP address.
 * <p/>
 * This method is intended for use as a replacement of JDK method <code>InetAddress.getLocalHost</code>, because that method is ambiguous on Linux systems. Linux systems enumerate the loopback network interface the same way as regular LAN network
 * interfaces, but the JDK <code>InetAddress.getLocalHost</code> method does not specify the algorithm used to select the address returned under such circumstances, and will often return the loopback address, which is not valid for network
 * communication. Details <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4665037">here</a>.
 * <p/>
 * This method will scan all IP addresses on all network interfaces on the host machine to determine the IP address most likely to be the machine's LAN address. If the machine has multiple IP addresses, this method will prefer a site-local IP
 * address (e.g. 192.168.x.x or 10.10.x.x, usually IPv4) if the machine has one (and will return the first site-local address if the machine has more than one), but if the machine does not hold a site-local address, this method will return simply
 * the first non-loopback address found (IPv4 or IPv6).
 * <p/>
 * If this method cannot find a non-loopback address using this selection algorithm, it will fall back to calling and returning the result of JDK method <code>InetAddress.getLocalHost</code>.
 * <p/>
 *
 * @throws UnknownHostException
 *             If the LAN address of the machine cannot be found.
 */
public static InetAddress search() throws UnknownHostException {
	try {
		InetAddress candidateAddress = null;
		// Iterate all NICs (network interface cards)...
		for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements();) {
			NetworkInterface iface = ifaces.nextElement();
			AitLogger.trace(logger, "Analizando interfaz de red: {}", iface.getName());
			// Iterate all IP addresses assigned to each card...
			for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
				InetAddress inetAddr = inetAddrs.nextElement();
				AitLogger.trace(logger, "InetAddr {}: loopback: {}", inetAddr.getHostAddress(), inetAddr.isLoopbackAddress());
				if (!inetAddr.isLoopbackAddress()) {
					AitLogger.trace(logger, "InetAddr {}: siteLocal: {}", inetAddr.getHostAddress(), inetAddr.isSiteLocalAddress());
					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();
		AitLogger.trace(logger, "Sin candidatos, retornando metodo normal: {}", jdkSuppliedAddress);
		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;
	}
}
 
開發者ID:allianzit,項目名稱:ait-platform,代碼行數:65,代碼來源:AitLocalhostIpAddress.java


注:本文中的java.net.UnknownHostException.initCause方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。