SLING-5437 add connection check for couchbase resource provider
define separate NoSqlAdapter methods for creating index definitions, to ensure they are only executed after connection test succeeds
set versions to 1.1.0

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1725576 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/pom.xml b/pom.xml
index 0355b76..e8e074f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -28,7 +28,7 @@
     </parent>
 
     <artifactId>org.apache.sling.nosql.couchbase-resourceprovider</artifactId>
-    <version>1.0.1-SNAPSHOT</version>
+    <version>1.1.0-SNAPSHOT</version>
     <packaging>bundle</packaging>
   
     <name>Apache Sling NoSQL Couchbase Resource Provider</name>
@@ -56,7 +56,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.nosql.generic</artifactId>
-            <version>1.0.0</version>
+            <version>1.1.0-SNAPSHOT</version>
             <scope>provided</scope>
         </dependency>
     
diff --git a/src/main/java/org/apache/sling/nosql/couchbase/resourceprovider/impl/CouchbaseNoSqlAdapter.java b/src/main/java/org/apache/sling/nosql/couchbase/resourceprovider/impl/CouchbaseNoSqlAdapter.java
index 12302fa..2b0dd6c 100644
--- a/src/main/java/org/apache/sling/nosql/couchbase/resourceprovider/impl/CouchbaseNoSqlAdapter.java
+++ b/src/main/java/org/apache/sling/nosql/couchbase/resourceprovider/impl/CouchbaseNoSqlAdapter.java
@@ -26,12 +26,15 @@
 import java.util.regex.Pattern;
 
 import org.apache.commons.lang3.StringUtils;
+import org.apache.sling.api.resource.LoginException;
 import org.apache.sling.api.resource.ResourceUtil;
 import org.apache.sling.nosql.couchbase.client.CouchbaseClient;
 import org.apache.sling.nosql.couchbase.client.CouchbaseKey;
 import org.apache.sling.nosql.generic.adapter.AbstractNoSqlAdapter;
 import org.apache.sling.nosql.generic.adapter.MultiValueMode;
 import org.apache.sling.nosql.generic.adapter.NoSqlData;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import com.couchbase.client.java.Bucket;
 import com.couchbase.client.java.document.JsonDocument;
@@ -57,14 +60,11 @@
     
     private static final N1qlParams N1QL_PARAMS = N1qlParams.build().consistency(ScanConsistency.REQUEST_PLUS);
 
+    private static final Logger log = LoggerFactory.getLogger(CouchbaseNoSqlAdapter.class);
+
     public CouchbaseNoSqlAdapter(CouchbaseClient couchbaseClient, String cacheKeyPrefix) {
         this.couchbaseClient = couchbaseClient;
         this.cacheKeyPrefix = cacheKeyPrefix;
-        
-        // make sure primary index and index on parentPath is present - ignore error if it is already present
-        Bucket bucket = couchbaseClient.getBucket();
-        bucket.query(N1qlQuery.simple("CREATE PRIMARY INDEX ON `" + couchbaseClient.getBucketName() + "`"));
-        bucket.query(N1qlQuery.simple("CREATE INDEX " + PN_PARENT_PATH + " ON `" + couchbaseClient.getBucketName() + "`(" + PN_PARENT_PATH + ")"));
     }
 
     @Override
@@ -183,4 +183,30 @@
         }
     }
 
+    @Override
+    public void checkConnection() throws LoginException {
+        // try to access root element to check connection
+        try {
+            Bucket bucket = couchbaseClient.getBucket();
+            String cacheKey = CouchbaseKey.build("/", cacheKeyPrefix);
+            bucket.exists(cacheKey);
+        }
+        catch (Throwable ex) {
+            throw new LoginException(ex);
+        }
+    }
+
+    @Override
+    public void createIndexDefinitions() {
+        // make sure primary index and index on parentPath is present - ignore error if it is already present
+        try {
+            Bucket bucket = couchbaseClient.getBucket();
+            bucket.query(N1qlQuery.simple("CREATE PRIMARY INDEX ON `" + couchbaseClient.getBucketName() + "`"));
+            bucket.query(N1qlQuery.simple("CREATE INDEX " + PN_PARENT_PATH + " ON `" + couchbaseClient.getBucketName() + "`(" + PN_PARENT_PATH + ")"));
+        }
+        catch (Throwable ex) {
+            log.debug("Unable to create/validate couchbase index definitions: " + ex.getMessage(), ex);
+        }
+    }
+
 }