Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Issue 230: Added a filter to control which IP address are returned by…

… ServiceInstanceBuilder.getAllLocalIPs().

Set the filter via ServiceInstanceBuilder.setLocalIpFilter().
  • Loading branch information...
commit 93b2e958e01acffa9fba98980c1b83f91f60f720 1 parent bacec77
@Randgalt Randgalt authored
View
3  CHANGES.txt
@@ -10,6 +10,9 @@ wasn't correct. Due to an unfortunate implementation of ZKPaths.PathAndNode (mea
differently than non-root paths. To work around this, I added a method to EnsurePath - excludingLast() - that
can be used instead of the idiom.
+* Issue 230: Added a filter to control which IP address are returned by ServiceInstanceBuilder.getAllLocalIPs().
+Set the filter via ServiceInstanceBuilder.setLocalIpFilter().
+
1.2.6 - January 1, 2013
=======================
* Issue 214: Added rebuildNode method to PathChildrenCache.
View
10 curator-x-discovery/src/main/java/com/netflix/curator/x/discovery/LocalIpFilter.java
@@ -0,0 +1,10 @@
+package com.netflix.curator.x.discovery;
+
+import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.net.SocketException;
+
+public interface LocalIpFilter
+{
+ public boolean use(NetworkInterface networkInterface, InetAddress address) throws SocketException;
+}
View
37 curator-x-discovery/src/main/java/com/netflix/curator/x/discovery/ServiceInstanceBuilder.java
@@ -25,6 +25,7 @@
import java.util.Collection;
import java.util.Enumeration;
import java.util.List;
+import java.util.concurrent.atomic.AtomicReference;
/**
* Builder for service instances
@@ -41,6 +42,38 @@
private ServiceType serviceType = ServiceType.DYNAMIC;
private UriSpec uriSpec;
+ private static final AtomicReference<LocalIpFilter> localIpFilter = new AtomicReference<LocalIpFilter>
+ (
+ new LocalIpFilter()
+ {
+ @Override
+ public boolean use(NetworkInterface nif, InetAddress adr) throws SocketException
+ {
+ return (adr != null) && !adr.isLoopbackAddress() && (nif.isPointToPoint() || !adr.isLinkLocalAddress());
+ }
+ }
+ );
+
+ /**
+ * Replace the default local ip filter used by {@link #getAllLocalIPs()}
+ *
+ * @param newLocalIpFilter the new local ip filter
+ */
+ public static void setLocalIpFilter(LocalIpFilter newLocalIpFilter)
+ {
+ localIpFilter.set(newLocalIpFilter);
+ }
+
+ /**
+ * Return the current local ip filter used by {@link #getAllLocalIPs()}
+ *
+ * @return ip filter
+ */
+ public static LocalIpFilter getLocalIpFilter()
+ {
+ return localIpFilter.get();
+ }
+
ServiceInstanceBuilder()
{
}
@@ -140,10 +173,10 @@
// We ignore subinterfaces - as not yet needed.
Enumeration<InetAddress> adrs = nif.getInetAddresses();
- while (adrs.hasMoreElements())
+ while ( adrs.hasMoreElements() )
{
InetAddress adr = adrs.nextElement();
- if (adr != null && !adr.isLoopbackAddress() && (nif.isPointToPoint() || !adr.isLinkLocalAddress()))
+ if ( localIpFilter.get().use(nif, adr) )
{
listAdr.add(adr);
}
View
42 curator-x-discovery/src/test/java/com/netflix/curator/x/discovery/TestLocalIpFilter.java
@@ -0,0 +1,42 @@
+package com.netflix.curator.x.discovery;
+
+import com.google.common.collect.Lists;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.net.SocketException;
+import java.util.List;
+
+public class TestLocalIpFilter
+{
+ @Test
+ public void testFilterEverything() throws SocketException
+ {
+ LocalIpFilter localIpFilter = ServiceInstanceBuilder.getLocalIpFilter();
+ try
+ {
+ ServiceInstanceBuilder.setLocalIpFilter
+ (
+ new LocalIpFilter()
+ {
+ @Override
+ public boolean use(NetworkInterface networkInterface, InetAddress address) throws SocketException
+ {
+ return false;
+ }
+ }
+ );
+
+ List<InetAddress> allLocalIPs = Lists.newArrayList(ServiceInstanceBuilder.getAllLocalIPs());
+ Assert.assertEquals(allLocalIPs.size(), 0);
+ }
+ finally
+ {
+ ServiceInstanceBuilder.setLocalIpFilter(localIpFilter);
+ }
+
+ List<InetAddress> allLocalIPs = Lists.newArrayList(ServiceInstanceBuilder.getAllLocalIPs());
+ Assert.assertTrue(allLocalIPs.size() > 0);
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.