jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1005449 - in /jackrabbit/trunk/jackrabbit-jcr-commons: ./ src/main/java/org/apache/jackrabbit/commons/ src/test/java/org/apache/jackrabbit/commons/
Date Thu, 07 Oct 2010 13:56:23 GMT
Author: jukka
Date: Thu Oct  7 13:56:23 2010
New Revision: 1005449

URL: http://svn.apache.org/viewvc?rev=1005449&view=rev
Log:
JCR-2771: Simplified Repository URI format for JNDI lookups

Added:
    jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/JcrUtilsTest.java
  (with props)
Modified:
    jackrabbit/trunk/jackrabbit-jcr-commons/pom.xml
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JndiRepositoryFactory.java

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/pom.xml
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/pom.xml?rev=1005449&r1=1005448&r2=1005449&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/pom.xml (original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/pom.xml Thu Oct  7 13:56:23 2010
@@ -71,6 +71,12 @@
       <artifactId>json</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>simple-jndi</groupId>
+      <artifactId>simple-jndi</artifactId>
+      <version>0.11.4.1</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
 </project>

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java?rev=1005449&r1=1005448&r2=1005449&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java
Thu Oct  7 13:56:23 2010
@@ -107,6 +107,23 @@ public class JcrUtils {
      */
     public static Repository getRepository(Map<String, String> parameters)
             throws RepositoryException {
+        String newline = System.getProperty("line.separator");
+
+        // Prepare the potential error message (JCR-2459)
+        StringBuilder log = new StringBuilder("Unable to access a repository");
+        if (parameters != null) {
+            log.append(" with the following settings:");
+            for (Map.Entry<String, String> entry : parameters.entrySet()) {
+                log.append(newline);
+                log.append("    ");
+                log.append(entry.getKey());
+                log.append(": ");
+                log.append(entry.getValue());
+            }
+        } else {
+            log.append(" with the default settings.");
+        }
+
         // Use the query part of a repository URI as additional parameters
         if (parameters != null
                 && parameters.containsKey(JcrUtils.REPOSITORY_URI)) {
@@ -136,29 +153,17 @@ public class JcrUtils {
                     parameters = copy;
                 }
             } catch (URISyntaxException e) {
-                // Ignore invalid URIs
+                log.append(newline);
+                log.append("Note that the given repository URI was invalid:");
+                log.append(newline);
+                log.append("        " + uri);
+                log.append(newline);
+                log.append("        " + e.getMessage());
             } catch (UnsupportedEncodingException e) {
                 throw new RepositoryException("UTF-8 is not supported!", e);
             }
         }
 
-        String newline = System.getProperty("line.separator");
-
-        // Prepare the potential error message (JCR-2459)
-        StringBuilder log = new StringBuilder("Unable to access a repository");
-        if (parameters != null) {
-            log.append(" with the following settings:");
-            for (Map.Entry<String, String> entry : parameters.entrySet()) {
-                log.append(newline);
-                log.append("    ");
-                log.append(entry.getKey());
-                log.append(": ");
-                log.append(entry.getValue());
-            }
-        } else {
-            log.append(" with the default settings.");
-        }
-
         // Iterate through the available RepositoryFactories, with logging
         log.append(newline);
         log.append("The following RepositoryFactory classes were consulted:");

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JndiRepositoryFactory.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JndiRepositoryFactory.java?rev=1005449&r1=1005448&r2=1005449&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JndiRepositoryFactory.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JndiRepositoryFactory.java
Thu Oct  7 13:56:23 2010
@@ -24,6 +24,7 @@ import java.util.Map;
 import javax.jcr.Repository;
 import javax.jcr.RepositoryException;
 import javax.jcr.RepositoryFactory;
+import javax.naming.Context;
 import javax.naming.InitialContext;
 import javax.naming.NamingException;
 
@@ -52,7 +53,7 @@ import javax.naming.NamingException;
  *
  * @since Apache Jackrabbit 2.0
  */
-@SuppressWarnings("unchecked")
+@SuppressWarnings({ "rawtypes", "unchecked" })
 public class JndiRepositoryFactory implements RepositoryFactory {
 
     /**
@@ -75,8 +76,7 @@ public class JndiRepositoryFactory imple
                 try {
                     URI uri = new URI(parameter.toString().trim());
                     if ("jndi".equalsIgnoreCase(uri.getScheme())) {
-                        String name = uri.getSchemeSpecificPart();
-                        return getRepository(name, environment);
+                        return getRepository(uri, environment);
                     } else {
                         return null; // not a jndi: URI
                     }
@@ -89,6 +89,27 @@ public class JndiRepositoryFactory imple
         }
     }
 
+    private Repository getRepository(URI uri, Hashtable environment)
+            throws RepositoryException {
+        String name;
+        if (uri.isOpaque()) {
+            name = uri.getSchemeSpecificPart();
+        } else {
+            name = uri.getPath();
+            if (name == null) {
+                name = "";
+            } else if (name.startsWith("/")) {
+                name = name.substring(1);
+            }
+            String authority = uri.getAuthority();
+            if (authority != null && authority.length() > 0) {
+                environment = new Hashtable(environment);
+                environment.put(Context.INITIAL_CONTEXT_FACTORY, authority);
+            }
+        }
+        return getRepository(name, environment);
+    }
+
     private Repository getRepository(String name, Hashtable environment)
             throws RepositoryException {
         try {

Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/JcrUtilsTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/JcrUtilsTest.java?rev=1005449&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/JcrUtilsTest.java
(added)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/JcrUtilsTest.java
Thu Oct  7 13:56:23 2010
@@ -0,0 +1,62 @@
+/*
+ * 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.
+ */
+package org.apache.jackrabbit.commons;
+
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Map;
+
+import javax.naming.InitialContext;
+
+public class JcrUtilsTest extends MockCase {
+
+    public void testGetRepository() throws Exception {
+        Object repository = record(AbstractRepository.class);
+
+        Hashtable<String, String> environment = new Hashtable<String, String>();
+        environment.put(
+                "java.naming.factory.initial",
+                "org.osjava.sj.memory.MemoryContextFactory");
+        environment.put("org.osjava.sj.jndi.shared", "true");
+        InitialContext context = new InitialContext(environment);
+        context.bind("repository", repository);
+
+        // Test lookup with a traditional map of parameters
+        Map<String, String> parameters = new HashMap<String, String>();
+        parameters.put(
+                "org.apache.jackrabbit.repository.jndi.name", "repository");
+        parameters.put(
+                "java.naming.factory.initial",
+                "org.osjava.sj.memory.MemoryContextFactory");
+        parameters.put("org.osjava.sj.jndi.shared", "true");
+        assertTrue(repository == JcrUtils.getRepository(parameters));
+
+        // Test lookup with URI query parameters
+        assertTrue(repository == JcrUtils.getRepository(
+                "jndi://x"
+                + "?org.apache.jackrabbit.repository.jndi.name=repository"
+                + "&org.osjava.sj.jndi.shared=true"
+                + "&java.naming.factory.initial"
+                + "=org.osjava.sj.memory.MemoryContextFactory"));
+
+        // Test lookup with the custom JNDI URI format (JCR-2771)
+        assertTrue(repository == JcrUtils.getRepository(
+                "jndi://org.osjava.sj.memory.MemoryContextFactory/repository"
+                + "?org.osjava.sj.jndi.shared=true"));
+    }
+
+}

Propchange: jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/JcrUtilsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message