Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Mar 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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.
Expand Down Expand Up @@ -28,13 +28,22 @@ class CdiRepositoryClient {
@Inject
private CdiPersonRepository cdiPersonRepository;

@Inject
@OtherQualifier
@PersonDB
private QualifiedPersonRepository qualifiedPersonRepository;

@Inject
private Bucket couchbaseClient;

public CdiPersonRepository getCdiPersonRepository() {
return cdiPersonRepository;
}

public QualifiedPersonRepository getQualifiedPersonRepository() {
return qualifiedPersonRepository;
}

public Bucket getCouchbaseClient() {
return couchbaseClient;
}
Expand Down
@@ -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.
Expand Down Expand Up @@ -39,6 +39,7 @@ public class CdiRepositoryTests {

private static CdiTestContainer cdiContainer;
private CdiPersonRepository repository;
private QualifiedPersonRepository qualifiedPersonRepository;
private Bucket couchbaseClient;

@BeforeClass
Expand All @@ -58,6 +59,7 @@ public static void shutdown() throws Exception {
public void setUp() {
CdiRepositoryClient repositoryClient = cdiContainer.getInstance(CdiRepositoryClient.class);
repository = repositoryClient.getCdiPersonRepository();
qualifiedPersonRepository = repositoryClient.getQualifiedPersonRepository();

couchbaseClient = repositoryClient.getCouchbaseClient();
createAndWaitForDesignDocs(couchbaseClient);
Expand Down Expand Up @@ -91,7 +93,26 @@ public void testCdiRepository() {
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());
}

/**
Expand Down
@@ -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.
Expand All @@ -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;
Expand All @@ -34,15 +36,23 @@
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();
}

public void close(@Disposes Bucket couchbaseClient) {
couchbaseClient.close();
}

public void close(@Disposes Cluster cluster) {
cluster.disconnect();
}
}
@@ -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.
Expand Down Expand Up @@ -30,12 +30,12 @@
* 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();
}

Expand Down
@@ -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.
Expand Down Expand Up @@ -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);
}

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

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

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

}
@@ -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.
Expand Down Expand Up @@ -38,7 +38,7 @@
*/
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
Expand All @@ -52,7 +52,7 @@ <T> void processBean(@Observes ProcessBean<T> processBean) {
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));
}
}
}
Expand Down Expand Up @@ -87,8 +87,7 @@ void afterBeanDiscovery(@Observes AfterBeanDiscovery afterBeanDiscovery, BeanMan
*/
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.",
Expand Down