diff --git a/src/integration/java/org/springframework/data/couchbase/repository/cdi/CdiRepositoryClient.java b/src/integration/java/org/springframework/data/couchbase/repository/cdi/CdiRepositoryClient.java index d6c99c74..2662f036 100644 --- a/src/integration/java/org/springframework/data/couchbase/repository/cdi/CdiRepositoryClient.java +++ b/src/integration/java/org/springframework/data/couchbase/repository/cdi/CdiRepositoryClient.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,6 +28,11 @@ class CdiRepositoryClient { @Inject private CdiPersonRepository cdiPersonRepository; + @Inject + @OtherQualifier + @PersonDB + private QualifiedPersonRepository qualifiedPersonRepository; + @Inject private Bucket couchbaseClient; @@ -35,6 +40,10 @@ class CdiRepositoryClient { return cdiPersonRepository; } + public QualifiedPersonRepository getQualifiedPersonRepository() { + return qualifiedPersonRepository; + } + public Bucket getCouchbaseClient() { return couchbaseClient; } diff --git a/src/integration/java/org/springframework/data/couchbase/repository/cdi/CdiRepositoryTests.java b/src/integration/java/org/springframework/data/couchbase/repository/cdi/CdiRepositoryTests.java index 91ab94e6..b172e142 100644 --- a/src/integration/java/org/springframework/data/couchbase/repository/cdi/CdiRepositoryTests.java +++ b/src/integration/java/org/springframework/data/couchbase/repository/cdi/CdiRepositoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,6 +39,7 @@ public class CdiRepositoryTests { private static CdiTestContainer cdiContainer; private CdiPersonRepository repository; + private QualifiedPersonRepository qualifiedPersonRepository; private Bucket couchbaseClient; @BeforeClass @@ -58,6 +59,7 @@ public class CdiRepositoryTests { public void setUp() { CdiRepositoryClient repositoryClient = cdiContainer.getInstance(CdiRepositoryClient.class); repository = repositoryClient.getCdiPersonRepository(); + qualifiedPersonRepository = repositoryClient.getQualifiedPersonRepository(); couchbaseClient = repositoryClient.getCouchbaseClient(); createAndWaitForDesignDocs(couchbaseClient); @@ -91,7 +93,26 @@ public class CdiRepositoryTests { assertNotNull(retrieved); assertEquals(bean.getName(), retrieved.getName()); assertEquals(bean.getId(), retrieved.getId()); + } + + /** + * @see DATACOUCH-203 + */ + @Test + public void testQualifiedCdiRepository() { + assertNotNull(qualifiedPersonRepository); + qualifiedPersonRepository.deleteAll(); + Person bean = new Person("key", "username"); + + qualifiedPersonRepository.save(bean); + + assertTrue(qualifiedPersonRepository.exists(bean.getId())); + + Person retrieved = qualifiedPersonRepository.findOne(bean.getId()); + assertNotNull(retrieved); + assertEquals(bean.getName(), retrieved.getName()); + assertEquals(bean.getId(), retrieved.getId()); } /** diff --git a/src/integration/java/org/springframework/data/couchbase/repository/cdi/CouchbaseClientProducer.java b/src/integration/java/org/springframework/data/couchbase/repository/cdi/CouchbaseClientProducer.java index c32da14f..f36f2240 100644 --- a/src/integration/java/org/springframework/data/couchbase/repository/cdi/CouchbaseClientProducer.java +++ b/src/integration/java/org/springframework/data/couchbase/repository/cdi/CouchbaseClientProducer.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,10 +16,12 @@ package org.springframework.data.couchbase.repository.cdi; +import javax.enterprise.context.ApplicationScoped; import javax.enterprise.inject.Disposes; import javax.enterprise.inject.Produces; import com.couchbase.client.java.Bucket; +import com.couchbase.client.java.Cluster; import com.couchbase.client.java.CouchbaseCluster; import org.springframework.data.couchbase.config.CouchbaseBucketFactoryBean; @@ -34,10 +36,14 @@ import org.springframework.data.couchbase.config.CouchbaseBucketFactoryBean; class CouchbaseClientProducer { @Produces - public Bucket createCouchbaseClient() throws Exception { - //FIXME produce a Cluster and close it properly? - CouchbaseBucketFactoryBean couchbaseFactoryBean = new CouchbaseBucketFactoryBean( - CouchbaseCluster.create(), "default"); + @ApplicationScoped + public Cluster cluster() { + return CouchbaseCluster.create(); + } + + @Produces + public Bucket createCouchbaseClient(Cluster cluster) throws Exception { + CouchbaseBucketFactoryBean couchbaseFactoryBean = new CouchbaseBucketFactoryBean(cluster, "default"); couchbaseFactoryBean.afterPropertiesSet(); return couchbaseFactoryBean.getObject(); } @@ -45,4 +51,8 @@ class CouchbaseClientProducer { public void close(@Disposes Bucket couchbaseClient) { couchbaseClient.close(); } + + public void close(@Disposes Cluster cluster) { + cluster.disconnect(); + } } diff --git a/src/integration/java/org/springframework/data/couchbase/repository/cdi/CouchbaseClusterInfoProducer.java b/src/integration/java/org/springframework/data/couchbase/repository/cdi/CouchbaseClusterInfoProducer.java index 04762d67..6c66c677 100644 --- a/src/integration/java/org/springframework/data/couchbase/repository/cdi/CouchbaseClusterInfoProducer.java +++ b/src/integration/java/org/springframework/data/couchbase/repository/cdi/CouchbaseClusterInfoProducer.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,12 +30,12 @@ import org.springframework.data.couchbase.core.CouchbaseTemplate; * Produces a {@link ClusterInfo} instance for test usage. * * @author Simon Baslé + * @author Mark Paluch */ class CouchbaseClusterInfoProducer { @Produces - public ClusterInfo createClusterInfo() throws Exception { - Cluster cluster = CouchbaseCluster.create(); + public ClusterInfo createClusterInfo(Cluster cluster) throws Exception { return cluster.clusterManager("default", "").info(); } diff --git a/src/integration/java/org/springframework/data/couchbase/repository/cdi/CouchbaseOperationsProducer.java b/src/integration/java/org/springframework/data/couchbase/repository/cdi/CouchbaseOperationsProducer.java index 47b8b322..8335fc9f 100644 --- a/src/integration/java/org/springframework/data/couchbase/repository/cdi/CouchbaseOperationsProducer.java +++ b/src/integration/java/org/springframework/data/couchbase/repository/cdi/CouchbaseOperationsProducer.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,10 +33,14 @@ class CouchbaseOperationsProducer { @Produces public CouchbaseOperations createCouchbaseOperations(Bucket couchbaseClient, ClusterInfo clusterInfo) throws Exception { - - CouchbaseTemplate couchbaseTemplate = new CouchbaseTemplate(clusterInfo, couchbaseClient); - - return couchbaseTemplate; + return new CouchbaseTemplate(clusterInfo, couchbaseClient); + } + + @Produces + @OtherQualifier + @PersonDB + public CouchbaseOperations createQualifiedCouchbaseOperations(Bucket couchbaseClient, ClusterInfo clusterInfo) throws Exception { + return new CouchbaseTemplate(clusterInfo, couchbaseClient); } } diff --git a/src/integration/java/org/springframework/data/couchbase/repository/cdi/OtherQualifier.java b/src/integration/java/org/springframework/data/couchbase/repository/cdi/OtherQualifier.java new file mode 100644 index 00000000..59d88abd --- /dev/null +++ b/src/integration/java/org/springframework/data/couchbase/repository/cdi/OtherQualifier.java @@ -0,0 +1,35 @@ +/* + * Copyright 2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.data.couchbase.repository.cdi; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import javax.inject.Qualifier; + +/** + * @author Mark Paluch + * @see DATACOUCH-203 + */ +@Qualifier +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER }) +@interface OtherQualifier { + +} diff --git a/src/integration/java/org/springframework/data/couchbase/repository/cdi/PersonDB.java b/src/integration/java/org/springframework/data/couchbase/repository/cdi/PersonDB.java new file mode 100644 index 00000000..41f06e4d --- /dev/null +++ b/src/integration/java/org/springframework/data/couchbase/repository/cdi/PersonDB.java @@ -0,0 +1,35 @@ +/* + * Copyright 2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.data.couchbase.repository.cdi; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import javax.inject.Qualifier; + +/** + * @author Mark Paluch + * @see DATACOUCH-203 + */ +@Qualifier +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER }) +@interface PersonDB { + +} diff --git a/src/integration/java/org/springframework/data/couchbase/repository/cdi/QualifiedPersonRepository.java b/src/integration/java/org/springframework/data/couchbase/repository/cdi/QualifiedPersonRepository.java new file mode 100644 index 00000000..dc89dd1f --- /dev/null +++ b/src/integration/java/org/springframework/data/couchbase/repository/cdi/QualifiedPersonRepository.java @@ -0,0 +1,29 @@ +/* + * Copyright 2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.data.couchbase.repository.cdi; + +import org.springframework.data.repository.CrudRepository; + +/** + * @author Mark Paluch + * @see DATACOUCH-203 + */ +@PersonDB +@OtherQualifier +public interface QualifiedPersonRepository extends CrudRepository { + +} diff --git a/src/main/java/org/springframework/data/couchbase/repository/cdi/CouchbaseRepositoryExtension.java b/src/main/java/org/springframework/data/couchbase/repository/cdi/CouchbaseRepositoryExtension.java index eab8fb20..82b5f6ed 100644 --- a/src/main/java/org/springframework/data/couchbase/repository/cdi/CouchbaseRepositoryExtension.java +++ b/src/main/java/org/springframework/data/couchbase/repository/cdi/CouchbaseRepositoryExtension.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,7 +38,7 @@ import org.springframework.data.repository.cdi.CdiRepositoryExtensionSupport; */ public class CouchbaseRepositoryExtension extends CdiRepositoryExtensionSupport{ - private final Map> couchbaseOperationsMap = new HashMap>(); + private final Map, Bean> couchbaseOperationsMap = new HashMap, Bean>(); /** * Implementation of a an observer which checks for CouchbaseOperations beans and stores them in {@link #couchbaseOperationsMap} for @@ -52,7 +52,7 @@ public class CouchbaseRepositoryExtension extends CdiRepositoryExtensionSupport{ Bean bean = processBean.getBean(); for (Type type : bean.getTypes()) { if (type instanceof Class && CouchbaseOperations.class.isAssignableFrom((Class) type)) { - couchbaseOperationsMap.put(bean.getQualifiers().toString(), ((Bean) bean)); + couchbaseOperationsMap.put(bean.getQualifiers(), ((Bean) bean)); } } } @@ -87,8 +87,7 @@ public class CouchbaseRepositoryExtension extends CdiRepositoryExtensionSupport{ */ private CdiRepositoryBean createRepositoryBean(Class repositoryType, Set qualifiers, BeanManager beanManager) { - Bean couchbaseOperationsBean = this.couchbaseOperationsMap.get(qualifiers - .toString()); + Bean couchbaseOperationsBean = this.couchbaseOperationsMap.get(qualifiers); if (couchbaseOperationsBean == null) { throw new UnsatisfiedResolutionException(String.format("Unable to resolve a bean for '%s' with qualifiers %s.",