/** * @param addr The {@link InetAddress} to be considered * @return <code>true</code> if the address is a loopback one. * <B>Note:</B> if {@link InetAddress#isLoopback()} * returns <code>false</code> the address <U>string</U> is checked * @see #toAddressString(InetAddress) * @see #isLoopback(String) */ public static boolean isLoopback (InetAddress addr) { if (addr == null) { return false; } if (addr.isLoopbackAddress()) { return true; } String ip=toAddressString(addr); return isLoopback(ip); }
public InetAddress getFallbackAddress() { InetAddress addr = null; // Trapped in guess work if (addr == null) { addr = NetworkAddressUtil.getFirstExternalNetwork4Address(); } // Giving up and using localhost. if (addr == null) { addr = NetworkAddressUtil.getDefaultNetworkAddress(); } return addr; }
public InetAddress getConnectingIp() { // Try a target system, because it is more accurate, and it does not require full connectivity if ((!StringUtil.isEmpty(testHost)) && (testPort > 0)) { return NetworkAddressUtil.getExternalAddressThroughTCPConnect(testHost, testPort); } return null; } }
public static String createServerLabel(ServerName serverName, String fqdn, String ip, String pid) { final String label; if (StringUtil.isEmpty(fqdn)) { // if no properties are available, default to server name label = StringUtil.isEmpty(ip) ? serverName.getName() : ip; } else if (NetworkAddressUtil.isLoopback(fqdn)) { // if no IP used use the FQDN even if it is a loopback label = StringUtil.isEmpty(ip) ? fqdn : ip; } else { label = fqdn; } if (StringUtil.isEmpty(pid)) { return label; } else { return label + "-" + pid; } } }
/** * Extract the MAC address of an IP and write it in the following format: 12-34-AA-BB-CC-DD * @param ip IP address - ignored if <code>null</code>/empty * @return The formatted MAC address - <code>null</code> if unable to extract * it - e.g., {@link NetworkInterface} <code>getHardwareAddress</code> was * was introduced in JDK 1.6, so if we are running on a JVM below that * version, then we cannot extract the MAC address (though this is <U>not</U> * the only reason why a MAC might not be available) */ public static String getMac(String ip) { // since NetworkInterface.getHardwareAddress was introduced in JDK 1.6, we need to call it by reflection if ((macAddressMethod == null) || StringUtil.isEmpty(ip)) { return null; } try { NetworkInterface nif=NetworkInterface.getByInetAddress(InetAddress.getByName(ip)); byte[] mac=getMac(nif); return getMac(mac); } catch (Exception ex) { return null; } }
/** * Returns the first external network address assigned to this * machine or null if one is not found. * @return Inet4Address associated with an external interface * DevNote: We actually return InetAddress here, as Inet4Addresses are final and cannot be mocked. */ public static InetAddress getFirstExternalNetwork4Address() { List<? extends InetAddress> addresses = getExternalNetwork4Addresses(); if (ListUtil.size(addresses) > 0) return addresses.get(0); else return null; }
/** * Returns a list of local network addresses which are not multicast or localhost */ public static List<InetAddress> getExternalNetwork4Addresses() { List<InetAddress> addresses = new ArrayList<InetAddress>(); try { for (Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces(); (nets != null) && nets.hasMoreElements(); ) { NetworkInterface netint=nets.nextElement(); /* TODO - uncomment when 1.5 compatibility no longer required if (!netint.isUp()) { continue; // ignore non-running interfaces } */ for (Enumeration<InetAddress> inetAddresses = netint.getInetAddresses(); (inetAddresses != null) && inetAddresses.hasMoreElements(); ) { InetAddress inetAddress=inetAddresses.nextElement(); if (isValidHostAddress(inetAddress)) { addresses.add(inetAddress); } } } } catch (SocketException e) { // swallow } return addresses; } /**
}; for (String ip : candidates) { if ((!StringUtil.isEmpty(ip)) && (!NetworkAddressUtil.isLoopback(ip))) { return ip;
public Map<String, String> getProperties(Map<String, String> existingProperties) { Map<String, String> props = new HashMap<String, String>(); String preferredIp = getPreferredIp(existingProperties); props.put(getMappedPropertyKey(NETWORK_IP_KEY), preferredIp); String macAddress = NetworkAddressUtil.getMac(preferredIp); if (!StringUtil.isEmpty(macAddress)) { props.put(getMappedPropertyKey(NETWORK_MAC_KEY), macAddress); } return props; }
public String getProperty(String name) { if (StringUtil.isEmpty(name)) { throw new IllegalArgumentException("No property name"); } if (NETWORK_CONNECTING_IP_KEY.equals(name)) { InetAddress conAddr = getConnectingIp(); return NetworkAddressUtil.toAddressString(conAddr); } else if (NETWORK_FALLBACK_IP_KEY.equals(name)) { InetAddress addr = getFallbackAddress(); return NetworkAddressUtil.toAddressString(addr); } else if (NETWORK_NAME_KEY.equals(name)) { InetAddress addr = getConnectingIp(); if ((addr == null) || NetworkAddressUtil.isLoopback(addr)) { InetAddress fallback=getFallbackAddress(); if (fallback != null) { // if no fallback, then use the address even if it is a loopback addr = fallback; } } if (addr != null) { return addr.getHostName(); } } return null; }