diff options
author | Weston M. Price <wprice@apache.org> | 2012-05-03 19:03:57 +0000 |
---|---|---|
committer | Weston M. Price <wprice@apache.org> | 2012-05-03 19:03:57 +0000 |
commit | 7d01b7ea7529ccb52e4f965f3997d67f153a5b0e (patch) | |
tree | 7a591efe6018c346003aeaab43ca1c564a072499 | |
parent | 82695a85d7f1caa65b6ce805f2214abafd2f7f52 (diff) | |
download | qpid-python-7d01b7ea7529ccb52e4f965f3997d67f153a5b0e.tar.gz |
QPID-3971: PropertiesFileInitialContextFactory cannot open file URL
*Added capability to read java.naming.provider.url from file:// URI
*Added PropertiesFileInitialContextFactoryTest
*Moved functionaly from old JNDIPropertiesTest to new test class
*Minor cleanup. Added @SuppressWarnings annotation to
PropertiesFileInitialContext
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1333586 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | qpid/java/client/src/main/java/org/apache/qpid/jndi/PropertiesFileInitialContextFactory.java | 62 | ||||
-rw-r--r-- | qpid/java/client/src/test/java/org/apache/qpid/jndi/JNDITest.properties (renamed from qpid/java/client/src/test/java/org/apache/qpid/test/unit/jndi/JNDITest.properties) | 0 | ||||
-rw-r--r-- | qpid/java/client/src/test/java/org/apache/qpid/jndi/PropertiesFileInitialContextFactoryTest.java (renamed from qpid/java/client/src/test/java/org/apache/qpid/test/unit/jndi/JNDIPropertyFileTest.java) | 64 | ||||
-rw-r--r-- | qpid/java/client/src/test/java/org/apache/qpid/jndi/hello.properties | 27 |
4 files changed, 114 insertions, 39 deletions
diff --git a/qpid/java/client/src/main/java/org/apache/qpid/jndi/PropertiesFileInitialContextFactory.java b/qpid/java/client/src/main/java/org/apache/qpid/jndi/PropertiesFileInitialContextFactory.java index bc3f89849e..9b202a13ee 100644 --- a/qpid/java/client/src/main/java/org/apache/qpid/jndi/PropertiesFileInitialContextFactory.java +++ b/qpid/java/client/src/main/java/org/apache/qpid/jndi/PropertiesFileInitialContextFactory.java @@ -20,8 +20,26 @@ */ package org.apache.qpid.jndi; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Hashtable; +import java.util.Iterator; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.ConcurrentHashMap; + +import javax.jms.ConnectionFactory; +import javax.jms.Destination; +import javax.jms.Queue; +import javax.jms.Topic; +import javax.naming.ConfigurationException; +import javax.naming.Context; +import javax.naming.NamingException; +import javax.naming.spi.InitialContextFactory; import org.apache.qpid.client.AMQConnectionFactory; import org.apache.qpid.client.AMQDestination; @@ -33,23 +51,10 @@ import org.apache.qpid.framing.AMQShortString; import org.apache.qpid.url.BindingURL; import org.apache.qpid.url.URLSyntaxException; import org.apache.qpid.util.Strings; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + -import javax.jms.ConnectionFactory; -import javax.jms.Destination; -import javax.jms.Queue; -import javax.jms.Topic; -import javax.naming.ConfigurationException; -import javax.naming.Context; -import javax.naming.NamingException; -import javax.naming.spi.InitialContextFactory; -import java.io.BufferedInputStream; -import java.io.FileInputStream; -import java.io.IOException; -import java.util.Hashtable; -import java.util.Iterator; -import java.util.Map; -import java.util.Properties; -import java.util.concurrent.ConcurrentHashMap; public class PropertiesFileInitialContextFactory implements InitialContextFactory { @@ -60,6 +65,7 @@ public class PropertiesFileInitialContextFactory implements InitialContextFactor private String QUEUE_PREFIX = "queue."; private String TOPIC_PREFIX = "topic."; + @SuppressWarnings({ "rawtypes", "unchecked" }) public Context getInitialContext(Hashtable environment) throws NamingException { Map data = new ConcurrentHashMap(); @@ -68,6 +74,7 @@ public class PropertiesFileInitialContextFactory implements InitialContextFactor { String file = null; + if (environment.containsKey(Context.PROVIDER_URL)) { file = (String) environment.get(Context.PROVIDER_URL); @@ -77,13 +84,23 @@ public class PropertiesFileInitialContextFactory implements InitialContextFactor file = System.getProperty(Context.PROVIDER_URL); } + // Load the properties specified if (file != null) { _logger.info("Loading Properties from:" + file); + BufferedInputStream inputStream = null; - // Load the properties specified - BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file)); + if(file.contains("file:")) + { + inputStream = new BufferedInputStream(new FileInputStream(new File(new URI(file)))); + } + else + { + inputStream = new BufferedInputStream(new FileInputStream(file)); + } + Properties p = new Properties(); + try { p.load(inputStream); @@ -119,6 +136,11 @@ public class PropertiesFileInitialContextFactory implements InitialContextFactor _logger.warn("Unable to load property file specified in Provider_URL:" + environment.get(Context.PROVIDER_URL) +"\n" + "Due to:"+ioe.getMessage()); } + catch(URISyntaxException uoe) + { + _logger.warn("Unable to load property file specified in Provider_URL:" + environment.get(Context.PROVIDER_URL) +"\n" + + "Due to:"+uoe.getMessage()); + } createConnectionFactories(data, environment); diff --git a/qpid/java/client/src/test/java/org/apache/qpid/test/unit/jndi/JNDITest.properties b/qpid/java/client/src/test/java/org/apache/qpid/jndi/JNDITest.properties index 07017a05a6..07017a05a6 100644 --- a/qpid/java/client/src/test/java/org/apache/qpid/test/unit/jndi/JNDITest.properties +++ b/qpid/java/client/src/test/java/org/apache/qpid/jndi/JNDITest.properties diff --git a/qpid/java/client/src/test/java/org/apache/qpid/test/unit/jndi/JNDIPropertyFileTest.java b/qpid/java/client/src/test/java/org/apache/qpid/jndi/PropertiesFileInitialContextFactoryTest.java index 576ab4fa05..4371f8d753 100644 --- a/qpid/java/client/src/test/java/org/apache/qpid/test/unit/jndi/JNDIPropertyFileTest.java +++ b/qpid/java/client/src/test/java/org/apache/qpid/jndi/PropertiesFileInitialContextFactoryTest.java @@ -14,29 +14,35 @@ * "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. + * under the License. + * * - * */ -package org.apache.qpid.test.unit.jndi; +package org.apache.qpid.jndi; -import junit.framework.TestCase; -import org.apache.qpid.client.AMQDestination; -import org.apache.qpid.framing.AMQShortString; +import java.util.Properties; +import javax.jms.Destination; import javax.jms.Queue; import javax.jms.Topic; import javax.naming.ConfigurationException; import javax.naming.Context; import javax.naming.InitialContext; -import java.util.Properties; -public class JNDIPropertyFileTest extends TestCase +import junit.framework.TestCase; + +import org.apache.qpid.client.AMQDestination; +import org.apache.qpid.framing.AMQShortString; + +public class PropertiesFileInitialContextFactoryTest extends TestCase { + private static final String FILE_URL_PATH = System.getProperty("user.dir") + "/client/src/test/java/org/apache/qpid/jndi/"; + private static final String FILE_NAME = "hello.properties"; + private Context ctx; - - public JNDIPropertyFileTest() throws Exception + + protected void setUp() throws Exception { Properties properties = new Properties(); properties.load(this.getClass().getResourceAsStream("JNDITest.properties")); @@ -44,19 +50,39 @@ public class JNDIPropertyFileTest extends TestCase //Create the initial context ctx = new InitialContext(properties); } - + + public void testInitialContextProviderURL() throws Exception + { + Destination d = null; + + System.setProperty("java.naming.factory.initial", "org.apache.qpid.jndi.PropertiesFileInitialContextFactory"); + System.setProperty("java.naming.provider.url", FILE_URL_PATH + FILE_NAME); + + InitialContext ctx = new InitialContext(); + d = (Destination)ctx.lookup("topicExchange"); + assertNotNull("Lookup for Destination from file path should not be null", d); + + ctx.close(); + + System.setProperty("java.naming.provider.url", "file:///" + FILE_URL_PATH + FILE_NAME); + + ctx = new InitialContext(); + d = (Destination)ctx.lookup("topicExchange"); + assertNotNull("Lookup for Destination from file URI should not be null", d); + } + public void testQueueNamesWithTrailingSpaces() throws Exception { Queue queue = (Queue)ctx.lookup("QueueNameWithSpace"); - assertEquals("QueueNameWithSpace",queue.getQueueName()); + assertEquals("QueueNameWithSpace",queue.getQueueName()); } - + public void testTopicNamesWithTrailingSpaces() throws Exception { Topic topic = (Topic)ctx.lookup("TopicNameWithSpace"); - assertEquals("TopicNameWithSpace",topic.getTopicName()); + assertEquals("TopicNameWithSpace",topic.getTopicName()); } - + public void testMultipleTopicNamesWithTrailingSpaces() throws Exception { Topic topic = (Topic)ctx.lookup("MultipleTopicNamesWithSpace"); @@ -64,16 +90,16 @@ public class JNDIPropertyFileTest extends TestCase for (AMQShortString bindingKey: ((AMQDestination)topic).getBindingKeys()) { i++; - assertEquals("Topic" + i + "WithSpace",bindingKey.asString()); + assertEquals("Topic" + i + "WithSpace",bindingKey.asString()); } } - + public void testConfigurationErrors() throws Exception { Properties properties = new Properties(); properties.put("java.naming.factory.initial", "org.apache.qpid.jndi.PropertiesFileInitialContextFactory"); properties.put("destination.my-queue","amq.topic/test;create:always}"); - + try { ctx = new InitialContext(properties); @@ -83,6 +109,6 @@ public class JNDIPropertyFileTest extends TestCase { assertTrue("Incorrect exception", e.getMessage().contains("Failed to parse entry: amq.topic/test;create:always}")); } - + } } diff --git a/qpid/java/client/src/test/java/org/apache/qpid/jndi/hello.properties b/qpid/java/client/src/test/java/org/apache/qpid/jndi/hello.properties new file mode 100644 index 0000000000..d017d137fe --- /dev/null +++ b/qpid/java/client/src/test/java/org/apache/qpid/jndi/hello.properties @@ -0,0 +1,27 @@ +# +# 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. +# +java.naming.factory.initial = org.apache.qpid.jndi.PropertiesFileInitialContextFactory + +# register some connection factories +# connectionfactory.[jndiname] = [ConnectionURL] +connectionfactory.qpidConnectionfactory = amqp://guest:guest@clientid/test?brokerlist='tcp://10.0.1.46:5672' + +# Register an AMQP destination in JNDI +# destination.[jniName] = [Address Format] +destination.topicExchange = amq.topic |