Quantcast

activemq-artemis git commit: ARTEMIS-1164: NameNotFoundException when using java.naming.provider.url to set url via jndi

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

activemq-artemis git commit: ARTEMIS-1164: NameNotFoundException when using java.naming.provider.url to set url via jndi

jbertram
Repository: activemq-artemis
Updated Branches:
  refs/heads/master 502f9c544 -> c6ac1d943


ARTEMIS-1164: NameNotFoundException when using java.naming.provider.url to set url via jndi

Support setting PROVIDER_URL on initial context to create default connection factories.


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

Branch: refs/heads/master
Commit: c6ac1d943c6e0b3387896e59537d7bf22e19d81c
Parents: 502f9c5
Author: Michael Andre Pearce <[hidden email]>
Authored: Mon May 15 17:23:52 2017 +0100
Committer: Justin Bertram <[hidden email]>
Committed: Mon May 15 15:20:53 2017 -0500

----------------------------------------------------------------------
 .../jndi/ActiveMQInitialContextFactory.java     | 23 +++++++++++++++--
 .../integration/jms/SimpleJNDIClientTest.java   | 27 ++++++++++++++++++++
 2 files changed, 48 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/c6ac1d94/artemis-jms-client/src/main/java/org/apache/activemq/artemis/jndi/ActiveMQInitialContextFactory.java
----------------------------------------------------------------------
diff --git a/artemis-jms-client/src/main/java/org/apache/activemq/artemis/jndi/ActiveMQInitialContextFactory.java b/artemis-jms-client/src/main/java/org/apache/activemq/artemis/jndi/ActiveMQInitialContextFactory.java
index 26641e9..9b1b7a0 100644
--- a/artemis-jms-client/src/main/java/org/apache/activemq/artemis/jndi/ActiveMQInitialContextFactory.java
+++ b/artemis-jms-client/src/main/java/org/apache/activemq/artemis/jndi/ActiveMQInitialContextFactory.java
@@ -22,6 +22,7 @@ import javax.jms.Topic;
 import javax.naming.Context;
 import javax.naming.NamingException;
 import javax.naming.spi.InitialContextFactory;
+import java.util.HashMap;
 import java.util.Hashtable;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
@@ -38,6 +39,8 @@ import org.apache.activemq.artemis.uri.ConnectionFactoryParser;
  */
 public class ActiveMQInitialContextFactory implements InitialContextFactory {
 
+   private static final String[] DEFAULT_CONNECTION_FACTORY_NAMES = {"ConnectionFactory", "XAConnectionFactory", "QueueConnectionFactory", "TopicConnectionFactory"};
+
    public static final String REFRESH_TIMEOUT = "refreshTimeout";
    public static final String DISCOVERY_INITIAL_WAIT_TIMEOUT = "discoveryInitialWaitTimeout";
    public static final String DYNAMIC_QUEUE_CONTEXT = "dynamicQueues";
@@ -50,13 +53,14 @@ public class ActiveMQInitialContextFactory implements InitialContextFactory {
    public Context getInitialContext(Hashtable<?, ?> environment) throws NamingException {
       // lets create a factory
       Map<String, Object> data = new ConcurrentHashMap<>();
+
+      Map<String, ConnectionFactory> connectionFactories = new HashMap<>();
       for (Map.Entry<?, ?> entry : environment.entrySet()) {
          String key = entry.getKey().toString();
          if (key.startsWith(connectionFactoryPrefix)) {
             String jndiName = key.substring(connectionFactoryPrefix.length());
             try {
-               ConnectionFactory factory = createConnectionFactory((String) environment.get(key), jndiName);
-               data.put(jndiName, factory);
+               connectionFactories.put(jndiName, createConnectionFactory((String) environment.get(key), jndiName));
             } catch (Exception e) {
                e.printStackTrace();
                throw new NamingException("Invalid broker URL");
@@ -64,6 +68,21 @@ public class ActiveMQInitialContextFactory implements InitialContextFactory {
          }
       }
 
+      if (connectionFactories.isEmpty()) {
+         String providerUrl = (String) environment.get(javax.naming.Context.PROVIDER_URL);
+         if (providerUrl != null) {
+            for (String factoryName : DEFAULT_CONNECTION_FACTORY_NAMES) {
+               try {
+                  connectionFactories.put(factoryName, createConnectionFactory(providerUrl, factoryName));
+               } catch (Exception e) {
+                  e.printStackTrace();
+                  throw new NamingException("Invalid broker URL");
+               }
+            }
+         }
+      }
+      connectionFactories.forEach((name, factory) -> data.put(name, factory));
+
       createQueues(data, environment);
       createTopics(data, environment);
 

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/c6ac1d94/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/jms/SimpleJNDIClientTest.java
----------------------------------------------------------------------
diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/jms/SimpleJNDIClientTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/jms/SimpleJNDIClientTest.java
index 193a0ab..7fcc9a0 100644
--- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/jms/SimpleJNDIClientTest.java
+++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/jms/SimpleJNDIClientTest.java
@@ -35,6 +35,7 @@ import java.util.HashSet;
 import java.util.Hashtable;
 import java.util.List;
 import java.util.Map;
+import java.util.Properties;
 import java.util.Set;
 
 import org.apache.activemq.artemis.api.config.ActiveMQDefaultConfiguration;
@@ -526,4 +527,30 @@ public class SimpleJNDIClientTest extends ActiveMQTestBase {
       }
 
    }
+
+   @Test
+   public void providerURLTest() throws NamingException {
+      String url = "(tcp://somehost:62616,tcp://somehost:62616)?ha=true";
+
+      Properties props = new Properties();
+      props.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, ActiveMQInitialContextFactory.class.getName());
+      props.setProperty(javax.naming.Context.PROVIDER_URL, url);
+
+      InitialContext context =  new InitialContext(props);
+      ConnectionFactory connectionFactory = (ConnectionFactory)context.lookup("ConnectionFactory");
+   }
+
+   @Test
+   public void connectionFactoryProperty() throws NamingException {
+      String url = "(tcp://somehost:62616,tcp://somehost:62616)?ha=true";
+
+      Properties props = new Properties();
+      props.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, ActiveMQInitialContextFactory.class.getName());
+      props.setProperty(javax.naming.Context.PROVIDER_URL, url);
+
+      props.setProperty("connectionFactory.ConnectionFactory",url);
+
+      InitialContext context =  new InitialContext(props);
+      ConnectionFactory connectionFactory = (ConnectionFactory)context.lookup("ConnectionFactory");
+   }
 }

Loading...