You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by dk...@apache.org on 2014/10/21 22:17:26 UTC

[2/3] git commit: Update UDPDestination to allow specifying the interface to use via a property Also, try to decect a usable interface if not specified

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/fc59fb85
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/fc59fb85
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/fc59fb85

Branch: refs/heads/3.0.x-fixes
Commit: fc59fb85d895d8a21fdcf24b4c95a1fe830f3bdb
Parents: 1c87694
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:16:12 2014 -0400

----------------------------------------------------------------------
 .../cxf/transport/udp/UDPDestination.java       | 37 ++++++++++++++++++++
 1 file changed, 37 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/fc59fb85/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();