Repository: qpid-jms Updated Branches: refs/heads/master f19b01cb4 -> 5f46af2c1
QPIDJMS-211: make the JNDI connection factory property handling a little more flexible Project: http://git-wip-us.apache.org/repos/asf/qpid-jms/repo Commit: http://git-wip-us.apache.org/repos/asf/qpid-jms/commit/5f46af2c Tree: http://git-wip-us.apache.org/repos/asf/qpid-jms/tree/5f46af2c Diff: http://git-wip-us.apache.org/repos/asf/qpid-jms/diff/5f46af2c Branch: refs/heads/master Commit: 5f46af2c1a2c48c0c9c8fcc8a1733a991e542ac8 Parents: f19b01c Author: Robert Gemmell <[email protected]> Authored: Wed Sep 28 17:49:17 2016 +0100 Committer: Robert Gemmell <[email protected]> Committed: Wed Sep 28 17:52:29 2016 +0100 ---------------------------------------------------------------------- .../qpid/jms/jndi/JmsInitialContextFactory.java | 75 +++++++++----------- .../jms/jndi/JmsInitialContextFactoryTest.java | 48 +++++++++++-- 2 files changed, 78 insertions(+), 45 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/5f46af2c/qpid-jms-client/src/main/java/org/apache/qpid/jms/jndi/JmsInitialContextFactory.java ---------------------------------------------------------------------- diff --git a/qpid-jms-client/src/main/java/org/apache/qpid/jms/jndi/JmsInitialContextFactory.java b/qpid-jms-client/src/main/java/org/apache/qpid/jms/jndi/JmsInitialContextFactory.java index 61482de..f70017c 100644 --- a/qpid-jms-client/src/main/java/org/apache/qpid/jms/jndi/JmsInitialContextFactory.java +++ b/qpid-jms-client/src/main/java/org/apache/qpid/jms/jndi/JmsInitialContextFactory.java @@ -22,13 +22,10 @@ import java.io.IOException; import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URL; -import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.Hashtable; import java.util.Iterator; import java.util.LinkedHashMap; -import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Properties; @@ -44,15 +41,6 @@ import org.apache.qpid.jms.JmsConnectionFactory; import org.apache.qpid.jms.JmsQueue; import org.apache.qpid.jms.JmsTopic; -/** - * A factory of the StompJms InitialContext which contains - * {@link javax.jms.ConnectionFactory} instances as well as a child context - * called <i>destinations</i> which contain all of the current active - * destinations, in child context depending on the QoS such as transient or - * durable and queue or topic. - * - * @since 1.0 - */ public class JmsInitialContextFactory implements InitialContextFactory { static final String[] DEFAULT_CONNECTION_FACTORY_NAMES = { @@ -63,6 +51,8 @@ public class JmsInitialContextFactory implements InitialContextFactory { static final String TOPIC_KEY_PREFIX = "topic."; static final String CONNECTION_FACTORY_DEFAULT_KEY_PREFIX = "default." + CONNECTION_FACTORY_KEY_PREFIX; static final String CONNECTION_FACTORY_PROPERTY_KEY_PREFIX = "property." + CONNECTION_FACTORY_KEY_PREFIX; + static final String DYNAMIC_QUEUES = "dynamicQueues"; + static final String DYNAMIC_TOPICS = "dynamicTopics"; @SuppressWarnings("unchecked") @Override @@ -117,7 +107,7 @@ public class JmsInitialContextFactory implements InitialContextFactory { // Add sub-contexts for dynamic creation on lookup. // "dynamicQueues/<queue-name>" - bindings.put("dynamicQueues", new LazyCreateContext() { + bindings.put(DYNAMIC_QUEUES, new LazyCreateContext() { private static final long serialVersionUID = 6503881346214855588L; @Override @@ -127,7 +117,7 @@ public class JmsInitialContextFactory implements InitialContextFactory { }); // "dynamicTopics/<topic-name>" - bindings.put("dynamicTopics", new LazyCreateContext() { + bindings.put(DYNAMIC_TOPICS, new LazyCreateContext() { private static final long serialVersionUID = 2019166796234979615L; @Override @@ -140,13 +130,15 @@ public class JmsInitialContextFactory implements InitialContextFactory { } private void createConnectionFactories(Hashtable<Object, Object> environment, Map<String, Object> bindings) throws NamingException { - List<String> names = getConnectionFactoryNames(environment); + Map<String, String> factories = getConnectionFactoryNamesAndURIs(environment); Map<String, String> defaults = getConnectionFactoryDefaults(environment); - for (String name : names) { - JmsConnectionFactory factory = null; + for (Entry<String, String> entry : factories.entrySet()) { + String name = entry.getKey(); + String uri = entry.getValue(); + JmsConnectionFactory factory = null; try { - factory = createConnectionFactory(name, defaults, environment); + factory = createConnectionFactory(name, uri, defaults, environment); } catch (Exception e) { NamingException ne = new NamingException("Exception while creating ConnectionFactory '" + name + "'."); ne.initCause(e); @@ -164,20 +156,15 @@ public class JmsInitialContextFactory implements InitialContextFactory { return new ReadOnlyContext(environment, bindings); } - protected JmsConnectionFactory createConnectionFactory(String name, Map<String, String> defaults, Hashtable<Object, Object> environment) throws URISyntaxException { - String cfNameKey = CONNECTION_FACTORY_KEY_PREFIX + name; + protected JmsConnectionFactory createConnectionFactory(String name, String uri, Map<String, String> defaults, Hashtable<Object, Object> environment) throws URISyntaxException { Map<String, String> props = new LinkedHashMap<String, String>(); // Add the defaults which apply to all connection factories props.putAll(defaults); // Add any URI entry for this specific factory name - Object o = environment.get(cfNameKey); - if (o != null) { - String value = String.valueOf(o); - if (value.trim().length() != 0) { - props.put(JmsConnectionFactory.REMOTE_URI_PROP, value); - } + if (uri != null && !uri.trim().isEmpty()) { + props.put(JmsConnectionFactory.REMOTE_URI_PROP, uri); } // Add any factory-specific additional properties @@ -186,23 +173,29 @@ public class JmsInitialContextFactory implements InitialContextFactory { return createConnectionFactory(props); } - protected List<String> getConnectionFactoryNames(Map<Object, Object> environment) { - List<String> list = new ArrayList<String>(); + protected Map<String, String> getConnectionFactoryNamesAndURIs(Map<Object, Object> environment) { + Map<String, String> factories = new LinkedHashMap<String, String>(); for (Iterator<Entry<Object, Object>> iter = environment.entrySet().iterator(); iter.hasNext();) { Map.Entry<Object, Object> entry = iter.next(); String key = String.valueOf(entry.getKey()); - if (key.startsWith(CONNECTION_FACTORY_KEY_PREFIX)) { - String jndiName = key.substring(CONNECTION_FACTORY_KEY_PREFIX.length()); - list.add(jndiName); + if (key.toLowerCase().startsWith(CONNECTION_FACTORY_KEY_PREFIX)) { + String factoryName = key.substring(CONNECTION_FACTORY_KEY_PREFIX.length()); + String value = null; + if(entry.getValue() != null) { + value = String.valueOf(entry.getValue()); + } + + factories.put(factoryName, value); } } - if(list.isEmpty()) - { - list.addAll(Arrays.asList(DEFAULT_CONNECTION_FACTORY_NAMES)); + if (factories.isEmpty()) { + for (int i = 0; i < DEFAULT_CONNECTION_FACTORY_NAMES.length; i++) { + factories.put(DEFAULT_CONNECTION_FACTORY_NAMES[i], null); + } } - return list; + return factories; } protected Map<String, String> getConnectionFactoryDefaults(Map<Object, Object> environment) { @@ -212,7 +205,7 @@ public class JmsInitialContextFactory implements InitialContextFactory { for (Iterator<Entry<Object, Object>> iter = environment.entrySet().iterator(); iter.hasNext();) { Map.Entry<Object, Object> entry = iter.next(); String key = String.valueOf(entry.getKey()); - if (key.startsWith(CONNECTION_FACTORY_DEFAULT_KEY_PREFIX)) { + if (key.toLowerCase().startsWith(CONNECTION_FACTORY_DEFAULT_KEY_PREFIX)) { String jndiName = key.substring(CONNECTION_FACTORY_DEFAULT_KEY_PREFIX.length()); map.put(jndiName, String.valueOf(entry.getValue())); } @@ -224,14 +217,16 @@ public class JmsInitialContextFactory implements InitialContextFactory { protected Map<String, String> getConnectionFactoryProperties(String factoryName, Map<Object, Object> environment) { Map<String, String> map = new LinkedHashMap<String, String>(); - String factoryPropertiesPrefix = CONNECTION_FACTORY_PROPERTY_KEY_PREFIX + factoryName + "."; + final String factoryNameSuffix = factoryName + "."; for (Iterator<Entry<Object, Object>> iter = environment.entrySet().iterator(); iter.hasNext();) { Map.Entry<Object, Object> entry = iter.next(); String key = String.valueOf(entry.getKey()); - if (key.startsWith(factoryPropertiesPrefix)) { - String propertyName = key.substring(factoryPropertiesPrefix.length()); - map.put(propertyName, String.valueOf(entry.getValue())); + if (key.toLowerCase().startsWith(CONNECTION_FACTORY_PROPERTY_KEY_PREFIX)) { + if(key.substring(CONNECTION_FACTORY_PROPERTY_KEY_PREFIX.length()).startsWith(factoryNameSuffix)) { + String propertyName = key.substring(CONNECTION_FACTORY_PROPERTY_KEY_PREFIX.length() + factoryNameSuffix.length()); + map.put(propertyName, String.valueOf(entry.getValue())); + } } } http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/5f46af2c/qpid-jms-client/src/test/java/org/apache/qpid/jms/jndi/JmsInitialContextFactoryTest.java ---------------------------------------------------------------------- diff --git a/qpid-jms-client/src/test/java/org/apache/qpid/jms/jndi/JmsInitialContextFactoryTest.java b/qpid-jms-client/src/test/java/org/apache/qpid/jms/jndi/JmsInitialContextFactoryTest.java index c1a0652..9400ad5 100644 --- a/qpid-jms-client/src/test/java/org/apache/qpid/jms/jndi/JmsInitialContextFactoryTest.java +++ b/qpid-jms-client/src/test/java/org/apache/qpid/jms/jndi/JmsInitialContextFactoryTest.java @@ -87,10 +87,16 @@ public class JmsInitialContextFactoryTest extends QpidJmsTestCase { @Test public void testDefaultConnectionFactorySeesFactorySpecificProperty() throws Exception { - String updatedClientID = _testName.getMethodName(); - String factoryName = JmsInitialContextFactory.DEFAULT_CONNECTION_FACTORY_NAMES[0]; - String propertyPrefix = JmsInitialContextFactory.CONNECTION_FACTORY_PROPERTY_KEY_PREFIX; + + // lower case prefix + doDefaultConnectionFactorySeesFactorySpecificPropertyTestImpl("property.connectionfactory.", factoryName); + // camelCase prefix + doDefaultConnectionFactorySeesFactorySpecificPropertyTestImpl("property.connectionFactory.", factoryName); + } + + private void doDefaultConnectionFactorySeesFactorySpecificPropertyTestImpl(String propertyPrefix, String factoryName) throws Exception { + String updatedClientID = _testName.getMethodName(); Hashtable<Object, Object> env = new Hashtable<Object, Object>(); env.put(propertyPrefix + factoryName + "." + "clientID", updatedClientID); @@ -100,7 +106,7 @@ public class JmsInitialContextFactoryTest extends QpidJmsTestCase { assertNotNull("No object returned", o); assertEquals("Unexpected class type for returned object", JmsConnectionFactory.class, o.getClass()); - assertEquals("Unexpected URI for returned factory", updatedClientID, ((JmsConnectionFactory) o).getClientID()); + assertEquals("Unexpected ClientID for returned factory", updatedClientID, ((JmsConnectionFactory) o).getClientID()); } @Test @@ -119,13 +125,45 @@ public class JmsInitialContextFactoryTest extends QpidJmsTestCase { } } + + @Test + public void testDefaultConnectionFactoriesSeeDefaultPropertyUpdate() throws Exception { + String factoryName = JmsInitialContextFactory.DEFAULT_CONNECTION_FACTORY_NAMES[0]; + + // lower case prefix + doDefaultConnectionFactorySeesDefaultPropertyUpdatePropertyTestImpl("default.connectionfactory.", factoryName); + // camelCase prefix + doDefaultConnectionFactorySeesDefaultPropertyUpdatePropertyTestImpl("default.connectionFactory.", factoryName); + } + + private void doDefaultConnectionFactorySeesDefaultPropertyUpdatePropertyTestImpl(String propertyPrefix, String factoryName) throws Exception { + String updatedClientID = _testName.getMethodName(); + + Hashtable<Object, Object> env = new Hashtable<Object, Object>(); + env.put(propertyPrefix + "clientID", updatedClientID); + Context ctx = createInitialContext(env); + + Object o = ctx.lookup(factoryName); + + assertNotNull("No object returned", o); + assertEquals("Unexpected class type for returned object", JmsConnectionFactory.class, o.getClass()); + assertEquals("Unexpected ClientID for returned factory", updatedClientID, ((JmsConnectionFactory) o).getClientID()); + } + @Test public void testConnectionFactoryBinding() throws Exception { String factoryName = "myNewFactory"; + // lower case prefix + doConnectionFactoryBindingTestImpl("connectionfactory." + factoryName, factoryName); + // camelCase prefix + doConnectionFactoryBindingTestImpl("connectionFactory." + factoryName, factoryName); + } + + private void doConnectionFactoryBindingTestImpl(String environmentProperty, String factoryName) throws NamingException { String uri = "amqp://example.com:1234"; Hashtable<Object, Object> env = new Hashtable<Object, Object>(); - env.put(JmsInitialContextFactory.CONNECTION_FACTORY_KEY_PREFIX + factoryName, uri); + env.put(environmentProperty, uri); Context ctx = createInitialContext(env); Object o = ctx.lookup(factoryName); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
