Skip to content

Commit

Permalink
Merge pull request #236 from Randgalt/master
Browse files Browse the repository at this point in the history
Update
  • Loading branch information
Randgalt committed Jan 8, 2013
2 parents 8eec911 + 93b2e95 commit b3eeb7b
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Expand Up @@ -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.
Expand Down
@@ -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;
}
Expand Up @@ -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
Expand All @@ -41,6 +42,38 @@ public class ServiceInstanceBuilder<T>
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()
{
}
Expand Down Expand Up @@ -140,10 +173,10 @@ public static Collection<InetAddress> getAllLocalIPs() throws SocketException
// 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);
}
Expand Down
@@ -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);
}
}

0 comments on commit b3eeb7b

Please sign in to comment.