jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r828993 - in /jackrabbit/trunk: jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jndi/ jackrabbit-core/src/main/resources/META-INF/services/ jackrabbit-core/src/test/java/org/apache/jackrabbit/core/integration/ jackrabbit-jcr-common...
Date Fri, 23 Oct 2009 10:41:38 GMT
Author: jukka
Date: Fri Oct 23 10:41:37 2009
New Revision: 828993

URL: http://svn.apache.org/viewvc?rev=828993&view=rev
Log:
JCR-2360: JcrUtils.getRepository(...) for simple repository access

Added a o.a.j.commons.GenericRepositoryFactory class that implements the repository URI and
JNDI lookup mechanisms. Dropped the o.a.j.core.jndi.RepositoryFactoryImpl class whose functionality
is now merged to GenericRepositoryFactory.

Adapted the JcrUtils.getRepository(String uri) method to use GenericRepositoryFactory through
the standard RepositoryFactory mechanism instead of directly implementing the URI deconstruction
mechanism. This way other RepositoryFactory implementations can also take a shot at processing
the org.apache.jackrabbit.repository.uri parameter.

Added a JcrUtils.getRepository() method that returns the default repository. Adapted jackrabbit-core
tests to use JcrUtils.

Added:
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/GenericRepositoryFactory.java
  (with props)
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/resources/META-INF/services/javax.jcr.RepositoryFactory
Removed:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jndi/RepositoryFactoryImpl.java
Modified:
    jackrabbit/trunk/jackrabbit-core/src/main/resources/META-INF/services/javax.jcr.RepositoryFactory
    jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/integration/RepositoryFactoryImplTest.java
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java

Modified: jackrabbit/trunk/jackrabbit-core/src/main/resources/META-INF/services/javax.jcr.RepositoryFactory
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/resources/META-INF/services/javax.jcr.RepositoryFactory?rev=828993&r1=828992&r2=828993&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/resources/META-INF/services/javax.jcr.RepositoryFactory
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/resources/META-INF/services/javax.jcr.RepositoryFactory
Fri Oct 23 10:41:37 2009
@@ -18,4 +18,3 @@
 #
 
 org.apache.jackrabbit.core.RepositoryFactoryImpl
-org.apache.jackrabbit.core.jndi.RepositoryFactoryImpl

Modified: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/integration/RepositoryFactoryImplTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/integration/RepositoryFactoryImplTest.java?rev=828993&r1=828992&r2=828993&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/integration/RepositoryFactoryImplTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/integration/RepositoryFactoryImplTest.java
Fri Oct 23 10:41:37 2009
@@ -20,16 +20,16 @@
 import java.util.Map;
 import java.util.HashMap;
 import java.util.Hashtable;
-import java.util.Iterator;
 
 import javax.jcr.RepositoryException;
 import javax.jcr.Repository;
 import javax.naming.InitialContext;
 import javax.naming.Context;
-import javax.imageio.spi.ServiceRegistry;
 
 import javax.jcr.RepositoryFactory;
 import org.apache.jackrabbit.api.JackrabbitRepository;
+import org.apache.jackrabbit.commons.GenericRepositoryFactory;
+import org.apache.jackrabbit.commons.JcrUtils;
 import org.apache.jackrabbit.core.RepositoryFactoryImpl;
 import org.apache.jackrabbit.core.jndi.provider.DummyInitialContextFactory;
 import org.apache.jackrabbit.core.jndi.RegistryHelper;
@@ -66,7 +66,7 @@
      */
     public void testDefaultRepository() throws RepositoryException {
         setProperties();
-        repo = RepositoryManager.getRepository(null);
+        repo = JcrUtils.getRepository();
         checkRepository(repo);
     }
 
@@ -77,7 +77,7 @@
      */
     public void testWithParameters() throws RepositoryException {
         resetProperties();
-        repo = RepositoryManager.getRepository(getParamters());
+        repo = JcrUtils.getRepository(getParamters());
         checkRepository(repo);
     }
 
@@ -89,9 +89,9 @@
      */
     public void testMultipleConnect() throws RepositoryException {
         setProperties();
-        repo = RepositoryManager.getRepository(null);
+        repo = JcrUtils.getRepository();
         checkRepository(repo);
-        assertSame(repo, RepositoryManager.getRepository(null));
+        assertSame(repo, JcrUtils.getRepository());
     }
 
     /**
@@ -102,9 +102,9 @@
      */
     public void testMultipleConnectWithParameters() throws RepositoryException {
         resetProperties();
-        repo = RepositoryManager.getRepository(getParamters());
+        repo = JcrUtils.getRepository(getParamters());
         checkRepository(repo);
-        assertSame(repo, RepositoryManager.getRepository(getParamters()));
+        assertSame(repo, JcrUtils.getRepository(getParamters()));
     }
 
     /**
@@ -117,7 +117,7 @@
     public void testShutdown() throws RepositoryException {
         setProperties();
         for (int i = 0; i < 2; i++) {
-            repo = RepositoryManager.getRepository(null);
+            repo = JcrUtils.getRepository();
             checkRepository(repo);
             ((JackrabbitRepository) repo).shutdown();
         }
@@ -131,15 +131,15 @@
      */
     public void testJNDI() throws Exception {
         String name = "jackrabbit-repository";
-        Map parameters = new HashMap();
+        Map<String, String> parameters = new HashMap<String, String>();
         parameters.put(Context.INITIAL_CONTEXT_FACTORY, DummyInitialContextFactory.class.getName());
         parameters.put(Context.PROVIDER_URL, "localhost");
-        InitialContext context = new InitialContext(new Hashtable(parameters));
+        InitialContext context = new InitialContext(new Hashtable<String, String>(parameters));
         RegistryHelper.registerRepository(context, name,
                 REPO_CONF.getAbsolutePath(), REPO_HOME.getAbsolutePath(), false);
         try {
-            parameters.put(org.apache.jackrabbit.core.jndi.RepositoryFactoryImpl.REPOSITORY_JNDI_NAME,
name);
-            repo = RepositoryManager.getRepository(parameters);
+            parameters.put(GenericRepositoryFactory.JNDI_NAME, name);
+            repo = JcrUtils.getRepository(parameters);
             checkRepository(repo);
         } finally {
             RegistryHelper.unregisterRepository(context, name);
@@ -162,27 +162,11 @@
         System.setProperty(RepositoryFactoryImpl.REPOSITORY_CONF, "");
     }
 
-    private static Map getParamters() {
-        Map params = new HashMap();
+    private static Map<String, String> getParamters() {
+        Map<String, String> params = new HashMap<String, String>();
         params.put(RepositoryFactoryImpl.REPOSITORY_HOME, REPO_HOME.getAbsolutePath());
         params.put(RepositoryFactoryImpl.REPOSITORY_CONF, REPO_CONF.getAbsolutePath());
         return params;
     }
 
-    private static final class RepositoryManager {
-
-        private static Repository getRepository(Map parameters)
-                throws RepositoryException {
-            Repository repo = null;
-            Iterator factories = ServiceRegistry.lookupProviders(RepositoryFactory.class);
-            while (factories.hasNext()) {
-                RepositoryFactory factory = (RepositoryFactory) factories.next();
-                repo = factory.getRepository(parameters);
-                if (repo != null) {
-                    break;
-                }
-            }
-            return repo;
-        }
-    }
 }

Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/GenericRepositoryFactory.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/GenericRepositoryFactory.java?rev=828993&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/GenericRepositoryFactory.java
(added)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/GenericRepositoryFactory.java
Fri Oct 23 10:41:37 2009
@@ -0,0 +1,174 @@
+/*
+ * 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.io.File;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Map;
+
+import javax.jcr.Repository;
+import javax.jcr.RepositoryException;
+import javax.jcr.RepositoryFactory;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+
+/**
+ * Generic JCR repository factory. This factory knows how to handle
+ * parameter maps containing the following keys:
+ * <dl>
+ *   <dt>{@link #JNDI_NAME org.apache.jackrabbit.repository.jndi.name}</dt>
+ *   <dd>
+ *     The repository instance is looked up from JNDI.
+ *     An {@link InitialContext initial JNDI context} is constructed using
+ *     all the other parameters, and a repository at the given name is
+ *     looked up from the instantiated context.
+ *   </dd>
+ *   <dt>{@link #URI org.apache.jackrabbit.repository.uri}</dt>
+ *   <dd>
+ *     Connects to the given repository URI.
+ *     See {@link JcrUtils#getRepository(String)} for the supported URI types.
+ *     Note that this class does not directly implement all these connection
+ *     mechanisms. Instead it maps the given repository URI to known
+ *     {@link RepositoryFactory} parameters and uses the Java Service
+ *     Provider mechanism to recursively ask all the available repository
+ *     factories to handle the resulting connection parameters. All the other
+ *     parameters except the repository URI from the original invocation are
+ *     also passed on to these recursive calls.
+ *   </dd>
+ * </dl>
+ *
+ * @since Apache Jackrabbit 2.0
+ */
+@SuppressWarnings("unchecked")
+public class GenericRepositoryFactory implements RepositoryFactory {
+
+    /**
+     * The repository URI parameter name.
+     */
+    public static final String URI =
+        "org.apache.jackrabbit.repository.uri";
+
+    /**
+     * The JNDI name parameter name.
+     */
+    public static final String JNDI_NAME =
+        "org.apache.jackrabbit.repository.jndi.name";
+
+    /**
+     * Handles the generic repository parameters mentioned in the
+     * description of this class. Returns <code>null</code> if none of
+     * the described parameters are given or if the given parameter map is
+     * <code>null</code>.
+     */
+    public Repository getRepository(Map parameters)
+            throws RepositoryException {
+        if (parameters == null) {
+            return null;
+        } else if (parameters.containsKey(URI)) {
+            return getUriRepository(parameters);
+        } else if (parameters.containsKey(JNDI_NAME)) {
+            return getJndiRepository(parameters);
+        } else {
+            return null;
+        }
+    }
+
+    /**
+     * Implements the repository URI connection as documented in the
+     * description of this class.
+     *
+     * @param original original repository parameters, including {@link #URI}
+     * @return repository instance
+     * @throws RepositoryException if the repository can not be accessed,
+     *                             or if the URI is invalid or unknown
+     */
+    private Repository getUriRepository(Map original)
+            throws RepositoryException {
+        Map parameters = new HashMap(original);
+        String parameter = parameters.remove(URI).toString().trim();
+
+        try {
+            URI uri = new URI(parameter);
+            String scheme = uri.getScheme();
+            if ("jndi".equalsIgnoreCase(scheme)) {
+                parameters.put(JNDI_NAME, uri.getSchemeSpecificPart());
+            } else if ("file".equalsIgnoreCase(scheme)) {
+                File file = new File(uri);
+                String home, conf;
+                if (file.isFile()) {
+                    home = file.getParentFile().getPath();
+                    conf = file.getPath();
+                } else {
+                    home = file.getPath();
+                    conf = new File(file, "repository.xml").getPath();
+                }
+                parameters.put("org.apache.jackrabbit.repository.home", home);
+                parameters.put("org.apache.jackrabbit.repository.conf", conf);
+            } else if ("http".equalsIgnoreCase(scheme)
+                    || "https".equalsIgnoreCase(scheme)){
+                parameters.put(
+                        "org.apache.jackrabbit.spi.RepositoryServiceFactory",
+                        "org.apache.jackrabbit.spi2davex.Spi2davexRepositoryServiceFactory");
+                parameters.put(
+                        "org.apache.jackrabbit.spi2davex.uri", parameter);
+            } else {
+                throw new RepositoryException(
+                        "Unsupported repository URI: " + parameter);
+            }
+        } catch (URISyntaxException e) {
+            throw new RepositoryException(
+                    "Invalid repository URI: " + parameter, e);
+        }
+
+        return JcrUtils.getRepository(parameters);
+    }
+
+    /**
+     * Implements the JNDI lookup as documented in the description of
+     * this class.
+     *
+     * @param parameters repository parameters, including {@link #JNDI_NAME}
+     * @return the repository instance from JNDI
+     * @throws RepositoryException if the name is not found in JNDI
+     *                             or does not point to a repository instance
+     */
+    private Repository getJndiRepository(Map parameters)
+            throws RepositoryException {
+        Hashtable environment = new Hashtable(parameters);
+        String name = environment.remove(JNDI_NAME).toString();
+
+        try {
+            Object value = new InitialContext(environment).lookup(name);
+            if (value instanceof Repository) {
+                return (Repository) value;
+            } else {
+                throw new RepositoryException(
+                        "Invalid repository object " + value
+                        + " found at " + name + " in JNDI environment "
+                        + environment);
+            }
+        } catch (NamingException e) {
+            throw new RepositoryException(
+                    "Failed to look up " + name
+                    + " from JNDI environment " + environment, e);
+        }
+    }
+
+}

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

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=828993&r1=828992&r2=828993&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
Fri Oct 23 10:41:37 2009
@@ -16,9 +16,6 @@
  */
 package org.apache.jackrabbit.commons;
 
-import java.io.File;
-import java.net.URI;
-import java.net.URISyntaxException;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
@@ -53,11 +50,28 @@
     }
 
     /**
+     * Returns the default repository of the current environment.
+     * Implemented by calling {@link #getRepository(Map)} with a
+     * <code>null</code> parameter map.
+     *
+     * @see RepositoryFactory#getRepository(Map)
+     * @return default repository
+     * @throws RepositoryException if a default repository is not available
+     *                             or can not be accessed
+     */
+    public static Repository getRepository() throws RepositoryException {
+        return getRepository((Map<String, String>) null);
+    }
+
+    /**
      * Looks up the available {@link RepositoryFactory repository factories}
      * and returns the {@link Repository repository} that one of the factories
      * returns for the given settings.
+     * <p>
+     * Note that unlike {@link RepositoryFactory#getRepository(Map)} this
+     * method will throw an exception instead of returning <code>null</code>
+     * if the given parameters can not be interpreted.
      *
-     * @see RepositoryFactory#getRepository(Map)
      * @param parameters repository settings
      * @return repository reference
      * @throws RepositoryException if the repository can not be accessed,
@@ -83,9 +97,8 @@
     }
 
     /**
-     * Returns the repository identified by the given URI.
-     * <p>
-     * The currently supported URI types are:
+     * Returns the repository identified by the given URI. The following
+     * URI types are currently supported:
      * <dl>
      *   <dt>http(s)://...</dt>
      *   <dd>
@@ -97,11 +110,10 @@
      *   </dd>
      *   <dt>jndi:...</dt>
      *   <dd>
-     *     JNDI lookup for a repository stored under the given name in the
-     *     default JNDI context.
-     *   </dd>
+     *     JNDI lookup for the named repository. See the JNDI support
+     *     described above.
+     *  </dd>
      * </dl>
-     * <p>
      *
      * @param uri repository URI
      * @return repository instance
@@ -110,79 +122,9 @@
      */
     public static Repository getRepository(String uri)
             throws RepositoryException {
-        try {
-            URI parsed = new URI(uri.trim());
-            String scheme = parsed.getScheme();
-            if ("jndi".equalsIgnoreCase(scheme)) {
-                Map<String, String> parameters = new HashMap<String, String>();
-                parameters.put(
-                        "org.apache.jackrabbit.repository.jndi.name",
-                        parsed.getSchemeSpecificPart());
-                return getRepository(parameters);
-            } else if ("file".equalsIgnoreCase(scheme)) {
-                return getRepository(new File(parsed));
-            } else if ("http".equalsIgnoreCase(scheme)
-                    || "https".equalsIgnoreCase(scheme)){
-                return getRepository(parsed);
-            } else {
-                throw new RepositoryException(
-                        "Unsupported repository URI: " + uri);
-            }
-        } catch (URISyntaxException e) {
-            throw new RepositoryException("Invalid repository URI: " + uri, e);
-        }
-    }
-
-    /**
-     * Returns an SPI2DAVex client that accesses a repository at the given URI.
-     *
-     * @param uri repository URI
-     * @return SPI2DAVex repository client
-     * @throws RepositoryException if the repository can not be accessed,
-     *                             or if the required SPI2DAVex libraries
-     *                             are not available
-     */
-    private static Repository getRepository(URI uri)
-            throws RepositoryException {
-        Map<String, String> parameters = new HashMap<String, String>();
-        parameters.put(
-                "org.apache.jackrabbit.spi.RepositoryServiceFactory",
-                "org.apache.jackrabbit.spi2davex.Spi2davexRepositoryServiceFactory");
-        parameters.put(
-                "org.apache.jackrabbit.spi2davex.uri", uri.toASCIIString());
-        return getRepository(parameters);
-    }
-
-    /**
-     * Returns an embedded Jackrabbit repository located at the given
-     * file system path. The path can point either to the repository
-     * directory (in which case a "repository.xml" configuration file
-     * is expected to be found inside the directory) or to the repository
-     * configuration file (in which case the repository directory is assumed
-     * to be the directory that contains the configuration file).
-     *
-     * @param file repository path
-     * @return Jackrabbit repository
-     * @throws RepositoryException if the repository can not be accessed,
-     *                             or if the required Jackrabbit libraries
-     *                             are not available
-     */
-    private static Repository getRepository(File file)
-            throws RepositoryException {
         Map<String, String> parameters = new HashMap<String, String>();
-
-        String home, conf;
-        if (file.isFile()) {
-            home = file.getParentFile().getPath();
-            conf = file.getPath();
-        } else {
-            home = file.getPath();
-            conf = new File(file, "repository.xml").getPath();
-        }
-        parameters.put("org.apache.jackrabbit.repository.home", home);
-        parameters.put("org.apache.jackrabbit.repository.conf", conf);
-
-        return getRepository(parameters);
+        parameters.put(GenericRepositoryFactory.URI, uri);
+        return new GenericRepositoryFactory().getRepository(parameters);
     }
 
     /**

Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/resources/META-INF/services/javax.jcr.RepositoryFactory
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/resources/META-INF/services/javax.jcr.RepositoryFactory?rev=828993&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/resources/META-INF/services/javax.jcr.RepositoryFactory
(added)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/resources/META-INF/services/javax.jcr.RepositoryFactory
Fri Oct 23 10:41:37 2009
@@ -0,0 +1,16 @@
+#  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.
+
+org.apache.jackrabbit.commons.GenericRepositoryFactory



Mime
View raw message