Browse Source

DATACMNS-989 - Polishing.

Introduced overload for CustomRepositoryImplementationDetector.detectCustomImplementation(…) to take a RepositoryConfiguration to simplify the invocation from the Spring container related client. Further simplification to unify invocations also from the CDI implementation to be done in 2.0.

Expose getExcludeFilter() on RepositoryConfiguration to not have to inspect the configuration source on clients.

Formatting and Javadoc. Missing license headers in newly created types.

Original pull request: #195.
pull/194/head
Oliver Gierke 9 years ago
parent
commit
ebd871b566
  1. 18
      src/main/java/org/springframework/data/repository/cdi/CdiRepositoryBean.java
  2. 10
      src/main/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSource.java
  3. 31
      src/main/java/org/springframework/data/repository/config/CustomRepositoryImplementationDetector.java
  4. 16
      src/main/java/org/springframework/data/repository/config/DefaultRepositoryConfiguration.java
  5. 3
      src/main/java/org/springframework/data/repository/config/RepositoryBeanDefinitionBuilder.java
  6. 12
      src/main/java/org/springframework/data/repository/config/RepositoryConfiguration.java
  7. 5
      src/main/java/org/springframework/data/repository/config/RepositoryConfigurationSource.java
  8. 17
      src/test/java/org/springframework/data/repository/config/MyOtherRepositoryExtensions.java
  9. 28
      src/test/java/org/springframework/data/repository/config/MyOtherRepositoryImpl.java
  10. 24
      src/test/java/org/springframework/data/repository/config/excluded/MyOtherRepositoryImpl.java

18
src/main/java/org/springframework/data/repository/cdi/CdiRepositoryBean.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2011-2014 the original author or authors.
* Copyright 2011-2017 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.
@ -246,7 +246,7 @@ public abstract class CdiRepositoryBean<T> implements Bean<T>, PassivationCapabl @@ -246,7 +246,7 @@ public abstract class CdiRepositoryBean<T> implements Bean<T>, PassivationCapabl
String className = getCustomImplementationClassName(repositoryType, cdiRepositoryConfiguration);
AbstractBeanDefinition beanDefinition = detector.detectCustomImplementation(className,
Collections.singleton(repositoryType.getPackage().getName()), Collections.<TypeFilter>emptySet());
Collections.singleton(repositoryType.getPackage().getName()), Collections.<TypeFilter> emptySet());
if (beanDefinition == null) {
return null;
@ -255,8 +255,8 @@ public abstract class CdiRepositoryBean<T> implements Bean<T>, PassivationCapabl @@ -255,8 +255,8 @@ public abstract class CdiRepositoryBean<T> implements Bean<T>, PassivationCapabl
try {
return Class.forName(beanDefinition.getBeanClassName());
} catch (ClassNotFoundException e) {
throw new UnsatisfiedResolutionException(String.format("Unable to resolve class for '%s'",
beanDefinition.getBeanClassName()), e);
throw new UnsatisfiedResolutionException(
String.format("Unable to resolve class for '%s'", beanDefinition.getBeanClassName()), e);
}
}
@ -367,9 +367,9 @@ public abstract class CdiRepositoryBean<T> implements Bean<T>, PassivationCapabl @@ -367,9 +367,9 @@ public abstract class CdiRepositoryBean<T> implements Bean<T>, PassivationCapabl
protected T create(CreationalContext<T> creationalContext, Class<T> repositoryType) {
Bean<?> customImplementationBean = getCustomImplementationBean(repositoryType, beanManager, qualifiers);
Object customImplementation = customImplementationBean == null ? null : beanManager.getReference(
customImplementationBean, customImplementationBean.getBeanClass(),
beanManager.createCreationalContext(customImplementationBean));
Object customImplementation = customImplementationBean == null ? null
: beanManager.getReference(customImplementationBean, customImplementationBean.getBeanClass(),
beanManager.createCreationalContext(customImplementationBean));
return create(creationalContext, repositoryType, customImplementation);
}
@ -393,8 +393,8 @@ public abstract class CdiRepositoryBean<T> implements Bean<T>, PassivationCapabl @@ -393,8 +393,8 @@ public abstract class CdiRepositoryBean<T> implements Bean<T>, PassivationCapabl
*/
@Override
public String toString() {
return String
.format("CdiRepositoryBean: type='%s', qualifiers=%s", repositoryType.getName(), qualifiers.toString());
return String.format("CdiRepositoryBean: type='%s', qualifiers=%s", repositoryType.getName(),
qualifiers.toString());
}
static enum DefaultCdiRepositoryConfiguration implements CdiRepositoryConfiguration {

10
src/main/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSource.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2017 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.
@ -262,8 +262,8 @@ public class AnnotationRepositoryConfigurationSource extends RepositoryConfigura @@ -262,8 +262,8 @@ public class AnnotationRepositoryConfigurationSource extends RepositoryConfigura
for (Class<?> filterClass : filterAttributes.getClassArray("value")) {
switch (filterType) {
case ANNOTATION:
Assert.isAssignable(Annotation.class, filterClass, "An error occured when processing a @ComponentScan "
+ "ANNOTATION type filter: ");
Assert.isAssignable(Annotation.class, filterClass,
"An error occured when processing a @ComponentScan " + "ANNOTATION type filter: ");
@SuppressWarnings("unchecked")
Class<Annotation> annoClass = (Class<Annotation>) filterClass;
typeFilters.add(new AnnotationTypeFilter(annoClass));
@ -272,8 +272,8 @@ public class AnnotationRepositoryConfigurationSource extends RepositoryConfigura @@ -272,8 +272,8 @@ public class AnnotationRepositoryConfigurationSource extends RepositoryConfigura
typeFilters.add(new AssignableTypeFilter(filterClass));
break;
case CUSTOM:
Assert.isAssignable(TypeFilter.class, filterClass, "An error occured when processing a @ComponentScan "
+ "CUSTOM type filter: ");
Assert.isAssignable(TypeFilter.class, filterClass,
"An error occured when processing a @ComponentScan " + "CUSTOM type filter: ");
typeFilters.add(BeanUtils.instantiateClass(filterClass, TypeFilter.class));
break;
default:

31
src/main/java/org/springframework/data/repository/config/CustomRepositoryImplementationDetector.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2017 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.
@ -69,14 +69,30 @@ public class CustomRepositoryImplementationDetector { @@ -69,14 +69,30 @@ public class CustomRepositoryImplementationDetector {
this.resourceLoader = resourceLoader;
}
/**
* Tries to detect a custom implementation for a repository bean by classpath scanning.
*
* @param configuration the {@link RepositoryConfiguration} to consider.
* @return the {@code AbstractBeanDefinition} of the custom implementation or {@literal null} if none found.
*/
public AbstractBeanDefinition detectCustomImplementation(RepositoryConfiguration<?> configuration) {
// TODO 2.0: Extract into dedicated interface for custom implementation lookup configuration.
return detectCustomImplementation(configuration.getImplementationClassName(), //
configuration.getBasePackages(), //
configuration.getExcludeFilters());
}
/**
* Tries to detect a custom implementation for a repository bean by classpath scanning.
*
* @param className must not be {@literal null}.
* @param basePackages must not be {@literal null}.
* @return the {@code AbstractBeanDefinition} of the custom implementation or {@literal null} if none found
* @return the {@code AbstractBeanDefinition} of the custom implementation or {@literal null} if none found.
*/
public AbstractBeanDefinition detectCustomImplementation(String className, Iterable<String> basePackages, Iterable<TypeFilter> excludeFilters) {
public AbstractBeanDefinition detectCustomImplementation(String className, Iterable<String> basePackages,
Iterable<TypeFilter> excludeFilters) {
Assert.notNull(className, "ClassName must not be null!");
Assert.notNull(basePackages, "BasePackages must not be null!");
@ -91,7 +107,8 @@ public class CustomRepositoryImplementationDetector { @@ -91,7 +107,8 @@ public class CustomRepositoryImplementationDetector {
provider.setResourcePattern(String.format(CUSTOM_IMPLEMENTATION_RESOURCE_PATTERN, className));
provider.setMetadataReaderFactory(metadataReaderFactory);
provider.addIncludeFilter(new RegexPatternTypeFilter(pattern));
for(TypeFilter excludeFilter : excludeFilters) {
for (TypeFilter excludeFilter : excludeFilters) {
provider.addExcludeFilter(excludeFilter);
}
@ -114,8 +131,8 @@ public class CustomRepositoryImplementationDetector { @@ -114,8 +131,8 @@ public class CustomRepositoryImplementationDetector {
implementationClassNames.add(bean.getBeanClassName());
}
throw new IllegalStateException(String.format(
"Ambiguous custom implementations detected! Found %s but expected a single implementation!",
StringUtils.collectionToCommaDelimitedString(implementationClassNames)));
throw new IllegalStateException(
String.format("Ambiguous custom implementations detected! Found %s but expected a single implementation!",
StringUtils.collectionToCommaDelimitedString(implementationClassNames)));
}
}

16
src/main/java/org/springframework/data/repository/config/DefaultRepositoryConfiguration.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2017 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,6 +16,7 @@ @@ -16,6 +16,7 @@
package org.springframework.data.repository.config;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.core.type.filter.TypeFilter;
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
@ -26,8 +27,8 @@ import org.springframework.util.StringUtils; @@ -26,8 +27,8 @@ import org.springframework.util.StringUtils;
*
* @author Oliver Gierke
*/
public class DefaultRepositoryConfiguration<T extends RepositoryConfigurationSource> implements
RepositoryConfiguration<T> {
public class DefaultRepositoryConfiguration<T extends RepositoryConfigurationSource>
implements RepositoryConfiguration<T> {
public static final String DEFAULT_REPOSITORY_IMPLEMENTATION_POSTFIX = "Impl";
private static final Key DEFAULT_QUERY_LOOKUP_STRATEGY = Key.CREATE_IF_NOT_FOUND;
@ -167,4 +168,13 @@ public class DefaultRepositoryConfiguration<T extends RepositoryConfigurationSou @@ -167,4 +168,13 @@ public class DefaultRepositoryConfiguration<T extends RepositoryConfigurationSou
public boolean isLazyInit() {
return definition.isLazyInit();
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.config.RepositoryConfiguration#getExcludeFilters()
*/
@Override
public Iterable<TypeFilter> getExcludeFilters() {
return configurationSource.getExcludeFilters();
}
}

3
src/main/java/org/springframework/data/repository/config/RepositoryBeanDefinitionBuilder.java

@ -128,8 +128,7 @@ class RepositoryBeanDefinitionBuilder { @@ -128,8 +128,7 @@ class RepositoryBeanDefinitionBuilder {
return beanName;
}
AbstractBeanDefinition beanDefinition = implementationDetector
.detectCustomImplementation(configuration.getImplementationClassName(), configuration.getBasePackages(), configuration.getConfigurationSource().getExcludeFilters());
AbstractBeanDefinition beanDefinition = implementationDetector.detectCustomImplementation(configuration);
if (null == beanDefinition) {
return null;

12
src/main/java/org/springframework/data/repository/config/RepositoryConfiguration.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2017 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,6 +16,7 @@ @@ -16,6 +16,7 @@
package org.springframework.data.repository.config;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.core.type.filter.TypeFilter;
import org.springframework.data.repository.query.QueryLookupStrategy;
/**
@ -101,9 +102,16 @@ public interface RepositoryConfiguration<T extends RepositoryConfigurationSource @@ -101,9 +102,16 @@ public interface RepositoryConfiguration<T extends RepositoryConfigurationSource
T getConfigurationSource();
/**
* Returns whether to inititialize the repository proxy lazily.
* Returns whether to initialize the repository proxy lazily.
*
* @return
*/
boolean isLazyInit();
/**
* Returns the {@link TypeFilter}s to be used to exclude packages from repository scanning.
*
* @return
*/
Iterable<TypeFilter> getExcludeFilters();
}

5
src/main/java/org/springframework/data/repository/config/RepositoryConfigurationSource.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2017 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.
@ -112,7 +112,8 @@ public interface RepositoryConfigurationSource { @@ -112,7 +112,8 @@ public interface RepositoryConfigurationSource {
boolean usesExplicitFilters();
/**
* Return the {@link TypeFilter}s to define which types to exclude when scanning for repositories or repository implementations.
* Return the {@link TypeFilter}s to define which types to exclude when scanning for repositories or repository
* implementations.
*
* @return must not be {@literal null}.
*/

17
src/test/java/org/springframework/data/repository/config/MyOtherRepositoryExtensions.java

@ -1,5 +1,20 @@ @@ -1,5 +1,20 @@
/*
* Copyright 2017 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.repository.config;
public interface MyOtherRepositoryExtensions {
String getImplementationId();
String getImplementationId();
}

28
src/test/java/org/springframework/data/repository/config/MyOtherRepositoryImpl.java

@ -1,8 +1,28 @@ @@ -1,8 +1,28 @@
/*
* Copyright 2017 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.repository.config;
public class MyOtherRepositoryImpl implements MyOtherRepositoryExtensions {
@Override
public String getImplementationId() {
return getClass().getName();
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.config.MyOtherRepositoryExtensions#getImplementationId()
*/
@Override
public String getImplementationId() {
return getClass().getName();
}
}

24
src/test/java/org/springframework/data/repository/config/excluded/MyOtherRepositoryImpl.java

@ -1,10 +1,26 @@ @@ -1,10 +1,26 @@
/*
* Copyright 2017 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.repository.config.excluded;
import org.springframework.data.repository.config.MyOtherRepositoryExtensions;
public class MyOtherRepositoryImpl implements MyOtherRepositoryExtensions {
@Override
public String getImplementationId() {
return getClass().getName();
}
@Override
public String getImplementationId() {
return getClass().getName();
}
}

Loading…
Cancel
Save