Update UDPDestination to allow specifying the interface to use via a property Also, try to decect a usable interface if not specified
Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/6d4746a7 Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/6d4746a7 Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/6d4746a7 Branch: refs/heads/2.7.x-fixes Commit: 6d4746a7be50765d5b1326fde52833e2b81c34b3 Parents: cf14336 Author: Daniel Kulp <dk...@apache.org> Authored: Mon Oct 20 13:21:25 2014 -0400 Committer: Daniel Kulp <dk...@apache.org> Committed: Tue Oct 21 16:21:43 2014 -0400 ---------------------------------------------------------------------- .../cxf/transport/udp/UDPDestination.java | 37 ++++++++++++++++++++ 1 file changed, 37 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/6d4746a7/rt/transports/udp/src/main/java/org/apache/cxf/transport/udp/UDPDestination.java ---------------------------------------------------------------------- diff --git a/rt/transports/udp/src/main/java/org/apache/cxf/transport/udp/UDPDestination.java b/rt/transports/udp/src/main/java/org/apache/cxf/transport/udp/UDPDestination.java index f06304d..aec65f6 100644 --- a/rt/transports/udp/src/main/java/org/apache/cxf/transport/udp/UDPDestination.java +++ b/rt/transports/udp/src/main/java/org/apache/cxf/transport/udp/UDPDestination.java @@ -25,9 +25,15 @@ import java.io.InputStream; import java.io.OutputStream; import java.net.DatagramPacket; import java.net.InetSocketAddress; +import java.net.InterfaceAddress; import java.net.MulticastSocket; +import java.net.NetworkInterface; +import java.net.SocketException; import java.net.SocketTimeoutException; import java.net.URI; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.List; import java.util.logging.Logger; import org.apache.cxf.Bus; @@ -56,6 +62,8 @@ import org.apache.mina.transport.socket.nio.NioDatagramAcceptor; * */ public class UDPDestination extends AbstractDestination { + public static final String NETWORK_INTERFACE = UDPDestination.class.getName() + ".NETWORK_INTERFACE"; + private static final Logger LOG = LogUtils.getL7dLogger(UDPDestination.class); private static final AttributeKey KEY_IN = new AttributeKey(StreamIoHandler.class, "in"); private static final AttributeKey KEY_OUT = new AttributeKey(StreamIoHandler.class, "out"); @@ -165,6 +173,7 @@ public class UDPDestination extends AbstractDestination { socket.setSendBufferSize(64 * 1024); socket.setTimeToLive(1); socket.bind(new InetSocketAddress(isa.getPort())); + socket.setNetworkInterface(findNetworkInterface()); socket.joinGroup(isa.getAddress()); mcast = socket; queue.execute(new MCastListener()); @@ -185,6 +194,34 @@ public class UDPDestination extends AbstractDestination { throw new RuntimeException(ex); } } + private NetworkInterface findNetworkInterface() throws SocketException { + String name = (String)this.getEndpointInfo().getProperty(UDPDestination.NETWORK_INTERFACE); + NetworkInterface ret = null; + if (!StringUtils.isEmpty(name)) { + ret = NetworkInterface.getByName(name); + } + if (ret == null) { + Enumeration<NetworkInterface> ifcs = NetworkInterface.getNetworkInterfaces(); + List<NetworkInterface> possibles = new ArrayList<NetworkInterface>(); + while (ifcs.hasMoreElements()) { + NetworkInterface ni = ifcs.nextElement(); + if (ni.supportsMulticast() + && ni.isUp()) { + for (InterfaceAddress ia : ni.getInterfaceAddresses()) { + if (ia.getAddress() instanceof java.net.Inet4Address + && !ia.getAddress().isLoopbackAddress() + && !ni.getDisplayName().startsWith("vnic")) { + possibles.add(ni); + } + } + } + } + ret = possibles.isEmpty() ? null : possibles.get(possibles.size() - 1); + + } + return ret; + } + protected void deactivate() { if (acceptor != null) { acceptor.unbind();