From 20846809e1918bb9d422c7ceca966ee1d23badb3 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Wed, 12 Feb 2014 11:49:05 +0100 Subject: [PATCH] DATACMNS-439 - AnnotationRepositoryConfigSource now evaluates REGEX and ASPECTJ filters. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We now leniently detect AspectJ and regular expression filters in an @Filter annotation in @Enable…Repositories annotations. --- ...notationRepositoryConfigurationSource.java | 45 +++++++++++++++++-- ...ositoryBeanDefinitionRegistrarSupport.java | 2 +- ...epositoryConfigurationSourceUnitTests.java | 14 +++--- 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSource.java b/src/main/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSource.java index f23185554..6407af551 100644 --- a/src/main/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSource.java +++ b/src/main/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2012-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. @@ -22,14 +22,18 @@ import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.regex.Pattern; import org.springframework.beans.BeanUtils; import org.springframework.context.annotation.FilterType; import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.env.Environment; +import org.springframework.core.io.ResourceLoader; import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.filter.AnnotationTypeFilter; +import org.springframework.core.type.filter.AspectJTypeFilter; import org.springframework.core.type.filter.AssignableTypeFilter; +import org.springframework.core.type.filter.RegexPatternTypeFilter; import org.springframework.core.type.filter.TypeFilter; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; @@ -53,6 +57,7 @@ public class AnnotationRepositoryConfigurationSource extends RepositoryConfigura private final AnnotationMetadata metadata; private final AnnotationAttributes attributes; + private final ResourceLoader resourceLoader; /** * Creates a new {@link AnnotationRepositoryConfigurationSource} from the given {@link AnnotationMetadata} and @@ -60,18 +65,21 @@ public class AnnotationRepositoryConfigurationSource extends RepositoryConfigura * * @param metadata must not be {@literal null}. * @param annotation must not be {@literal null}. + * @param resourceLoader must not be {@literal null}. * @param environment */ public AnnotationRepositoryConfigurationSource(AnnotationMetadata metadata, Class annotation, - Environment environment) { + ResourceLoader resourceLoader, Environment environment) { super(environment); Assert.notNull(metadata); Assert.notNull(annotation); + Assert.notNull(resourceLoader); this.attributes = new AnnotationAttributes(metadata.getAnnotationAttributes(annotation.getName())); this.metadata = metadata; + this.resourceLoader = resourceLoader; } /* @@ -198,6 +206,7 @@ public class AnnotationRepositoryConfigurationSource extends RepositoryConfigura * @return */ private List typeFiltersFor(AnnotationAttributes filterAttributes) { + List typeFilters = new ArrayList(); FilterType filterType = filterAttributes.getEnum("type"); @@ -219,9 +228,23 @@ public class AnnotationRepositoryConfigurationSource extends RepositoryConfigura typeFilters.add(BeanUtils.instantiateClass(filterClass, TypeFilter.class)); break; default: - throw new IllegalArgumentException("unknown filter type " + filterType); + throw new IllegalArgumentException("Unknown filter type " + filterType); } } + + for (String expression : getPatterns(filterAttributes)) { + + String rawName = filterType.toString(); + + if ("REGEX".equals(rawName)) { + typeFilters.add(new RegexPatternTypeFilter(Pattern.compile(expression))); + } else if ("ASPECTJ".equals(rawName)) { + typeFilters.add(new AspectJTypeFilter(expression, this.resourceLoader.getClassLoader())); + } else { + throw new IllegalArgumentException("Unknown filter type " + filterType); + } + } + return typeFilters; } @@ -233,4 +256,20 @@ public class AnnotationRepositoryConfigurationSource extends RepositoryConfigura public boolean shouldConsiderNestedRepositories() { return attributes.containsKey(CONSIDER_NESTED_REPOSITORIES) && attributes.getBoolean(CONSIDER_NESTED_REPOSITORIES); } + + /** + * Safely reads the {@code pattern} attribute from the given {@link AnnotationAttributes} and returns an empty list if + * the attribute is not present. + * + * @param filterAttributes must not be {@literal null}. + * @return + */ + private String[] getPatterns(AnnotationAttributes filterAttributes) { + + try { + return filterAttributes.getStringArray("pattern"); + } catch (IllegalArgumentException o_O) { + return new String[0]; + } + } } diff --git a/src/main/java/org/springframework/data/repository/config/RepositoryBeanDefinitionRegistrarSupport.java b/src/main/java/org/springframework/data/repository/config/RepositoryBeanDefinitionRegistrarSupport.java index 390eb64ca..74a869664 100644 --- a/src/main/java/org/springframework/data/repository/config/RepositoryBeanDefinitionRegistrarSupport.java +++ b/src/main/java/org/springframework/data/repository/config/RepositoryBeanDefinitionRegistrarSupport.java @@ -72,7 +72,7 @@ public abstract class RepositoryBeanDefinitionRegistrarSupport implements Import } AnnotationRepositoryConfigurationSource configurationSource = new AnnotationRepositoryConfigurationSource( - annotationMetadata, getAnnotation(), environment); + annotationMetadata, getAnnotation(), resourceLoader, environment); RepositoryConfigurationDelegate delegate = new RepositoryConfigurationDelegate(configurationSource, resourceLoader); delegate.registerRepositoriesIn(registry, getExtension()); diff --git a/src/test/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSourceUnitTests.java b/src/test/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSourceUnitTests.java index 07d06671f..fd692a577 100644 --- a/src/test/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSourceUnitTests.java +++ b/src/test/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSourceUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2012-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. @@ -26,6 +26,7 @@ import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.core.env.Environment; import org.springframework.core.env.StandardEnvironment; import org.springframework.core.io.DefaultResourceLoader; +import org.springframework.core.io.ResourceLoader; import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.StandardAnnotationMetadata; @@ -39,13 +40,16 @@ public class AnnotationRepositoryConfigurationSourceUnitTests { RepositoryConfigurationSource source; Environment environment; + ResourceLoader resourceLoader; @Before public void setUp() { AnnotationMetadata annotationMetadata = new StandardAnnotationMetadata(SampleConfiguration.class, true); environment = new StandardEnvironment(); - source = new AnnotationRepositoryConfigurationSource(annotationMetadata, EnableRepositories.class, environment); + resourceLoader = new DefaultResourceLoader(); + source = new AnnotationRepositoryConfigurationSource(annotationMetadata, EnableRepositories.class, resourceLoader, + environment); } @Test @@ -70,7 +74,7 @@ public class AnnotationRepositoryConfigurationSourceUnitTests { AnnotationMetadata metadata = new StandardAnnotationMetadata(DefaultConfiguration.class); AnnotationRepositoryConfigurationSource source = new AnnotationRepositoryConfigurationSource(metadata, - EnableRepositories.class, environment); + EnableRepositories.class, resourceLoader, environment); Iterable packages = source.getBasePackages(); assertThat(packages, hasItem(DefaultConfiguration.class.getPackage().getName())); @@ -82,7 +86,7 @@ public class AnnotationRepositoryConfigurationSourceUnitTests { AnnotationMetadata metadata = new StandardAnnotationMetadata(DefaultConfigurationWithBasePackage.class); AnnotationRepositoryConfigurationSource source = new AnnotationRepositoryConfigurationSource(metadata, - EnableRepositories.class, environment); + EnableRepositories.class, resourceLoader, environment); Iterable packages = source.getBasePackages(); assertThat(packages, hasItem("foo")); @@ -96,7 +100,7 @@ public class AnnotationRepositoryConfigurationSourceUnitTests { AnnotationMetadata metadata = new StandardAnnotationMetadata(DefaultConfigurationWithNestedRepositories.class); AnnotationRepositoryConfigurationSource source = new AnnotationRepositoryConfigurationSource(metadata, - EnableRepositories.class, environment); + EnableRepositories.class, resourceLoader, environment); assertThat(source.shouldConsiderNestedRepositories(), is(true)); }