handle failure on interface check if its up (seems to happen on jdk 7)

This commit is contained in:
kimchy 2011-07-08 21:58:39 +03:00
parent 61ad8b614a
commit 9464208f83
1 changed files with 22 additions and 5 deletions

View File

@ -26,8 +26,20 @@ import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.os.OsUtils; import org.elasticsearch.common.os.OsUtils;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.*; import java.net.Inet4Address;
import java.util.*; import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/** /**
* @author kimchy (shay.banon) * @author kimchy (shay.banon)
@ -123,8 +135,13 @@ public abstract class NetworkUtils {
} }
for (NetworkInterface intf : intfsList) { for (NetworkInterface intf : intfsList) {
if (!intf.isUp() || intf.isLoopback()) try {
if (!intf.isUp() || intf.isLoopback())
continue;
} catch (Exception e) {
// might happen when calling on a network interface that does not exists
continue; continue;
}
address = getFirstNonLoopbackAddress(intf, ip_version); address = getFirstNonLoopbackAddress(intf, ip_version);
if (address != null) { if (address != null) {
return address; return address;
@ -145,7 +162,7 @@ public abstract class NetworkUtils {
if (intf == null) if (intf == null)
throw new IllegalArgumentException("Network interface pointer is null"); throw new IllegalArgumentException("Network interface pointer is null");
for (Enumeration addresses = intf.getInetAddresses(); addresses.hasMoreElements();) { for (Enumeration addresses = intf.getInetAddresses(); addresses.hasMoreElements(); ) {
InetAddress address = (InetAddress) addresses.nextElement(); InetAddress address = (InetAddress) addresses.nextElement();
if (!address.isLoopbackAddress()) { if (!address.isLoopbackAddress()) {
if ((address instanceof Inet4Address && ipVersion == StackType.IPv4) || if ((address instanceof Inet4Address && ipVersion == StackType.IPv4) ||
@ -229,7 +246,7 @@ public abstract class NetworkUtils {
public static List<NetworkInterface> getAllAvailableInterfaces() throws SocketException { public static List<NetworkInterface> getAllAvailableInterfaces() throws SocketException {
List<NetworkInterface> allInterfaces = new ArrayList<NetworkInterface>(10); List<NetworkInterface> allInterfaces = new ArrayList<NetworkInterface>(10);
NetworkInterface intf; NetworkInterface intf;
for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
intf = (NetworkInterface) en.nextElement(); intf = (NetworkInterface) en.nextElement();
allInterfaces.add(intf); allInterfaces.add(intf);
} }