jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r828957 - /jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java
Date Fri, 23 Oct 2009 08:17:33 GMT
Author: jukka
Date: Fri Oct 23 08:17:33 2009
New Revision: 828957

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

Committed the proposed patch.

Modified:
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java

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=828957&r1=828956&r2=828957&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 08:17:33 2009
@@ -16,11 +16,21 @@
  */
 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;
+
+import javax.imageio.spi.ServiceRegistry;
 import javax.jcr.Node;
 import javax.jcr.NodeIterator;
 import javax.jcr.Property;
 import javax.jcr.PropertyIterator;
+import javax.jcr.Repository;
 import javax.jcr.RepositoryException;
+import javax.jcr.RepositoryFactory;
 import javax.jcr.query.QueryResult;
 import javax.jcr.query.Row;
 import javax.jcr.query.RowIterator;
@@ -43,6 +53,139 @@
     }
 
     /**
+     * Looks up the available {@link RepositoryFactory repository factories}
+     * and returns the {@link Repository repository} that one of the factories
+     * returns for the given settings.
+     *
+     * @see RepositoryFactory#getRepository(Map)
+     * @param parameters repository settings
+     * @return repository reference
+     * @throws RepositoryException if the repository can not be accessed,
+     *                             or if an appropriate repository factory
+     *                             is not available
+     */
+    public static Repository getRepository(Map<String, String> parameters)
+            throws RepositoryException {
+        Iterator<RepositoryFactory> iterator =
+            ServiceRegistry.lookupProviders(RepositoryFactory.class);
+
+        while (iterator.hasNext()) {
+            RepositoryFactory factory = iterator.next();
+            Repository repository = factory.getRepository(parameters);
+            if (repository != null) {
+                return repository;
+            }
+        }
+
+        throw new RepositoryException(
+                "No repository factory can handle the given configuration: "
+                + parameters);
+    }
+
+    /**
+     * Returns the repository identified by the given URI.
+     * <p>
+     * The currently supported URI types are:
+     * <dl>
+     *   <dt>http(s)://...</dt>
+     *   <dd>
+     *     A remote repository connection using SPI2DAVex with the given URL.
+     *   </dd>
+     *   <dt>file://...</dt>
+     *   <dd>
+     *     An embedded Jackrabbit repository located in the given directory.
+     *   </dd>
+     *   <dt>jndi:...</dt>
+     *   <dd>
+     *     JNDI lookup for a repository stored under the given name in the
+     *     default JNDI context.
+     *   </dd>
+     * </dl>
+     * <p>
+     *
+     * @param uri repository URI
+     * @return repository instance
+     * @throws RepositoryException if the repository can not be accessed,
+     *                             or if the given URI is unknown or invalid
+     */
+    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);
+    }
+
+    /**
      * Calls {@link Node#getSharedSet()} on the given node and returns
      * the resulting {@link NodeIterator} as an {@link Iterable<Node>} instance
      * for use in a Java 5 for-each loop.



Mime
View raw message