Merge pull request #471 from jgallimore/connector-path-fix2

Fix issue where the registry lookup was a hardcoded name and didn't a…
This commit is contained in:
Jean-Baptiste Onofré 2020-02-24 13:24:33 +01:00 committed by GitHub
commit a58a624c3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 86 additions and 13 deletions

View File

@ -70,6 +70,11 @@ public class ManagementContext implements Service {
*/ */
public static final String DEFAULT_DOMAIN = "org.apache.activemq"; public static final String DEFAULT_DOMAIN = "org.apache.activemq";
/**
* Default registry lookup name
*/
public static final String DEFAULT_LOOKUP_NAME = "jmxrmi";
static { static {
String option = Boolean.FALSE.toString(); String option = Boolean.FALSE.toString();
try { try {
@ -95,6 +100,7 @@ public class ManagementContext implements Service {
private Map<String, ?> environment; private Map<String, ?> environment;
private int rmiServerPort; private int rmiServerPort;
private String connectorPath = "/jmxrmi"; private String connectorPath = "/jmxrmi";
private String lookupName = DEFAULT_LOOKUP_NAME;
private final AtomicBoolean started = new AtomicBoolean(false); private final AtomicBoolean started = new AtomicBoolean(false);
private final CountDownLatch connectorStarted = new CountDownLatch(1); private final CountDownLatch connectorStarted = new CountDownLatch(1);
private JMXConnectorServer connectorServer; private JMXConnectorServer connectorServer;
@ -597,6 +603,12 @@ public class ManagementContext implements Service {
public void setConnectorPath(String connectorPath) { public void setConnectorPath(String connectorPath) {
this.connectorPath = connectorPath; this.connectorPath = connectorPath;
if (connectorPath == null || connectorPath.length() == 0) {
this.lookupName = DEFAULT_LOOKUP_NAME;
} else {
this.lookupName = connectorPath.replaceAll("^/+", "").replaceAll("/+$", "");
}
} }
public int getConnectorPort() { public int getConnectorPort() {
@ -684,16 +696,15 @@ public class ManagementContext implements Service {
*/ */
@SuppressWarnings("restriction") @SuppressWarnings("restriction")
private class JmxRegistry extends sun.rmi.registry.RegistryImpl { private class JmxRegistry extends sun.rmi.registry.RegistryImpl {
public static final String LOOKUP_NAME = "jmxrmi";
public JmxRegistry(int port) throws RemoteException { public JmxRegistry(int port) throws RemoteException {
super(port); super(port);
} }
@Override @Override
public Remote lookup(String s) throws RemoteException, NotBoundException { public Remote lookup(String s) throws RemoteException, NotBoundException {
return LOOKUP_NAME.equals(s) ? serverStub : null; return lookupName.equals(s) ? serverStub : null;
} }
@Override @Override
@ -710,7 +721,7 @@ public class ManagementContext implements Service {
@Override @Override
public String[] list() throws RemoteException { public String[] list() throws RemoteException {
return new String[] {LOOKUP_NAME}; return new String[] {lookupName};
} }
} }
} }

View File

@ -42,10 +42,18 @@ import org.slf4j.LoggerFactory;
*/ */
public class ManagementContextXBeanConfigTest extends TestCase { public class ManagementContextXBeanConfigTest extends TestCase {
protected BrokerService brokerService; private static final String MANAGEMENT_CONTEXT_TEST_XML = "org/apache/activemq/xbean/management-context-test.xml";
private static final String MANAGEMENT_CONTEXT_TEST_XML_CONNECTOR_PATH =
"org/apache/activemq/xbean/management-context-test-connector-path.xml";
private static final transient Logger LOG = LoggerFactory.getLogger(ManagementContextXBeanConfigTest.class); private static final transient Logger LOG = LoggerFactory.getLogger(ManagementContextXBeanConfigTest.class);
protected BrokerService brokerService;
public void testManagmentContextConfiguredCorrectly() throws Exception { public void testManagmentContextConfiguredCorrectly() throws Exception {
brokerService = getBrokerService(MANAGEMENT_CONTEXT_TEST_XML);
brokerService.start();
assertEquals(2011, brokerService.getManagementContext().getConnectorPort()); assertEquals(2011, brokerService.getManagementContext().getConnectorPort());
assertEquals("test.domain", brokerService.getManagementContext().getJmxDomainName()); assertEquals("test.domain", brokerService.getManagementContext().getJmxDomainName());
// Make sure the broker is registered in the right jmx domain. // Make sure the broker is registered in the right jmx domain.
@ -58,6 +66,9 @@ public class ManagementContextXBeanConfigTest extends TestCase {
} }
public void testSuccessAuthentication() throws Exception { public void testSuccessAuthentication() throws Exception {
brokerService = getBrokerService(MANAGEMENT_CONTEXT_TEST_XML);
brokerService.start();
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:2011/jmxrmi"); JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:2011/jmxrmi");
Map<String, Object> env = new HashMap<String, Object>(); Map<String, Object> env = new HashMap<String, Object>();
env.put(JMXConnector.CREDENTIALS, new String[]{"admin", "activemq"}); env.put(JMXConnector.CREDENTIALS, new String[]{"admin", "activemq"});
@ -65,7 +76,21 @@ public class ManagementContextXBeanConfigTest extends TestCase {
assertAuthentication(connector); assertAuthentication(connector);
} }
public void testConnectorPath() throws Exception {
brokerService = getBrokerService(MANAGEMENT_CONTEXT_TEST_XML_CONNECTOR_PATH);
brokerService.start();
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:2011/activemq-jmx");
Map<String, Object> env = new HashMap<String, Object>();
env.put(JMXConnector.CREDENTIALS, new String[]{"admin", "activemq"});
JMXConnector connector = JMXConnectorFactory.connect(url, env);
assertAuthentication(connector);
}
public void testFailAuthentication() throws Exception { public void testFailAuthentication() throws Exception {
brokerService = getBrokerService(MANAGEMENT_CONTEXT_TEST_XML);
brokerService.start();
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:2011/jmxrmi"); JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:2011/jmxrmi");
try { try {
JMXConnector connector = JMXConnectorFactory.connect(url, null); JMXConnector connector = JMXConnectorFactory.connect(url, null);
@ -85,12 +110,6 @@ public class ManagementContextXBeanConfigTest extends TestCase {
LOG.info("Broker " + mbean.getBrokerId() + " - " + mbean.getBrokerName()); LOG.info("Broker " + mbean.getBrokerId() + " - " + mbean.getBrokerName());
} }
@Override
protected void setUp() throws Exception {
brokerService = createBroker();
brokerService.start();
}
@Override @Override
protected void tearDown() throws Exception { protected void tearDown() throws Exception {
if (brokerService != null) { if (brokerService != null) {
@ -98,8 +117,7 @@ public class ManagementContextXBeanConfigTest extends TestCase {
} }
} }
protected BrokerService createBroker() throws Exception { private BrokerService getBrokerService(String uri) throws Exception {
String uri = "org/apache/activemq/xbean/management-context-test.xml";
return BrokerFactory.createBroker(new URI("xbean:" + uri)); return BrokerFactory.createBroker(new URI("xbean:" + uri));
} }

View File

@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<!-- this file can only be parsed using the xbean-spring library -->
<!-- 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" />
<broker useJmx="true" xmlns="http://activemq.apache.org/schema/core">
<managementContext>
<managementContext createConnector="true" connectorPort="2011" jmxDomainName="test.domain" connectorPath="/activemq-jmx">
<property xmlns="http://www.springframework.org/schema/beans" name="environment">
<map xmlns="http://www.springframework.org/schema/beans">
<entry xmlns="http://www.springframework.org/schema/beans" key="jmx.remote.x.password.file" value="src/test/resources/jmx.password"/>
<entry xmlns="http://www.springframework.org/schema/beans" key="jmx.remote.x.access.file" value="src/test/resources/jmx.access"/>
</map>
</property>
</managementContext>
</managementContext>
</broker>
</beans>
<!-- END SNIPPET: xbean -->