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

[activemq-artemis] branch master updated: ARTEMIS-2412 Allow CF configuration through JNDI references

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

clebertsuconic pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/activemq-artemis.git


The following commit(s) were added to refs/heads/master by this push:
     new 7f4f4a5  ARTEMIS-2412 Allow CF configuration through JNDI references
     new 440002f  This closes #2757
7f4f4a5 is described below

commit 7f4f4a5021ca856f9cf35a6cb7cc03dfb5589abb
Author: Maarten Boekhold <bo...@gmx.com>
AuthorDate: Wed Jul 17 16:43:28 2019 +0400

    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.
---
 .../jms/client/ActiveMQConnectionFactory.java      | 24 +++++++++++++++++
 .../tests/unit/jms/jndi/ObjectFactoryTest.java     | 31 ++++++++++++++++++++++
 2 files changed, 55 insertions(+)

diff --git a/artemis-jms-client/src/main/java/org/apache/activemq/artemis/jms/client/ActiveMQConnectionFactory.java b/artemis-jms-client/src/main/java/org/apache/activemq/artemis/jms/client/ActiveMQConnectionFactory.java
index 238f064..f050c42 100644
--- a/artemis-jms-client/src/main/java/org/apache/activemq/artemis/jms/client/ActiveMQConnectionFactory.java
+++ b/artemis-jms-client/src/main/java/org/apache/activemq/artemis/jms/client/ActiveMQConnectionFactory.java
@@ -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 {
diff --git a/tests/unit-tests/src/test/java/org/apache/activemq/artemis/tests/unit/jms/jndi/ObjectFactoryTest.java b/tests/unit-tests/src/test/java/org/apache/activemq/artemis/tests/unit/jms/jndi/ObjectFactoryTest.java
index 9ceebfd..6617932 100644
--- a/tests/unit-tests/src/test/java/org/apache/activemq/artemis/tests/unit/jms/jndi/ObjectFactoryTest.java
+++ b/tests/unit-tests/src/test/java/org/apache/activemq/artemis/tests/unit/jms/jndi/ObjectFactoryTest.java
@@ -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"));
+   }
 }
\ No newline at end of file