You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2008/03/12 08:45:17 UTC

svn commit: r636239 - /incubator/sling/trunk/jcr/base/src/main/java/org/apache/sling/jcr/base/util/RepositoryAccessor.java

Author: fmeschbe
Date: Wed Mar 12 00:45:09 2008
New Revision: 636239

URL: http://svn.apache.org/viewvc?rev=636239&view=rev
Log:
SLING-223 Provide Jackrabbit API to remote repository accessed over RMI

Modified:
    incubator/sling/trunk/jcr/base/src/main/java/org/apache/sling/jcr/base/util/RepositoryAccessor.java

Modified: incubator/sling/trunk/jcr/base/src/main/java/org/apache/sling/jcr/base/util/RepositoryAccessor.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/base/src/main/java/org/apache/sling/jcr/base/util/RepositoryAccessor.java?rev=636239&r1=636238&r2=636239&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/base/src/main/java/org/apache/sling/jcr/base/util/RepositoryAccessor.java (original)
+++ incubator/sling/trunk/jcr/base/src/main/java/org/apache/sling/jcr/base/util/RepositoryAccessor.java Wed Mar 12 00:45:09 2008
@@ -23,142 +23,190 @@
 import javax.jcr.Repository;
 import javax.naming.InitialContext;
 
-import org.apache.jackrabbit.rmi.client.ClientAdapterFactory;
 import org.apache.jackrabbit.rmi.client.ClientRepositoryFactory;
 import org.apache.jackrabbit.rmi.client.LocalAdapterFactory;
+import org.apache.jackrabbit.rmi.jackrabbit.JackrabbitClientAdapterFactory;
 import org.apache.jackrabbit.rmi.remote.RemoteRepository;
 import org.apache.sling.api.SlingException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /** Access a Repository via JNDI or RMI. */
- public class RepositoryAccessor {
+public class RepositoryAccessor {
     private final Logger log = LoggerFactory.getLogger(getClass());
-    
+
     /** Prefix for RMI Repository URLs */
     public static final String RMI_PREFIX = "rmi://";
-    
+
     /** Prefix for JNDI Repository URLs */
     public static final String JNDI_PREFIX = "jndi://";
-    
-    /** Name of the property that the jcr client and server bundles
-     *  to override their default configuration settings and connect
-     *  to the specified repository instead (SLING-254 and SLING-260)
+
+    /**
+     * Name of the property that the jcr client and server bundles to override
+     * their default configuration settings and connect to the specified
+     * repository instead (SLING-254 and SLING-260)
      */
     public static final String REPOSITORY_URL_OVERRIDE_PROPERTY = "sling.repository.url";
- 
+
     public static class RepositoryUrlException extends SlingException {
         RepositoryUrlException(String reason) {
             super(reason);
         }
     }
-    
-    /** First try to access the Repository via JNDI (unless jndiContext is null), and if
-     *  not successful try RMI.
-     *  
-     * @param repositoryName JNDI name or RMI URL (must start with "rmi://") of the Repository
+
+    /**
+     * First try to access the Repository via JNDI (unless jndiContext is null),
+     * and if not successful try RMI.
+     * 
+     * @param repositoryName JNDI name or RMI URL (must start with "rmi://") of
+     *            the Repository
      * @param jndiContext if null, JNDI is not tried
      * @return a Repository, or null if not found
      */
-    public Repository getRepository(String repositoryName, Hashtable<String, Object> jndiContext) {
-        
+    public Repository getRepository(String repositoryName,
+            Hashtable<String, Object> jndiContext) {
+
         Repository result = null;
         String tried = "";
-        
-        if(jndiContext == null || jndiContext.size() == 0) {
+
+        if (jndiContext == null || jndiContext.size() == 0) {
             log.info("jndiContext is null or empty, not trying JNDI");
         } else {
-            log.debug("Trying to acquire Repository '" + repositoryName + "' via JNDI, context=" + jndiContext);
+            log.debug("Trying to acquire Repository '" + repositoryName
+                + "' via JNDI, context=" + jndiContext);
             tried += "JNDI ";
             final ClassLoader old = Thread.currentThread().getContextClassLoader();
             try {
-                Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
+                Thread.currentThread().setContextClassLoader(
+                    this.getClass().getClassLoader());
                 InitialContext initialContext = new InitialContext(jndiContext);
                 Object repoObject = initialContext.lookup(repositoryName);
                 if (repoObject instanceof Repository) {
                     result = (Repository) repoObject;
-                    log.info("Acquired Repository '" + repositoryName + "' via JNDI");
+                    log.info("Acquired Repository '" + repositoryName
+                        + "' via JNDI");
 
                 } else if (repoObject instanceof RemoteRepository) {
                     RemoteRepository remoteRepo = (RemoteRepository) repoObject;
-                    LocalAdapterFactory laf = new ClientAdapterFactory();
+                    LocalAdapterFactory laf = getLocalAdapterFactory();
                     result = laf.getRepository(remoteRepo);
-                    log.info("Acquired RemoteRepository '" + repositoryName + "' via JNDI");
-                    
+                    log.info("Acquired RemoteRepository '" + repositoryName
+                        + "' via JNDI");
+
                 } else {
-                    log.info(
-                        "Repository '" + repositoryName + "' acquired via JDNI " 
+                    log.info("Repository '" + repositoryName
+                        + "' acquired via JDNI "
                         + "does not implement the required interfaces, class="
-                        + repoObject.getClass().getName()
-                    );
+                        + repoObject.getClass().getName());
                 }
-                
+
             } catch (Throwable t) {
-                log.info("Unable to acquire Repository '" + repositoryName + "' via JNDI, context=" + jndiContext, t);
+                log.info("Unable to acquire Repository '" + repositoryName
+                    + "' via JNDI, context=" + jndiContext, t);
             } finally {
                 Thread.currentThread().setContextClassLoader(old);
             }
         }
 
-        if(result == null) {
-            if(repositoryName==null || !repositoryName.startsWith(RMI_PREFIX)) {
-                log.info("Repository name does not start with '" + RMI_PREFIX + "', not trying RMI");
+        if (result == null) {
+            if (repositoryName == null
+                || !repositoryName.startsWith(RMI_PREFIX)) {
+                log.info("Repository name does not start with '" + RMI_PREFIX
+                    + "', not trying RMI");
             } else {
                 try {
                     tried += "RMI ";
-                    log.debug("Trying to acquire Repository '" + repositoryName + "' via RMI");
-                    ClientRepositoryFactory crf = new ClientRepositoryFactory();
+                    log.debug("Trying to acquire Repository '" + repositoryName
+                        + "' via RMI");
+                    ClientRepositoryFactory crf = getClientRepositoryFactory();
                     result = crf.getRepository(repositoryName);
-                    log.info("Acquired Repository '" + repositoryName + "' via RMI");
+                    log.info("Acquired Repository '" + repositoryName
+                        + "' via RMI");
                 } catch (Throwable t) {
-                    log.info("Unable to acquire Repository '" + repositoryName + "' via RMI", t);
+                    log.info("Unable to acquire Repository '" + repositoryName
+                        + "' via RMI", t);
                 }
             }
         }
-        
-        if(result == null) {
-            log.info("Unable to acquire Repository '" + repositoryName + "', tried " + tried);
+
+        if (result == null) {
+            log.info("Unable to acquire Repository '" + repositoryName
+                + "', tried " + tried);
         }
-        
+
         return result;
     }
-    
-    /** Acquire a Repository from the given URL
-     *  @param url for RMI, an RMI URL. For JNDI, "jndi://", followed by the JNDI repository name,
-     *  followed by a colon and a comma-separated list of JNDI context values, for example:
-     *  <pre>
+
+    /**
+     * Acquire a Repository from the given URL
+     * 
+     * @param url for RMI, an RMI URL. For JNDI, "jndi://", followed by the JNDI
+     *            repository name, followed by a colon and a comma-separated
+     *            list of JNDI context values, for example:
+     * 
+     * <pre>
      *      jndi://jackrabbit:java.naming.factory.initial=org.SomeClass,java.naming.provider.url=http://foo.com
-     *  </pre>  
+     * </pre>
      */
-    public Repository getRepositoryFromURL(String url) throws RepositoryUrlException {
-        if(url == null) {
+    public Repository getRepositoryFromURL(String url)
+            throws RepositoryUrlException {
+        if (url == null) {
             throw new RepositoryUrlException("null URL");
         }
-        
-        if(url.startsWith(JNDI_PREFIX)) {
+
+        if (url.startsWith(JNDI_PREFIX)) {
             // Parse JNDI URL to extract repository name and context
             String name = null;
-            final Hashtable<String, Object> jndiContext = new Hashtable <String, Object> ();
-            final String urlNoPrefix = url.substring(JNDI_PREFIX.length()); 
+            final Hashtable<String, Object> jndiContext = new Hashtable<String, Object>();
+            final String urlNoPrefix = url.substring(JNDI_PREFIX.length());
             final int colonPos = urlNoPrefix.indexOf(':');
-            if(colonPos < 0) {
+            if (colonPos < 0) {
                 name = urlNoPrefix;
             } else {
                 name = urlNoPrefix.substring(0, colonPos);
-                for(String entryStr : urlNoPrefix.substring(colonPos + 1).split(",")) {
-                    final String [] entry = entryStr.split("=");
-                    if(entry.length == 2) {
+                for (String entryStr : urlNoPrefix.substring(colonPos + 1).split(
+                    ",")) {
+                    final String[] entry = entryStr.split("=");
+                    if (entry.length == 2) {
                         jndiContext.put(entry[0], entry[1]);
                     }
                 }
             }
-            
+
             return getRepository(name, jndiContext);
-            
+
         } else {
-            
+
             // Use URL as is
             return getRepository(url, null);
         }
+    }
+
+    /**
+     * Returns the <code>LocalAdapterFactory</code> used to convert Jackrabbit
+     * JCR RMI remote objects to local JCR API objects.
+     * <p>
+     * This method returns an instance of the
+     * <code>JackrabbitClientAdapterFactory</code> which allows accessing
+     * Jackrabbit (or Jackrabbit-based) repositories over RMI. Extensions of
+     * this class may overwrite this method to use a different implementation.
+     */
+    protected LocalAdapterFactory getLocalAdapterFactory() {
+        return new JackrabbitClientAdapterFactory();
+    }
+
+    /**
+     * Returns the <code>ClientRepositoryFactory</code> to access the remote
+     * JCR repository over RMI.
+     * <p>
+     * This method creates an instance of the
+     * <code>ClientRepositoryFactory</code> class initialized with the
+     * <code>LocalAdapterFactory</code> returned from the
+     * {@link #getLocalAdapterFactory()} method. Extensions may overwrite this
+     * method to return an extension of the Jackrabbit JCR RMI
+     * <code>ClientRepositoryFactory</code> class.
+     */
+    protected ClientRepositoryFactory getClientRepositoryFactory() {
+        return new ClientRepositoryFactory(getLocalAdapterFactory());
     }
 }