ARTEMIS-2412 Allow CF configuration through JNDI references

Most connection related properties, like the SSL ones, currently
have to be encoded in the brokerURL. When configuring connections
purely through JNDI bindings, this is not always desireable.
This commit allows one to configure all properties included
in TransportConstants.ALLOWABLE_CONNECTOR_KEYS to be listed separately
in the JNDI bindings. These properties are then zipped into any
provided brokerURL. For properties that appear in both places,
the one specified separately in the JNDI bindings takes priority.

This commit should not affect any configuration other than those
configure through JNDIReferenceFactory.
This commit is contained in:
Maarten Boekhold 2019-07-17 16:43:28 +04:00 committed by Clebert Suconic
parent d0c3839e69
commit 7f4f4a5021
2 changed files with 55 additions and 0 deletions

View File

@ -40,6 +40,7 @@ import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Map;
import java.util.Properties;
import org.apache.activemq.artemis.api.core.DiscoveryGroupConfiguration;
@ -52,12 +53,14 @@ import org.apache.activemq.artemis.api.jms.ActiveMQJMSClient;
import org.apache.activemq.artemis.api.jms.ActiveMQJMSConstants;
import org.apache.activemq.artemis.api.jms.JMSFactoryType;
import org.apache.activemq.artemis.core.client.impl.ServerLocatorImpl;
import org.apache.activemq.artemis.core.remoting.impl.netty.TransportConstants;
import org.apache.activemq.artemis.jndi.JNDIStorable;
import org.apache.activemq.artemis.spi.core.remoting.ClientProtocolManagerFactory;
import org.apache.activemq.artemis.uri.ConnectionFactoryParser;
import org.apache.activemq.artemis.uri.ServerLocatorParser;
import org.apache.activemq.artemis.utils.ClassloadingUtil;
import org.apache.activemq.artemis.utils.uri.BeanSupport;
import org.apache.activemq.artemis.utils.uri.URISupport;
/**
* <p>ActiveMQ Artemis implementation of a JMS ConnectionFactory.</p>
@ -418,6 +421,7 @@ public class ActiveMQConnectionFactory extends JNDIStorable implements Connectio
}
try {
if (url != null && url.length() > 0) {
url = updateBrokerURL(url, props);
setBrokerURL(url);
}
@ -427,6 +431,26 @@ public class ActiveMQConnectionFactory extends JNDIStorable implements Connectio
}
}
private String updateBrokerURL(String url, Properties props) {
ConnectionFactoryParser cfParser = new ConnectionFactoryParser();
try {
URI uri = cfParser.expandURI(url);
final Map<String, String> params = URISupport.parseParameters(uri);
for (String key : TransportConstants.ALLOWABLE_CONNECTOR_KEYS) {
final String val = props.getProperty(key);
if (val != null) {
params.put(key, val);
}
}
final String newUrl = URISupport.applyParameters(uri, params).toString();
return newUrl;
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
@Override
protected void populateProperties(Properties props) {
try {

View File

@ -19,13 +19,19 @@ package org.apache.activemq.artemis.tests.unit.jms.jndi;
import static org.junit.Assert.assertEquals;
import javax.naming.Reference;
import javax.naming.StringRefAddr;
import java.net.URI;
import java.util.Map;
import org.apache.activemq.artemis.api.jms.ActiveMQJMSClient;
import org.apache.activemq.artemis.core.remoting.impl.netty.TransportConstants;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
import org.apache.activemq.artemis.jms.client.ActiveMQDestination;
import org.apache.activemq.artemis.jndi.JNDIReferenceFactory;
import org.apache.activemq.artemis.utils.RandomUtil;
import org.apache.activemq.artemis.utils.uri.URISupport;
import org.junit.Assert;
import org.junit.Test;
@ -135,4 +141,29 @@ public class ObjectFactoryTest {
// Check settings
assertEquals(dest.getAddress(), temp.getAddress());
}
@Test
public void testJndiSslParameters() throws Exception {
Reference reference = new Reference(ActiveMQConnectionFactory.class.getName(), JNDIReferenceFactory.class.getName(), null);
reference.add(new StringRefAddr("brokerURL", "(tcp://localhost:61616,tcp://localhost:5545,tcp://localhost:5555)?sslEnabled=false&trustStorePath=nopath"));
reference.add(new StringRefAddr(TransportConstants.SSL_ENABLED_PROP_NAME, "true"));
reference.add(new StringRefAddr(TransportConstants.TRUSTSTORE_PATH_PROP_NAME, "/path/to/trustStore"));
reference.add(new StringRefAddr(TransportConstants.TRUSTSTORE_PASSWORD_PROP_NAME, "trustStorePassword"));
reference.add(new StringRefAddr(TransportConstants.KEYSTORE_PATH_PROP_NAME, "/path/to/keyStore"));
reference.add(new StringRefAddr(TransportConstants.KEYSTORE_PASSWORD_PROP_NAME, "keyStorePassword"));
reference.add(new StringRefAddr("doesnotexist", "somevalue"));
JNDIReferenceFactory referenceFactory = new JNDIReferenceFactory();
ActiveMQConnectionFactory cf = (ActiveMQConnectionFactory)referenceFactory.getObjectInstance(reference, null, null, null);
URI uri = cf.toURI();
Map<String, String> params = URISupport.parseParameters(uri);
Assert.assertEquals("true", params.get(TransportConstants.SSL_ENABLED_PROP_NAME));
Assert.assertEquals("/path/to/trustStore", params.get(TransportConstants.TRUSTSTORE_PATH_PROP_NAME));
Assert.assertEquals("trustStorePassword", params.get(TransportConstants.TRUSTSTORE_PASSWORD_PROP_NAME));
Assert.assertEquals("/path/to/keyStore", params.get(TransportConstants.KEYSTORE_PATH_PROP_NAME));
Assert.assertEquals("keyStorePassword", params.get(TransportConstants.KEYSTORE_PASSWORD_PROP_NAME));
Assert.assertNull(params.get("doesnotexist"));
}
}