You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by gt...@apache.org on 2016/08/09 14:22:44 UTC

activemq git commit: AMQ-6392 - allow mapping of host names via the publish address strategy

Repository: activemq
Updated Branches:
  refs/heads/master 924931568 -> 4800a7a1a


AMQ-6392 - allow mapping of host names via the publish address strategy


Project: http://git-wip-us.apache.org/repos/asf/activemq/repo
Commit: http://git-wip-us.apache.org/repos/asf/activemq/commit/4800a7a1
Tree: http://git-wip-us.apache.org/repos/asf/activemq/tree/4800a7a1
Diff: http://git-wip-us.apache.org/repos/asf/activemq/diff/4800a7a1

Branch: refs/heads/master
Commit: 4800a7a1a46dd248b130b4289cf9f26787fe5612
Parents: 9249315
Author: gtully <ga...@gmail.com>
Authored: Tue Aug 9 15:22:19 2016 +0100
Committer: gtully <ga...@gmail.com>
Committed: Tue Aug 9 15:22:28 2016 +0100

----------------------------------------------------------------------
 .../activemq/broker/PublishedAddressPolicy.java | 25 ++++++-
 .../broker/PublishedAddressPolicyTest.java      | 70 ++++++++++++++++++++
 .../xbean/ConnectorXBeanConfigTest.java         |  2 +
 .../apache/activemq/xbean/connector-test.xml    | 16 ++++-
 4 files changed, 109 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/activemq/blob/4800a7a1/activemq-broker/src/main/java/org/apache/activemq/broker/PublishedAddressPolicy.java
----------------------------------------------------------------------
diff --git a/activemq-broker/src/main/java/org/apache/activemq/broker/PublishedAddressPolicy.java b/activemq-broker/src/main/java/org/apache/activemq/broker/PublishedAddressPolicy.java
index 53aeead..3b8b902 100644
--- a/activemq-broker/src/main/java/org/apache/activemq/broker/PublishedAddressPolicy.java
+++ b/activemq-broker/src/main/java/org/apache/activemq/broker/PublishedAddressPolicy.java
@@ -21,6 +21,7 @@ import java.net.URI;
 import java.net.UnknownHostException;
 import java.util.HashMap;
 import java.util.Locale;
+import java.util.Map;
 
 import org.apache.activemq.util.InetAddressUtil;
 
@@ -35,7 +36,8 @@ public class PublishedAddressPolicy {
 
     private String clusterClientUriQuery;
     private PublishedHostStrategy publishedHostStrategy = PublishedHostStrategy.DEFAULT;
-    private HashMap<Integer, Integer> portMapping = new HashMap<Integer, Integer>();
+    private Map<Integer, Integer> portMapping = new HashMap<Integer, Integer>();
+    private Map<String, String> hostMapping = new HashMap<String, String>();
 
     /**
      * Defines the value of the published host value.
@@ -75,6 +77,10 @@ public class PublishedAddressPolicy {
 
         String userInfo = getPublishedUserInfoValue(connectorURI.getUserInfo());
         String host = getPublishedHostValue(connectorURI.getHost());
+        if (hostMapping.containsKey(host)) {
+            host = hostMapping.get(host);
+        }
+
         int port = connectorURI.getPort();
         if (portMapping.containsKey(port)) {
             port = portMapping.get(port);
@@ -215,7 +221,22 @@ public class PublishedAddressPolicy {
     /**
      * @param portMapping map the ports in restrictive environments
      */
-    public void setPortMapping(HashMap<Integer, Integer> portMapping) {
+    public void setPortMapping(Map<Integer, Integer> portMapping) {
         this.portMapping = portMapping;
     }
+
+    public Map<Integer, Integer>  getPortMapping() {
+        return this.portMapping;
+    }
+
+    /**
+     * @param hostMapping map the resolved hosts
+     */
+    public void setHostMapping(Map<String, String> hostMapping) {
+        this.hostMapping = hostMapping;
+    }
+
+    public Map<String, String> getHostMapping() {
+        return hostMapping;
+    }
 }

http://git-wip-us.apache.org/repos/asf/activemq/blob/4800a7a1/activemq-broker/src/test/java/org/apache/activemq/broker/PublishedAddressPolicyTest.java
----------------------------------------------------------------------
diff --git a/activemq-broker/src/test/java/org/apache/activemq/broker/PublishedAddressPolicyTest.java b/activemq-broker/src/test/java/org/apache/activemq/broker/PublishedAddressPolicyTest.java
new file mode 100644
index 0000000..d5b719c
--- /dev/null
+++ b/activemq-broker/src/test/java/org/apache/activemq/broker/PublishedAddressPolicyTest.java
@@ -0,0 +1,70 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.activemq.broker;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicReference;
+
+import static org.junit.Assert.*;
+
+public class PublishedAddressPolicyTest {
+    PublishedAddressPolicy underTest = new PublishedAddressPolicy();
+
+    final AtomicReference<URI> uriAtomicReference = new AtomicReference<>();
+
+    final TransportConnector dummyTransportConnector = new TransportConnector() {
+        @Override
+        public URI getConnectUri() throws IOException, URISyntaxException {
+            return uriAtomicReference.get();
+        }
+    };
+
+    @Before
+    public void setTransport() throws Exception {
+        URI ok = new URI("tcp://bob:88");
+        uriAtomicReference.set(ok);
+    }
+
+    @Test
+    public void testDefaultReturnsHost() throws Exception {
+        assertTrue("contains bob", underTest.getPublishableConnectString(dummyTransportConnector).contains("bob"));
+    }
+
+    @Test
+    public void testHostMap() throws Exception {
+        HashMap<String, String> hostMap = new HashMap<>();
+        hostMap.put("bob", "pat");
+        underTest.setHostMapping(hostMap);
+        assertTrue("contains pat", underTest.getPublishableConnectString(dummyTransportConnector).contains("pat"));
+    }
+
+    @Test
+    public void testPortMap() throws Exception {
+        Map<Integer, Integer> portMap = new HashMap<Integer, Integer>();
+        portMap.put(88, 77);
+        underTest.setPortMapping(portMap);
+        assertTrue("contains 77", underTest.getPublishableConnectString(dummyTransportConnector).contains("77"));
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/activemq/blob/4800a7a1/activemq-unit-tests/src/test/java/org/apache/activemq/xbean/ConnectorXBeanConfigTest.java
----------------------------------------------------------------------
diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/xbean/ConnectorXBeanConfigTest.java b/activemq-unit-tests/src/test/java/org/apache/activemq/xbean/ConnectorXBeanConfigTest.java
index ad96459..d1db7f6 100644
--- a/activemq-unit-tests/src/test/java/org/apache/activemq/xbean/ConnectorXBeanConfigTest.java
+++ b/activemq-unit-tests/src/test/java/org/apache/activemq/xbean/ConnectorXBeanConfigTest.java
@@ -67,6 +67,8 @@ public class ConnectorXBeanConfigTest extends TestCase {
 
         // redundant start is now ignored
         brokerService.start();
+
+        assertTrue("mapped address in published address", brokerService.getTransportConnectorByScheme("tcp").getPublishableConnectString().contains("Mapped"));
     }
 
     public void testForceBrokerRestart() throws Exception {

http://git-wip-us.apache.org/repos/asf/activemq/blob/4800a7a1/activemq-unit-tests/src/test/resources/org/apache/activemq/xbean/connector-test.xml
----------------------------------------------------------------------
diff --git a/activemq-unit-tests/src/test/resources/org/apache/activemq/xbean/connector-test.xml b/activemq-unit-tests/src/test/resources/org/apache/activemq/xbean/connector-test.xml
index 5fb6403..3a83570 100644
--- a/activemq-unit-tests/src/test/resources/org/apache/activemq/xbean/connector-test.xml
+++ b/activemq-unit-tests/src/test/resources/org/apache/activemq/xbean/connector-test.xml
@@ -20,12 +20,24 @@
 <!-- START SNIPPET: xbean -->
 <beans 
   xmlns="http://www.springframework.org/schema/beans" 
-  xmlns:amq="http://activemq.apache.org/schema/core"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
   http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">
 
   <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/>
+  <bean id="publishedAddressPolicy" class="org.apache.activemq.broker.PublishedAddressPolicy">
+    <property name="portMapping">
+      <map key-type="java.lang.Integer" value-type="java.lang.Integer">
+        <entry key="61636" value="6666" />
+      </map>
+    </property>
+    <property name="hostMapping">
+      <map key-type="java.lang.String" value-type="java.lang.String">
+        <entry key="localhost" value="Mapped" />
+      </map>
+    </property>
+  </bean>
+
 
   <broker useJmx="true" xmlns="http://activemq.apache.org/schema/core" persistent="false">
 
@@ -52,7 +64,7 @@
     </networkConnectors>
 
     <transportConnectors>
-      <transportConnector uri="tcp://localhost:61636" />
+      <transportConnector uri="tcp://localhost:61636" publishedAddressPolicy="#publishedAddressPolicy"/>
     </transportConnectors>
 	
   </broker>