Browse Source

Return address from the interface with lowest index

Also fixes unclosed closeable warnings

Fixes gh-82
pull/84/merge
Dave Syer 9 years ago
parent
commit
7665f48653
  1. 14
      spring-cloud-commons/src/main/java/org/springframework/cloud/util/InetUtils.java
  2. 40
      spring-cloud-commons/src/test/java/org/springframework/cloud/util/InetUtilsTests.java

14
spring-cloud-commons/src/main/java/org/springframework/cloud/util/InetUtils.java

@ -77,12 +77,20 @@ public class InetUtils implements Closeable { @@ -77,12 +77,20 @@ public class InetUtils implements Closeable {
}
public InetAddress findFirstNonLoopbackAddress() {
InetAddress result = null;
try {
int lowest = Integer.MAX_VALUE;
for (Enumeration<NetworkInterface> nics = NetworkInterface
.getNetworkInterfaces(); nics.hasMoreElements();) {
NetworkInterface ifc = nics.nextElement();
if (ifc.isUp()) {
log.trace("Testing interface: " + ifc.getDisplayName());
if (ifc.getIndex() < lowest || result == null) {
lowest = ifc.getIndex();
}
else if (result != null) {
continue;
}
// @formatter:off
if (!ignoreInterface(ifc.getDisplayName())) {
@ -93,7 +101,7 @@ public class InetUtils implements Closeable { @@ -93,7 +101,7 @@ public class InetUtils implements Closeable {
&& !address.isLoopbackAddress()) {
log.trace("Found non-loopback interface: "
+ ifc.getDisplayName());
return address;
result = address;
}
}
}
@ -105,6 +113,10 @@ public class InetUtils implements Closeable { @@ -105,6 +113,10 @@ public class InetUtils implements Closeable {
log.error("Cannot get first non-loopback address", ex);
}
if (result != null) {
return result;
}
try {
return InetAddress.getLocalHost();
}

40
spring-cloud-commons/src/test/java/org/springframework/cloud/util/InetUtilsTests.java

@ -34,28 +34,32 @@ public class InetUtilsTests { @@ -34,28 +34,32 @@ public class InetUtilsTests {
@Test
public void testGetFirstNonLoopbackHostInfo() {
assertNotNull(
new InetUtils(new InetUtilsProperties()).findFirstNonLoopbackHostInfo());
try (InetUtils utils = new InetUtils(new InetUtilsProperties())) {
assertNotNull(utils.findFirstNonLoopbackHostInfo());
}
}
@Test
public void testGetFirstNonLoopbackAddress() {
assertNotNull(
new InetUtils(new InetUtilsProperties()).findFirstNonLoopbackAddress());
try (InetUtils utils = new InetUtils(new InetUtilsProperties())) {
assertNotNull(utils.findFirstNonLoopbackAddress());
}
}
@Test
public void testConvert() throws Exception {
assertNotNull(new InetUtils(new InetUtilsProperties())
.convertAddress(InetAddress.getByName("localhost")));
try (InetUtils utils = new InetUtils(new InetUtilsProperties())) {
assertNotNull(utils.convertAddress(InetAddress.getByName("localhost")));
}
assertNotNull(InetUtils.convert(InetAddress.getByName("localhost")));
}
@Test
public void testHostInfo() throws Exception {
HostInfo info = new InetUtils(new InetUtilsProperties())
.findFirstNonLoopbackHostInfo();
assertNotNull(info.getIpAddressAsInt());
try (InetUtils utils = new InetUtils(new InetUtilsProperties())) {
HostInfo info = utils.findFirstNonLoopbackHostInfo();
assertNotNull(info.getIpAddressAsInt());
}
}
@Test
@ -66,14 +70,20 @@ public class InetUtilsTests { @@ -66,14 +70,20 @@ public class InetUtilsTests {
// interface.
// https://docs.docker.com/v1.7/articles/networking/
properties.setIgnoredInterfaces(Arrays.asList("docker0", "veth.*"));
InetUtils inetUtils = new InetUtils(properties);
try (InetUtils inetUtils = new InetUtils(properties)) {
assertTrue("docker0 not ignored", inetUtils.ignoreInterface("docker0"));
assertTrue("vethAQI2QT0 not ignored", inetUtils.ignoreInterface("vethAQI2QT"));
assertFalse("docker1 ignored", inetUtils.ignoreInterface("docker1"));
assertTrue("docker0 not ignored", inetUtils.ignoreInterface("docker0"));
assertTrue("vethAQI2QT0 not ignored",
inetUtils.ignoreInterface("vethAQI2QT"));
assertFalse("docker1 ignored", inetUtils.ignoreInterface("docker1"));
}
}
assertFalse("docker0 ignored",
new InetUtils(new InetUtilsProperties()).ignoreInterface("docker0"));
@Test
public void testDefaultIgnoreInterface() {
try (InetUtils inetUtils = new InetUtils(new InetUtilsProperties())) {
assertFalse("docker0 ignored", inetUtils.ignoreInterface("docker0"));
}
}
}

Loading…
Cancel
Save