You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by kw...@apache.org on 2015/08/20 18:29:52 UTC

svn commit: r1696815 - in /qpid/java/trunk/client/src: main/java/org/apache/qpid/jndi/PropertiesFileInitialContextFactory.java test/java/org/apache/qpid/jndi/PropertiesFileInitialContextFactoryTest.java

Author: kwall
Date: Thu Aug 20 16:29:52 2015
New Revision: 1696815

URL: http://svn.apache.org/r1696815
Log:
QPID-6706: Change PropertiesFileInitialContextFactory to understand schemes other than file:

Modified:
    qpid/java/trunk/client/src/main/java/org/apache/qpid/jndi/PropertiesFileInitialContextFactory.java
    qpid/java/trunk/client/src/test/java/org/apache/qpid/jndi/PropertiesFileInitialContextFactoryTest.java

Modified: qpid/java/trunk/client/src/main/java/org/apache/qpid/jndi/PropertiesFileInitialContextFactory.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/client/src/main/java/org/apache/qpid/jndi/PropertiesFileInitialContextFactory.java?rev=1696815&r1=1696814&r2=1696815&view=diff
==============================================================================
--- qpid/java/trunk/client/src/main/java/org/apache/qpid/jndi/PropertiesFileInitialContextFactory.java (original)
+++ qpid/java/trunk/client/src/main/java/org/apache/qpid/jndi/PropertiesFileInitialContextFactory.java Thu Aug 20 16:29:52 2015
@@ -24,8 +24,9 @@ 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.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
 import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.Map;
@@ -69,18 +70,16 @@ public class PropertiesFileInitialContex
     {
         environment = (environment == null) ? new Hashtable() : environment;
 
-        String fileName = (environment.containsKey(Context.PROVIDER_URL))
+        String providerUrl = (environment.containsKey(Context.PROVIDER_URL))
                 ? (String)environment.get(Context.PROVIDER_URL) : System.getProperty(Context.PROVIDER_URL);
 
-        if (fileName != null)
+        if (providerUrl != null)
         {
-            try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream((fileName.contains("file:"))
-                                                                           ? new File(new URI(fileName))
-                                                                           : new File(fileName))))
+
+            try (BufferedInputStream inputStream = new BufferedInputStream(getProviderUrlInputStream(providerUrl)))
             {
                 // make copy of the original environment to adhere to the Contexts interface
                 environment = new Hashtable(environment);
-                _logger.debug("Attempting to load '{}'", fileName);
 
                 Properties p = new Properties();
                 p.load(inputStream);
@@ -96,16 +95,17 @@ public class PropertiesFileInitialContex
                     environment.put(key, expanded);
                 }
             }
-            catch (IOException | URISyntaxException e)
+            catch (IOException e)
             {
-                NamingException ne = new NamingException("Unable to load property file specified in Provider_URL:" + fileName + ".");
+                NamingException ne = new NamingException("Unable to load property file specified in Provider_URL:" + providerUrl + ".");
                 ne.setRootCause(e);
                 throw ne;
             }
         }
         else
         {
-            _logger.debug("{} was not specified in the context's environment or as a system property.", Context.PROVIDER_URL);
+            _logger.debug("{} was not specified in the context's environment or as a system property.",
+                          Context.PROVIDER_URL);
         }
 
         Map data = new ConcurrentHashMap();
@@ -120,6 +120,21 @@ public class PropertiesFileInitialContex
         return createContext(data, environment);
     }
 
+    private InputStream getProviderUrlInputStream(final String providerUrl) throws IOException
+    {
+        try
+        {
+            URL url = new URL(providerUrl);
+            _logger.debug("Using provider URL : '{}'", url);
+            return url.openStream();
+        }
+        catch (MalformedURLException mue)
+        {
+            _logger.debug("Could not interpret '{}' as a valid URL, loading from file system instead.", providerUrl);
+            return new FileInputStream(new File(providerUrl));
+        }
+    }
+
     // Implementation methods
     // -------------------------------------------------------------------------
     protected ReadOnlyContext createContext(Map data, Hashtable environment)

Modified: qpid/java/trunk/client/src/test/java/org/apache/qpid/jndi/PropertiesFileInitialContextFactoryTest.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/client/src/test/java/org/apache/qpid/jndi/PropertiesFileInitialContextFactoryTest.java?rev=1696815&r1=1696814&r2=1696815&view=diff
==============================================================================
--- qpid/java/trunk/client/src/test/java/org/apache/qpid/jndi/PropertiesFileInitialContextFactoryTest.java (original)
+++ qpid/java/trunk/client/src/test/java/org/apache/qpid/jndi/PropertiesFileInitialContextFactoryTest.java Thu Aug 20 16:29:52 2015
@@ -99,7 +99,7 @@ public class PropertiesFileInitialContex
      * Test loading of a JNDI properties file through use of a file:// URL
      * supplied via the InitialContext.PROVIDER_URL system property.
      */
-    public void testContextFromProviderURL() throws Exception
+    public void testContextFromProviderFileURL() throws Exception
     {
         Properties properties = new Properties();
         properties.put("connectionfactory.qpidConnectionfactory", CONNECTION_URL);
@@ -108,9 +108,7 @@ public class PropertiesFileInitialContex
         File f = File.createTempFile(getTestName(), ".properties");
         try
         {
-            FileOutputStream fos = new FileOutputStream(f);
-            properties.store(fos, null);
-            fos.close();
+            createJndiPropertiesFile(properties, f);
 
             setTestSystemProperty(InitialContext.INITIAL_CONTEXT_FACTORY, "org.apache.qpid.jndi.PropertiesFileInitialContextFactory");
             setTestSystemProperty(InitialContext.PROVIDER_URL,  f.toURI().toURL().toString());
@@ -123,7 +121,33 @@ public class PropertiesFileInitialContex
             ConnectionFactory factory = (ConnectionFactory) context.lookup("qpidConnectionfactory");
             assertTrue("ConnectionFactory was not an instance of AMQConnectionFactory", factory instanceof AMQConnectionFactory);
             assertEquals("Unexpected ConnectionURL value", CONNECTION_URL.replaceAll("password", "********"),
-                        ((AMQConnectionFactory)factory).getConnectionURLString());
+                         ((AMQConnectionFactory) factory).getConnectionURLString());
+
+            context.close();
+        }
+        finally
+        {
+            f.delete();
+        }
+    }
+
+    public void testContextFromFileOnFileSystem() throws Exception
+    {
+        Properties properties = new Properties();
+        properties.put("connectionfactory.qpidConnectionfactory", CONNECTION_URL);
+
+        File f = File.createTempFile(getTestName(), ".properties");
+        try
+        {
+            createJndiPropertiesFile(properties, f);
+
+            setTestSystemProperty(InitialContext.INITIAL_CONTEXT_FACTORY,
+                                  "org.apache.qpid.jndi.PropertiesFileInitialContextFactory");
+            setTestSystemProperty(InitialContext.PROVIDER_URL, f.getAbsolutePath());
+
+            InitialContext context = new InitialContext();
+            ConnectionFactory factory = (ConnectionFactory) context.lookup("qpidConnectionfactory");
+            assertNotNull("Connection factory not present in loaded configuration", factory);
 
             context.close();
         }
@@ -154,4 +178,13 @@ public class PropertiesFileInitialContex
         }
 
     }
+
+    private void createJndiPropertiesFile(final Properties properties, final File f) throws IOException
+    {
+        try(FileOutputStream fos = new FileOutputStream(f))
+        {
+            properties.store(fos, null);
+        }
+    }
+
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org