You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by st...@apache.org on 2014/06/11 17:39:09 UTC

svn commit: r1601931 - /sling/trunk/tooling/ide/impl-vlt/src/org/apache/sling/ide/jcr/RepositoryUtils.java

Author: stefanegli
Date: Wed Jun 11 15:39:09 2014
New Revision: 1601931

URL: http://svn.apache.org/r1601931
Log:
SLING-3647 : added a check to see if the repository provider is ready - with the idea of being able to provide a more useful exception message to the user

Modified:
    sling/trunk/tooling/ide/impl-vlt/src/org/apache/sling/ide/jcr/RepositoryUtils.java

Modified: sling/trunk/tooling/ide/impl-vlt/src/org/apache/sling/ide/jcr/RepositoryUtils.java
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/ide/impl-vlt/src/org/apache/sling/ide/jcr/RepositoryUtils.java?rev=1601931&r1=1601930&r2=1601931&view=diff
==============================================================================
--- sling/trunk/tooling/ide/impl-vlt/src/org/apache/sling/ide/jcr/RepositoryUtils.java (original)
+++ sling/trunk/tooling/ide/impl-vlt/src/org/apache/sling/ide/jcr/RepositoryUtils.java Wed Jun 11 15:39:09 2014
@@ -17,7 +17,9 @@
 package org.apache.sling.ide.jcr;
 
 import java.net.URISyntaxException;
+import java.util.Iterator;
 
+import javax.imageio.spi.ServiceRegistry;
 import javax.jcr.Credentials;
 import javax.jcr.Repository;
 import javax.jcr.RepositoryException;
@@ -25,18 +27,45 @@ import javax.jcr.Session;
 import javax.jcr.SimpleCredentials;
 
 import org.apache.jackrabbit.vault.fs.api.RepositoryAddress;
+import org.apache.jackrabbit.vault.fs.api.RepositoryFactory;
 import org.apache.jackrabbit.vault.util.RepositoryProvider;
 import org.apache.sling.ide.transport.RepositoryInfo;
 
 public abstract class RepositoryUtils {
 
+    private static final String REPOSITORY_PROVIDER_NOT_YET_READY_MSG = "Repository provider not yet ready, please retry in a moment";
     private static final RepositoryProvider REPOSITORY_PROVIDER = new RepositoryProvider();
     private static final Object SYNC = new Object();
     private static final String[] WEBDAV_URL_LOCATIONS = new String[] { "server/-/jcr:root", "crx/-/jcr:root" };
 
+    /**
+     * Tries to figure out, if the repository provider is ready.
+     * <p>
+     * Also see SLING-3647.
+     * <p>
+     * This is heuristic at the moment: it assumes readiness as soon as *any*
+     * RepositoryFactory is registered. Whether or not a repository with 
+     * a particular address can be created, is then a second step.
+     * <p>
+     * @return
+     */
+    public static boolean isRepositoryProviderReady() {
+        Iterator<RepositoryFactory> providerIt = ServiceRegistry.lookupProviders(RepositoryFactory.class);
+        return providerIt.hasNext();
+    }
+    
     public static Repository getRepository(RepositoryInfo repositoryInfo) throws RepositoryException {
+        final RepositoryAddress repositoryAddress = getRepositoryAddress(repositoryInfo);
         synchronized (SYNC) {
-            return REPOSITORY_PROVIDER.getRepository(getRepositoryAddress(repositoryInfo));
+            try{
+                return REPOSITORY_PROVIDER.getRepository(repositoryAddress);
+            } catch(RepositoryException re) {
+                if (isRepositoryProviderReady()) {
+                    throw re;
+                } else {
+                    throw new RepositoryException(REPOSITORY_PROVIDER_NOT_YET_READY_MSG, re);
+                }
+            }
         }
     }
 
@@ -59,8 +88,10 @@ public abstract class RepositoryUtils {
                             .getPassword().toCharArray()));
                     return address;
                 } catch (URISyntaxException e) {
+                    e.printStackTrace();
                     throw new RuntimeException(e);
                 } catch (RepositoryException e) {
+                    e.printStackTrace();
                     errors.append(url).append(" : ").append(e.getMessage()).append('\n');
                     continue;
                 } finally {
@@ -72,8 +103,13 @@ public abstract class RepositoryUtils {
 
         errors.deleteCharAt(errors.length() - 1);
 
-        throw new IllegalArgumentException("No repository found at " + repositoryInfo.getUrl() + "\n"
+        IllegalArgumentException iae = new IllegalArgumentException("No repository found at " + repositoryInfo.getUrl() + "\n"
                 + errors.toString());
+        if (isRepositoryProviderReady()) {
+            throw iae;
+        } else {
+            throw new IllegalArgumentException(REPOSITORY_PROVIDER_NOT_YET_READY_MSG, iae);
+        }
     }
 
     public static Credentials getCredentials(RepositoryInfo repositoryInfo) {