DATACOUCH-203 - Use Set to store qualifiers in CDI extension.

Qualifiers are now stored in their original set instead of using the toString representation.
This commit is contained in:
Mark Paluch
2016-03-15 14:13:14 +01:00
parent 4aa823722a
commit db4c24d9f1
9 changed files with 162 additions and 20 deletions

View File

@@ -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;
}

View File

@@ -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());
}
/**

View File

@@ -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();
}
}

View File

@@ -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();
}

View File

@@ -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);
}
}

View File

@@ -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 {
}

View File

@@ -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 {
}

View File

@@ -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<Person, String> {
}

View File

@@ -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<String, Bean<CouchbaseOperations>> couchbaseOperationsMap = new HashMap<String, Bean<CouchbaseOperations>>();
private final Map<Set<Annotation>, Bean<CouchbaseOperations>> couchbaseOperationsMap = new HashMap<Set<Annotation>, Bean<CouchbaseOperations>>();
/**
* 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<T> bean = processBean.getBean();
for (Type type : bean.getTypes()) {
if (type instanceof Class<?> && CouchbaseOperations.class.isAssignableFrom((Class<?>) type)) {
couchbaseOperationsMap.put(bean.getQualifiers().toString(), ((Bean<CouchbaseOperations>) bean));
couchbaseOperationsMap.put(bean.getQualifiers(), ((Bean<CouchbaseOperations>) bean));
}
}
}
@@ -87,8 +87,7 @@ public class CouchbaseRepositoryExtension extends CdiRepositoryExtensionSupport{
*/
private <T> CdiRepositoryBean<T> createRepositoryBean(Class<T> repositoryType, Set<Annotation> qualifiers, BeanManager beanManager) {
Bean<CouchbaseOperations> couchbaseOperationsBean = this.couchbaseOperationsMap.get(qualifiers
.toString());
Bean<CouchbaseOperations> couchbaseOperationsBean = this.couchbaseOperationsMap.get(qualifiers);
if (couchbaseOperationsBean == null) {
throw new UnsatisfiedResolutionException(String.format("Unable to resolve a bean for '%s' with qualifiers %s.",