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

This commit is contained in:
gtully 2016-08-09 15:22:19 +01:00
parent 9249315688
commit 4800a7a1a4
4 changed files with 109 additions and 4 deletions

View File

@ -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;
}
}

View File

@ -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"));
}
}

View File

@ -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 {

View File

@ -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>