You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by ro...@apache.org on 2019/01/28 16:24:11 UTC

[qpid-jms] branch master updated: QPIDJMS-442: transport options to configure local address and/or port used in connect

This is an automated email from the ASF dual-hosted git repository.

robbie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/qpid-jms.git


The following commit(s) were added to refs/heads/master by this push:
     new 0597ab6  QPIDJMS-442: transport options to configure local address and/or port used in connect
0597ab6 is described below

commit 0597ab62f79618cf1339e165e61d84ae7e55f056
Author: Robbie Gemmell <ro...@apache.org>
AuthorDate: Mon Jan 28 16:18:14 2019 +0000

    QPIDJMS-442: transport options to configure local address and/or port used in connect
    
    Based on PR from rahul.b.uk@gmail.com, but with changes and more tests from me. This closes #27.
---
 .../qpid/jms/transports/TransportOptions.java      | 20 +++++++++++++++++
 .../jms/transports/netty/NettyTcpTransport.java    |  8 +++++++
 .../jms/integration/ConnectionIntegrationTest.java | 26 ++++++++++++++++++++++
 .../qpid/jms/transports/TransportOptionsTest.java  |  7 ++++++
 .../netty/NettySslTransportFactoryTest.java        |  9 ++++++++
 .../netty/NettyTcpTransportFactoryTest.java        |  9 +++++++-
 6 files changed, 78 insertions(+), 1 deletion(-)

diff --git a/qpid-jms-client/src/main/java/org/apache/qpid/jms/transports/TransportOptions.java b/qpid-jms-client/src/main/java/org/apache/qpid/jms/transports/TransportOptions.java
index 23cdf57..2435151 100644
--- a/qpid-jms-client/src/main/java/org/apache/qpid/jms/transports/TransportOptions.java
+++ b/qpid-jms-client/src/main/java/org/apache/qpid/jms/transports/TransportOptions.java
@@ -48,6 +48,7 @@ public class TransportOptions implements Cloneable {
     public static final List<String> DEFAULT_DISABLED_PROTOCOLS = Collections.unmodifiableList(Arrays.asList(new String[]{"SSLv2Hello", "SSLv3"}));
     public static final int DEFAULT_SSL_PORT = 5671;
     public static final boolean DEFAULT_USE_OPENSSL = false;
+    public static final int DEFAULT_LOCAL_PORT = 0;
 
     private static final String JAVAX_NET_SSL_KEY_STORE = "javax.net.ssl.keyStore";
     private static final String JAVAX_NET_SSL_KEY_STORE_TYPE = "javax.net.ssl.keyStoreType";
@@ -65,6 +66,8 @@ public class TransportOptions implements Cloneable {
     private boolean tcpKeepAlive = DEFAULT_TCP_KEEP_ALIVE;
     private boolean tcpNoDelay = DEFAULT_TCP_NO_DELAY;
     private int defaultTcpPort = DEFAULT_TCP_PORT;
+    private String localAddress;
+    private int localPort = DEFAULT_LOCAL_PORT;
     private boolean useEpoll = DEFAULT_USE_EPOLL;
     private boolean useKQueue = DEFAULT_USE_KQUEUE;
     private boolean traceBytes = DEFAULT_TRACE_BYTES;
@@ -224,6 +227,21 @@ public class TransportOptions implements Cloneable {
         this.defaultTcpPort = defaultTcpPort;
     }
 
+    public String getLocalAddress() {
+        return localAddress;
+    }
+
+    public void setLocalAddress(String localAddress) {
+        this.localAddress = localAddress;
+    }
+
+    public int getLocalPort() {
+        return localPort;
+    }
+
+    public void setLocalPort(int localPort) {
+        this.localPort = localPort;
+    }
 
     /**
      * @return true if the netty epoll transport can be used if available on this platform.
@@ -558,6 +576,8 @@ public class TransportOptions implements Cloneable {
         copy.setDefaultSslPort(getDefaultSslPort());
         copy.setSslContextOverride(getSslContextOverride());
         copy.setUseOpenSSL(isUseOpenSSL());
+        copy.setLocalAddress(getLocalAddress());
+        copy.setLocalPort(getLocalPort());
 
         return copy;
     }
diff --git a/qpid-jms-client/src/main/java/org/apache/qpid/jms/transports/netty/NettyTcpTransport.java b/qpid-jms-client/src/main/java/org/apache/qpid/jms/transports/netty/NettyTcpTransport.java
index 1cf9ead..ce19cc0 100644
--- a/qpid-jms-client/src/main/java/org/apache/qpid/jms/transports/netty/NettyTcpTransport.java
+++ b/qpid-jms-client/src/main/java/org/apache/qpid/jms/transports/netty/NettyTcpTransport.java
@@ -461,6 +461,14 @@ public class NettyTcpTransport implements Transport {
         if (options.getTrafficClass() != -1) {
             bootstrap.option(ChannelOption.IP_TOS, options.getTrafficClass());
         }
+
+        if (options.getLocalAddress() != null || options.getLocalPort() != 0) {
+            if(options.getLocalAddress() != null) {
+                bootstrap.localAddress(options.getLocalAddress(), options.getLocalPort());
+            } else {
+                bootstrap.localAddress(options.getLocalPort());
+            }
+        }
     }
 
     private void configureChannel(final Channel channel) throws Exception {
diff --git a/qpid-jms-client/src/test/java/org/apache/qpid/jms/integration/ConnectionIntegrationTest.java b/qpid-jms-client/src/test/java/org/apache/qpid/jms/integration/ConnectionIntegrationTest.java
index 3293dfd..272dcd1 100644
--- a/qpid-jms-client/src/test/java/org/apache/qpid/jms/integration/ConnectionIntegrationTest.java
+++ b/qpid-jms-client/src/test/java/org/apache/qpid/jms/integration/ConnectionIntegrationTest.java
@@ -81,6 +81,7 @@ import org.apache.qpid.proton.amqp.UnsignedInteger;
 import org.apache.qpid.proton.amqp.transaction.TxnCapability;
 import org.apache.qpid.proton.engine.impl.AmqpHeader;
 import org.hamcrest.Matcher;
+import org.junit.Ignore;
 import org.junit.Test;
 
 public class ConnectionIntegrationTest extends QpidJmsTestCase {
@@ -864,4 +865,29 @@ public class ConnectionIntegrationTest extends QpidJmsTestCase {
             testPeer.waitForAllHandlersToComplete(2000);
         }
     }
+
+    @Ignore("Disabled due to requirement of hard coded port")
+    @Test(timeout = 20000)
+    public void testLocalPortOption() throws Exception {
+        try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
+            testPeer.expectSaslAnonymous();
+            testPeer.expectOpen();
+            // Each connection creates a session for managing temporary destinations etc
+            testPeer.expectBegin();
+
+            int localPort = 5671;
+            String uri = "amqp://localhost:" + testPeer.getServerPort() + "?transport.localPort=" + localPort;
+            ConnectionFactory factory = new JmsConnectionFactory(uri);
+            Connection connection = factory.createConnection();
+            connection.start();
+
+            testPeer.waitForAllHandlersToComplete(2000);
+
+            testPeer.expectClose();
+            connection.close();
+
+            int clientPort = testPeer.getClientSocket().getPort();
+            assertEquals(localPort, clientPort);
+        }
+    }
 }
diff --git a/qpid-jms-client/src/test/java/org/apache/qpid/jms/transports/TransportOptionsTest.java b/qpid-jms-client/src/test/java/org/apache/qpid/jms/transports/TransportOptionsTest.java
index f3d7ec3..272cd86 100644
--- a/qpid-jms-client/src/test/java/org/apache/qpid/jms/transports/TransportOptionsTest.java
+++ b/qpid-jms-client/src/test/java/org/apache/qpid/jms/transports/TransportOptionsTest.java
@@ -42,6 +42,8 @@ public class TransportOptionsTest extends QpidJmsTestCase {
     public static final int TEST_SO_TIMEOUT = 10;
     public static final int TEST_CONNECT_TIMEOUT = 90000;
     public static final int TEST_DEFAULT_TCP_PORT = 5682;
+    public static final String LOCAL_ADDRESS = "localhost";
+    public static final int LOCAL_PORT = 30000;
     public static final boolean TEST_USE_EPOLL_VALUE = !TransportOptions.DEFAULT_USE_EPOLL;
     public static final boolean TEST_TRACE_BYTES_VALUE = !TransportOptions.DEFAULT_TRACE_BYTES;
 
@@ -68,6 +70,7 @@ public class TransportOptionsTest extends QpidJmsTestCase {
     private static final String JAVAX_NET_SSL_TRUST_STORE = "javax.net.ssl.trustStore";
     private static final String JAVAX_NET_SSL_TRUST_STORE_PASSWORD = "javax.net.ssl.trustStorePassword";
 
+
     @Test
     public void testCreate() {
         TransportOptions options = new TransportOptions();
@@ -133,6 +136,8 @@ public class TransportOptionsTest extends QpidJmsTestCase {
         assertEquals(KEYSTORE_TYPE, options.getTrustStoreType());
         assertEquals(KEY_ALIAS, options.getKeyAlias());
         assertEquals(CONTEXT_PROTOCOL, options.getContextProtocol());
+        assertEquals(LOCAL_ADDRESS,options.getLocalAddress());
+        assertEquals(LOCAL_PORT,options.getLocalPort());
         assertEquals(SSL_CONTEXT, options.getSslContextOverride());
         assertArrayEquals(ENABLED_PROTOCOLS,options.getEnabledProtocols());
         assertArrayEquals(DISABLED_PROTOCOLS,options.getDisabledProtocols());
@@ -321,6 +326,8 @@ public class TransportOptionsTest extends QpidJmsTestCase {
         options.setEnabledCipherSuites(ENABLED_CIPHERS);
         options.setDisabledProtocols(DISABLED_PROTOCOLS);
         options.setDisabledCipherSuites(DISABLED_CIPHERS);
+        options.setLocalAddress(LOCAL_ADDRESS);
+        options.setLocalPort(LOCAL_PORT);
 
         return options;
     }
diff --git a/qpid-jms-client/src/test/java/org/apache/qpid/jms/transports/netty/NettySslTransportFactoryTest.java b/qpid-jms-client/src/test/java/org/apache/qpid/jms/transports/netty/NettySslTransportFactoryTest.java
index 054d41c..d81fb34 100644
--- a/qpid-jms-client/src/test/java/org/apache/qpid/jms/transports/netty/NettySslTransportFactoryTest.java
+++ b/qpid-jms-client/src/test/java/org/apache/qpid/jms/transports/netty/NettySslTransportFactoryTest.java
@@ -47,6 +47,9 @@ public class NettySslTransportFactoryTest {
     public static final int CUSTOM_SO_LINGER = Short.MIN_VALUE;
     public static final int CUSTOM_SO_TIMEOUT = 10;
     public static final int CUSTOM_CONNECT_TIMEOUT = 90000;
+    private static final String CUSTOM_LOCAL_ADDRESS = "localhost";
+    private static final int CUSTOM_LOCAL_PORT = 30000;
+
     public static final String CUSTOM_CONTEXT_PROTOCOL = "TLSv1.2";
     public static final String[] CUSTOM_ENABLED_PROTOCOLS = { "TLSv1.1", "TLSv1.2" };
     public static final String CUSTOM_ENABLED_PROTOCOLS_STRING = "TLSv1.1,TLSv1.2";
@@ -88,6 +91,8 @@ public class NettySslTransportFactoryTest {
         assertEquals(TransportOptions.DEFAULT_TCP_KEEP_ALIVE, options.isTcpKeepAlive());
         assertEquals(TransportOptions.DEFAULT_SO_LINGER, options.getSoLinger());
         assertEquals(TransportOptions.DEFAULT_SO_TIMEOUT, options.getSoTimeout());
+        assertNull(options.getLocalAddress());
+        assertEquals(TransportOptions.DEFAULT_LOCAL_PORT, options.getLocalPort());
 
         assertEquals(TransportOptions.DEFAULT_CONTEXT_PROTOCOL, options.getContextProtocol());
         assertNull(options.getEnabledProtocols());
@@ -127,6 +132,8 @@ public class NettySslTransportFactoryTest {
             "transport.tcpKeepAlive=" + CUSTOM_TCP_KEEP_ALIVE + "&" +
             "transport.soLinger=" + CUSTOM_SO_LINGER + "&" +
             "transport.soTimeout=" + CUSTOM_SO_TIMEOUT + "&" +
+            "transport.localAddress=" + CUSTOM_LOCAL_ADDRESS + "&" +
+            "transport.localPort=" + CUSTOM_LOCAL_PORT + "&" +
             "transport.verifyHost=" + CUSTOM_VERIFY_HOST + "&" +
             "transport.storeType=" + CUSTOM_STORE_TYPE + "&" +
             "transport.trustAll=" + CUSTOM_TRUST_ALL + "&" +
@@ -157,6 +164,8 @@ public class NettySslTransportFactoryTest {
         assertEquals(CUSTOM_TCP_KEEP_ALIVE, options.isTcpKeepAlive());
         assertEquals(CUSTOM_SO_LINGER, options.getSoLinger());
         assertEquals(CUSTOM_SO_TIMEOUT, options.getSoTimeout());
+        assertEquals(CUSTOM_LOCAL_ADDRESS, options.getLocalAddress());
+        assertEquals(CUSTOM_LOCAL_PORT, options.getLocalPort());
 
         assertTrue(options instanceof TransportOptions);
         TransportOptions sslOptions = options;
diff --git a/qpid-jms-client/src/test/java/org/apache/qpid/jms/transports/netty/NettyTcpTransportFactoryTest.java b/qpid-jms-client/src/test/java/org/apache/qpid/jms/transports/netty/NettyTcpTransportFactoryTest.java
index 1f0f97c..cbb88d3 100644
--- a/qpid-jms-client/src/test/java/org/apache/qpid/jms/transports/netty/NettyTcpTransportFactoryTest.java
+++ b/qpid-jms-client/src/test/java/org/apache/qpid/jms/transports/netty/NettyTcpTransportFactoryTest.java
@@ -19,6 +19,7 @@ package org.apache.qpid.jms.transports.netty;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
 import java.io.IOException;
@@ -41,6 +42,8 @@ public class NettyTcpTransportFactoryTest {
     public static final int CUSTOM_SO_LINGER = Short.MIN_VALUE;
     public static final int CUSTOM_SO_TIMEOUT = 10;
     public static final int CUSTOM_CONNECT_TIMEOUT = 90000;
+    private static final String CUSTOM_LOCAL_ADDRESS = "localhost";
+    private static final int CUSTOM_LOCAL_PORT = 30000;
 
     @Test(timeout = 30000)
     public void testCreateWithDefaultOptions() throws Exception {
@@ -65,6 +68,8 @@ public class NettyTcpTransportFactoryTest {
         assertEquals(TransportOptions.DEFAULT_TCP_KEEP_ALIVE, options.isTcpKeepAlive());
         assertEquals(TransportOptions.DEFAULT_SO_LINGER, options.getSoLinger());
         assertEquals(TransportOptions.DEFAULT_SO_TIMEOUT, options.getSoTimeout());
+        assertNull(options.getLocalAddress());
+        assertEquals(TransportOptions.DEFAULT_LOCAL_PORT, options.getLocalPort());
     }
 
     @Test(expected = IllegalArgumentException.class)
@@ -116,7 +121,9 @@ public class NettyTcpTransportFactoryTest {
             "transport.tcpNoDelay=" + CUSTOM_TCP_NO_DELAY + "&" +
             "transport.tcpKeepAlive=" + CUSTOM_TCP_KEEP_ALIVE + "&" +
             "transport.soLinger=" + CUSTOM_SO_LINGER + "&" +
-            "transport.soTimeout=" + CUSTOM_SO_TIMEOUT);
+            "transport.soTimeout=" + CUSTOM_SO_TIMEOUT + "&" +
+            "transport.localAddress=" + CUSTOM_LOCAL_ADDRESS + "&" +
+            "transport.localPort=" + CUSTOM_LOCAL_PORT);
 
         NettyTcpTransportFactory factory = new NettyTcpTransportFactory();
 


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org