From 1ee0045f0e68825753704187207ae67762e08f54 Mon Sep 17 00:00:00 2001 From: Michael Nitschinger Date: Mon, 9 Mar 2020 09:37:11 +0100 Subject: [PATCH] DATACOUCH-509 - Expose only SDK related infos on configurer Since the client factory is a spring data concept, it cannot be exposed on the configurer. Only expose the cluster and the environment on it. This also modifies the config to populate the right beans from the configurer methods as well. --- .../SimpleCouchbaseClientFactory.java | 2 +- .../AbstractCouchbaseConfiguration.java | 21 ++++++++++++++++--- .../couchbase/config/CouchbaseConfigurer.java | 8 +++---- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/springframework/data/couchbase/SimpleCouchbaseClientFactory.java b/src/main/java/org/springframework/data/couchbase/SimpleCouchbaseClientFactory.java index b7e71999..e0c58c50 100644 --- a/src/main/java/org/springframework/data/couchbase/SimpleCouchbaseClientFactory.java +++ b/src/main/java/org/springframework/data/couchbase/SimpleCouchbaseClientFactory.java @@ -50,7 +50,7 @@ public class SimpleCouchbaseClientFactory implements CouchbaseClientFactory { this(Cluster.connect(connectionString, ClusterOptions.clusterOptions(authenticator).environment(environment)), bucketName, scopeName); } - SimpleCouchbaseClientFactory(final Cluster cluster, final String bucketName, final String scopeName) { + public SimpleCouchbaseClientFactory(final Cluster cluster, final String bucketName, final String scopeName) { this.cluster = cluster; this.bucket = cluster.bucket(bucketName); this.scope = scopeName == null ? bucket.defaultScope() : bucket.scope(scopeName); diff --git a/src/main/java/org/springframework/data/couchbase/config/AbstractCouchbaseConfiguration.java b/src/main/java/org/springframework/data/couchbase/config/AbstractCouchbaseConfiguration.java index dac1de92..2ad56e78 100644 --- a/src/main/java/org/springframework/data/couchbase/config/AbstractCouchbaseConfiguration.java +++ b/src/main/java/org/springframework/data/couchbase/config/AbstractCouchbaseConfiguration.java @@ -20,6 +20,8 @@ import java.util.Collections; import java.util.HashSet; import java.util.Set; +import com.couchbase.client.java.Cluster; +import com.couchbase.client.java.ClusterOptions; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; @@ -50,6 +52,8 @@ import com.couchbase.client.core.env.PasswordAuthenticator; import com.couchbase.client.java.env.ClusterEnvironment; import com.couchbase.client.java.json.JacksonTransformers; +import static com.couchbase.client.java.ClusterOptions.*; + /** * Base class for Spring Data Couchbase configuration using JavaConfig. * @@ -61,6 +65,10 @@ import com.couchbase.client.java.json.JacksonTransformers; @Configuration public abstract class AbstractCouchbaseConfiguration implements CouchbaseConfigurer { + protected CouchbaseConfigurer couchbaseConfigurer() { + return this; + } + public abstract String getConnectionString(); public abstract String getUserName(); @@ -78,10 +86,17 @@ public abstract class AbstractCouchbaseConfiguration implements CouchbaseConfigu } @Bean + public CouchbaseClientFactory couchbaseClientFactory(Cluster couchbaseCluster) { + return new SimpleCouchbaseClientFactory(couchbaseCluster, getBucketName(), getScopeName()); + } + @Override - public CouchbaseClientFactory couchbaseClientFactory(ClusterEnvironment clusterEnvironment) { - return new SimpleCouchbaseClientFactory(getConnectionString(), authenticator(), getBucketName(), - getScopeName(), clusterEnvironment); + @Bean + public Cluster couchbaseCluster() throws Exception { + return Cluster.connect( + getConnectionString(), + clusterOptions(authenticator()).environment(couchbaseConfigurer().clusterEnvironment()) + ); } @Override diff --git a/src/main/java/org/springframework/data/couchbase/config/CouchbaseConfigurer.java b/src/main/java/org/springframework/data/couchbase/config/CouchbaseConfigurer.java index 281fc01b..07c8786b 100644 --- a/src/main/java/org/springframework/data/couchbase/config/CouchbaseConfigurer.java +++ b/src/main/java/org/springframework/data/couchbase/config/CouchbaseConfigurer.java @@ -16,8 +16,8 @@ package org.springframework.data.couchbase.config; +import com.couchbase.client.java.Cluster; import com.couchbase.client.java.env.ClusterEnvironment; -import org.springframework.data.couchbase.CouchbaseClientFactory; /** * Strategy interface for users to provide as a factory for custom components needed @@ -38,11 +38,11 @@ public interface CouchbaseConfigurer { ClusterEnvironment clusterEnvironment() throws Exception; /** - * Set up the underlying main {@link CouchbaseClientFactory} reference to be used by the Spring Data framework + * Set up the underlying main {@link Cluster} reference to be used by the Spring Data framework * when storing into Couchbase. * - * @throws Exception in case of error during the CouchbaseClientFactory instantiation. + * @throws Exception in case of error during the Cluster instantiation. */ - CouchbaseClientFactory couchbaseClientFactory(ClusterEnvironment clusterEnvironment) throws Exception; + Cluster couchbaseCluster() throws Exception; }