DATAES-113 Add support for custom implementations in CDI repositories

This commit is contained in:
Mark Paluch 2014-08-07 20:43:53 +02:00 committed by Mohsin Husen
parent 1f99dcb911
commit b77b8ba0f2
7 changed files with 124 additions and 6 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -31,24 +31,35 @@ import org.springframework.util.Assert;
* *
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
* @author Mark Paluch
*/ */
public class ElasticsearchRepositoryBean<T> extends CdiRepositoryBean<T> { public class ElasticsearchRepositoryBean<T> extends CdiRepositoryBean<T> {
private final Bean<ElasticsearchOperations> elasticsearchOperationsBean; private final Bean<ElasticsearchOperations> elasticsearchOperationsBean;
/**
* Creates a new {@link ElasticsearchRepositoryBean}.
*
* @param operations must not be {@literal null}.
* @param qualifiers must not be {@literal null}.
* @param repositoryType must not be {@literal null}.
* @param beanManager must not be {@literal null}.
* @param customImplementationBean the bean for the custom implementation of the
* {@link org.springframework.data.repository.Repository}, can be {@literal null}.
*/
public ElasticsearchRepositoryBean(Bean<ElasticsearchOperations> operations, Set<Annotation> qualifiers, public ElasticsearchRepositoryBean(Bean<ElasticsearchOperations> operations, Set<Annotation> qualifiers,
Class<T> repositoryType, BeanManager beanManager) { Class<T> repositoryType, BeanManager beanManager, Bean<?> customImplementationBean) {
super(qualifiers, repositoryType, beanManager); super(qualifiers, repositoryType, beanManager, customImplementationBean);
Assert.notNull(operations, "Cannot create repository with 'null' for ElasticsearchOperations."); Assert.notNull(operations, "Cannot create repository with 'null' for ElasticsearchOperations.");
this.elasticsearchOperationsBean = operations; this.elasticsearchOperationsBean = operations;
} }
@Override @Override
protected T create(CreationalContext<T> creationalContext, Class<T> repositoryType) { protected T create(CreationalContext<T> creationalContext, Class<T> repositoryType, Object customImplementation) {
ElasticsearchOperations elasticsearchOperations = getDependencyInstance(elasticsearchOperationsBean, ElasticsearchOperations elasticsearchOperations = getDependencyInstance(elasticsearchOperationsBean,
ElasticsearchOperations.class); ElasticsearchOperations.class);
return new ElasticsearchRepositoryFactory(elasticsearchOperations).getRepository(repositoryType); return new ElasticsearchRepositoryFactory(elasticsearchOperations).getRepository(repositoryType, customImplementation);
} }
@Override @Override

View File

@ -38,6 +38,7 @@ import org.springframework.data.repository.cdi.CdiRepositoryExtensionSupport;
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
* @author Oliver Gierke * @author Oliver Gierke
* @author Mark Paluch
*/ */
public class ElasticsearchRepositoryExtension extends CdiRepositoryExtensionSupport { public class ElasticsearchRepositoryExtension extends CdiRepositoryExtensionSupport {
@ -74,7 +75,8 @@ public class ElasticsearchRepositoryExtension extends CdiRepositoryExtensionSupp
throw new UnsatisfiedResolutionException(String.format("Unable to resolve a bean for '%s' with qualifiers %s.", throw new UnsatisfiedResolutionException(String.format("Unable to resolve a bean for '%s' with qualifiers %s.",
ElasticsearchOperations.class.getName(), qualifiers)); ElasticsearchOperations.class.getName(), qualifiers));
} }
Bean<?> customImplementationBean = getCustomImplementationBean(repositoryType, beanManager, qualifiers);
return new ElasticsearchRepositoryBean<T>(elasticsearchOperationsBean, qualifiers, repositoryType, beanManager); return new ElasticsearchRepositoryBean<T>(elasticsearchOperationsBean, qualifiers, repositoryType, beanManager, customImplementationBean);
} }
} }

View File

@ -24,6 +24,8 @@ import javax.inject.Inject;
class CdiRepositoryClient { class CdiRepositoryClient {
private CdiProductRepository repository; private CdiProductRepository repository;
private SamplePersonRepository samplePersonRepository;
public CdiProductRepository getRepository() { public CdiProductRepository getRepository() {
return repository; return repository;
@ -33,4 +35,13 @@ class CdiRepositoryClient {
public void setRepository(CdiProductRepository repository) { public void setRepository(CdiProductRepository repository) {
this.repository = repository; this.repository = repository;
} }
public SamplePersonRepository getSamplePersonRepository() {
return samplePersonRepository;
}
@Inject
public void setSamplePersonRepository(SamplePersonRepository samplePersonRepository) {
this.samplePersonRepository = samplePersonRepository;
}
} }

View File

@ -15,6 +15,7 @@
*/ */
package org.springframework.data.elasticsearch.repositories.cdi; package org.springframework.data.elasticsearch.repositories.cdi;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import org.apache.webbeans.cditest.CdiTestContainer; import org.apache.webbeans.cditest.CdiTestContainer;
@ -33,6 +34,7 @@ public class CdiRepositoryTests {
private static CdiTestContainer cdiContainer; private static CdiTestContainer cdiContainer;
private CdiProductRepository repository; private CdiProductRepository repository;
private SamplePersonRepository personRepository;
@BeforeClass @BeforeClass
public static void init() throws Exception { public static void init() throws Exception {
@ -51,6 +53,7 @@ public class CdiRepositoryTests {
public void setUp() { public void setUp() {
CdiRepositoryClient client = cdiContainer.getInstance(CdiRepositoryClient.class); CdiRepositoryClient client = cdiContainer.getInstance(CdiRepositoryClient.class);
repository = client.getRepository(); repository = client.getRepository();
personRepository = client.getSamplePersonRepository();
} }
@Test @Test
@ -80,4 +83,13 @@ public class CdiRepositoryTests {
retrieved = repository.findOne(bean.getId()); retrieved = repository.findOne(bean.getId());
assertNull(retrieved); assertNull(retrieved);
} }
/**
* @see DATAES-113
*/
@Test
public void returnOneFromCustomImpl() {
assertThat(personRepository.returnOne(), is(1));
}
} }

View File

@ -0,0 +1,27 @@
/*
* Copyright 2014 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.elasticsearch.repositories.cdi;
import org.springframework.data.elasticsearch.entities.Person;
import org.springframework.data.repository.Repository;
/**
* @author Mark Paluch
* @see DATAES-113
*/
public interface SamplePersonRepository extends Repository<Person, Long>, SamplePersonRepositoryCustom {
}

View File

@ -0,0 +1,26 @@
/*
* Copyright 2014 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.elasticsearch.repositories.cdi;
/**
* @see DATAES-113
* @author Mark Paluch
*/
interface SamplePersonRepositoryCustom {
int returnOne();
}

View File

@ -0,0 +1,29 @@
/*
* Copyright 2014 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.elasticsearch.repositories.cdi;
/**
* @see DATAES-113
* @author Mark Paluch
*/
class SamplePersonRepositoryImpl implements SamplePersonRepositoryCustom {
@Override
public int returnOne() {
return 1;
}
}