List of usage examples for java.net InetAddress isLoopbackAddress
public boolean isLoopbackAddress()
From source file:org.apache.htrace.core.TracerId.java
/** * <p>Get the best IP address that represents this node.</p> * * This is complicated since nodes can have multiple network interfaces, * and each network interface can have multiple IP addresses. What we're * looking for here is an IP address that will serve to identify this node * to HTrace. So we prefer site-local addresess (i.e. private ones on the * LAN) to publicly routable interfaces. If there are multiple addresses * to choose from, we select the one which comes first in textual sort * order. This should ensure that we at least consistently call each node * by a single name./*from w w w.j ava 2 s .c o m*/ */ static String getBestIpString() { Enumeration<NetworkInterface> ifaces; try { ifaces = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { LOG.error("Error getting network interfaces", e); return "127.0.0.1"; } TreeSet<String> siteLocalCandidates = new TreeSet<String>(); TreeSet<String> candidates = new TreeSet<String>(); while (ifaces.hasMoreElements()) { NetworkInterface iface = ifaces.nextElement(); for (Enumeration<InetAddress> addrs = iface.getInetAddresses(); addrs.hasMoreElements();) { InetAddress addr = addrs.nextElement(); if (!addr.isLoopbackAddress()) { if (addr.isSiteLocalAddress()) { siteLocalCandidates.add(addr.getHostAddress()); } else { candidates.add(addr.getHostAddress()); } } } } if (!siteLocalCandidates.isEmpty()) { return siteLocalCandidates.first(); } if (!candidates.isEmpty()) { return candidates.first(); } return "127.0.0.1"; }
From source file:org.magnum.dataup.VideoController.java
private static InetAddress getLocalHostLANAddress() throws UnknownHostException { try {//from w ww .j a va 2 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:net.centro.rtb.monitoringcenter.infos.NodeInfo.java
private static String detectPublicIpAddress() { Enumeration<NetworkInterface> networkInterfaces = null; try {/* w w w .ja va 2 s . c o m*/ networkInterfaces = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { logger.debug("Unable to obtain network interfaces!", e); return null; } for (NetworkInterface networkInterface : Collections.list(networkInterfaces)) { boolean isLoopback = false; try { isLoopback = networkInterface.isLoopback(); } catch (SocketException e) { logger.debug("Unable to identify if a network interface is a loopback or not"); } if (!isLoopback) { Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses(); while (inetAddresses.hasMoreElements()) { InetAddress inetAddress = inetAddresses.nextElement(); if (Inet4Address.class.isInstance(inetAddress)) { if (!inetAddress.isLoopbackAddress() && !inetAddress.isAnyLocalAddress() && !inetAddress.isLinkLocalAddress() && !inetAddress.isSiteLocalAddress()) { return inetAddress.getHostAddress(); } } } } } return null; }
From source file:org.apache.hadoop.mapreduce.v2.MiniMRYarnCluster.java
public static String getResolvedMRHistoryWebAppURLWithoutScheme(Configuration conf, boolean isSSLEnabled) { InetSocketAddress address = null; if (isSSLEnabled) { address = conf.getSocketAddr(JHAdminConfig.MR_HISTORY_WEBAPP_HTTPS_ADDRESS, JHAdminConfig.DEFAULT_MR_HISTORY_WEBAPP_HTTPS_ADDRESS, JHAdminConfig.DEFAULT_MR_HISTORY_WEBAPP_HTTPS_PORT); } else {/*from w ww . ja v a2 s. c o m*/ address = conf.getSocketAddr(JHAdminConfig.MR_HISTORY_WEBAPP_ADDRESS, JHAdminConfig.DEFAULT_MR_HISTORY_WEBAPP_ADDRESS, JHAdminConfig.DEFAULT_MR_HISTORY_WEBAPP_PORT); } address = NetUtils.getConnectAddress(address); StringBuffer sb = new StringBuffer(); InetAddress resolved = address.getAddress(); if (resolved == null || resolved.isAnyLocalAddress() || resolved.isLoopbackAddress()) { String lh = address.getHostName(); try { lh = InetAddress.getLocalHost().getCanonicalHostName(); } catch (UnknownHostException e) { //Ignore and fallback. } sb.append(lh); } else { sb.append(address.getHostName()); } sb.append(":").append(address.getPort()); return sb.toString(); }
From source file:org.jumpmind.util.AppUtils.java
public static String getIpAddress() { String ipAddress = System.getProperty(SYSPROP_IP_ADDRESS, UNKNOWN); if (UNKNOWN.equals(ipAddress)) { try {//from w w w. j a va 2 s .c o m Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface networkInterface = interfaces.nextElement(); Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses(); while (inetAddresses.hasMoreElements()) { InetAddress inetAddress = inetAddresses.nextElement(); if (!inetAddress.isLoopbackAddress()) { ipAddress = inetAddress.getHostAddress(); } } } } catch (Exception ex) { log.warn("", ex); } finally { } } if (UNKNOWN.equals(ipAddress)) { try { ipAddress = InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException ex) { log.warn("", ex); ipAddress = "127.0.0.1"; } } return ipAddress; }
From source file:de.pawlidi.openaletheia.utils.AletheiaUtils.java
/** * /*from ww w . j av a 2 s .com*/ * @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; } }
From source file:org.wso2.javaagent.JDBCAgentPublisher.java
public static InetAddress getLocalAddress() throws SocketException, UnknownHostException { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface iface = interfaces.nextElement(); Enumeration<InetAddress> addresses = iface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); if (addr instanceof Inet4Address && !addr.isLoopbackAddress()) { return addr; }//w w w. j a v a 2s . c o m } } return InetAddress.getLocalHost(); }
From source file:de.flapdoodle.embed.redis.RedisDProcess.java
public static boolean shutdownRedis(AbstractRedisConfig config) { try {/*from ww w . j a v a 2s. c o m*/ // ensure that we don't get into a stackoverflow when starting // the artifact fails entirely if (config.isNested()) { logger.log(Level.INFO, "Nested stop, won't execute redis process again"); return false; } InetAddress host = config.net().getServerAddress(); int port = config.net().getPort(); if (!host.isLoopbackAddress()) { logger.log(Level.WARNING, "" + "---------------------------------------\n" + "Your localhost (" + host.getHostAddress() + ") is not a loopback adress\n" + "We can NOT send shutdown to redis, because it is denied from remote." + "---------------------------------------\n"); return false; } try { Jedis j = new Jedis(host.getHostName(), port); String reply = j.shutdown(); if (StringUtils.isEmpty(reply)) { return true; } else { logger.log(Level.SEVERE, String .format("sendShutdown closing %s:%s; Got response from server %s", host, port, reply)); return false; } } catch (JedisConnectionException e) { logger.log(Level.WARNING, String.format("sendShutdown closing %s:%s. No Service listening on address.\n%s", host, port, e.getMessage())); return true; } } catch (UnknownHostException e) { logger.log(Level.SEVERE, "sendStop", e); } return false; }
From source file:NetworkUtil.java
/** * @return the current environment's IP address, taking into account the Internet connection to any of the available * machine's Network interfaces. Examples of the outputs can be in octats or in IPV6 format. * <pre>/*from w w w . j a va 2 s . com*/ * ==> wlan0 * * fec0:0:0:9:213:e8ff:fef1:b717%4 * siteLocal: true * isLoopback: false isIPV6: true * 130.212.150.216 <<<<<<<<<<<------------- This is the one we want to grab so that we can. * siteLocal: false address the DSP on the network. * isLoopback: false * isIPV6: false * * ==> lo * 0:0:0:0:0:0:0:1%1 * siteLocal: false * isLoopback: true * isIPV6: true * 127.0.0.1 * siteLocal: false * isLoopback: true * isIPV6: false * </pre> */ public static String getCurrentEnvironmentNetworkIp() { if (currentHostIpAddress == null) { Enumeration<NetworkInterface> netInterfaces = null; try { netInterfaces = NetworkInterface.getNetworkInterfaces(); while (netInterfaces.hasMoreElements()) { NetworkInterface ni = netInterfaces.nextElement(); Enumeration<InetAddress> address = ni.getInetAddresses(); while (address.hasMoreElements()) { InetAddress addr = address.nextElement(); // log.debug("Inetaddress:" + addr.getHostAddress() + " loop? " + addr.isLoopbackAddress() + " local? " // + addr.isSiteLocalAddress()); if (!addr.isLoopbackAddress() && addr.isSiteLocalAddress() && !(addr.getHostAddress().indexOf(":") > -1)) { currentHostIpAddress = addr.getHostAddress(); } } } if (currentHostIpAddress == null) { currentHostIpAddress = "127.0.0.1"; } } catch (SocketException e) { // log.error("Somehow we have a socket error acquiring the host IP... Using loopback instead..."); currentHostIpAddress = "127.0.0.1"; } } return currentHostIpAddress; }
From source file:com.beetle.framework.util.OtherUtil.java
/** * ?ip??char=2//from w ww . ja v a2 s. c o m * * @return */ public static String getLocalHostIps() { StringBuffer sb = new StringBuffer(); final char flag = 2; try { Enumeration<?> netInterfaces = NetworkInterface.getNetworkInterfaces(); while (netInterfaces.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement(); Enumeration<InetAddress> ips = ni.getInetAddresses(); while (ips.hasMoreElements()) { InetAddress inetAddress = ips.nextElement(); String ip = inetAddress.getHostAddress(); if (!inetAddress.isLoopbackAddress() && ip.indexOf(":") == -1) { sb.append(ip).append(flag); } } } } catch (Exception e) { return ""; } return sb.toString(); }