forked from spring-projects/spring-data-commons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CdiRepositoryExtensionSupport.java
138 lines (115 loc) · 4.71 KB
/
CdiRepositoryExtensionSupport.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Copyright 2011-2013 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.cdi;
import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.Any;
import javax.enterprise.inject.Default;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.ProcessAnnotatedType;
import javax.enterprise.util.AnnotationLiteral;
import javax.inject.Qualifier;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.RepositoryDefinition;
/**
* Base class for {@link Extension} implementations that create instances for Spring Data repositories.
*
* @author Dirk Mahler
* @author Oliver Gierke
*/
public abstract class CdiRepositoryExtensionSupport implements Extension {
private static final Log LOGGER = LogFactory.getLog(CdiRepositoryExtensionSupport.class);
private final Map<Class<?>, Set<Annotation>> repositoryTypes = new HashMap<Class<?>, Set<Annotation>>();
/**
* Implementation of a an observer which checks for Spring Data repository types and stores them in
* {@link #repositoryTypes} for later registration as bean type.
*
* @param <X> The type.
* @param processAnnotatedType The annotated type as defined by CDI.
*/
protected <X> void processAnnotatedType(@Observes ProcessAnnotatedType<X> processAnnotatedType) {
AnnotatedType<X> annotatedType = processAnnotatedType.getAnnotatedType();
Class<X> repositoryType = annotatedType.getJavaClass();
if (isRepository(repositoryType)) {
// Determine the qualifiers of the repository type.
Set<Annotation> qualifiers = getQualifiers(repositoryType);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("Discovered repository type '%s' with qualifiers %s.", repositoryType.getName(),
qualifiers));
}
// Store the repository type using its qualifiers.
repositoryTypes.put(repositoryType, qualifiers);
}
}
/**
* Returns whether the given type is a repository type.
*
* @param type must not be {@literal null}.
* @return
*/
private boolean isRepository(Class<?> type) {
boolean isInterface = type.isInterface();
boolean extendsRepository = Repository.class.isAssignableFrom(type);
boolean isAnnotated = type.isAnnotationPresent(RepositoryDefinition.class);
boolean excludedByAnnotation = type.isAnnotationPresent(NoRepositoryBean.class);
return isInterface && (extendsRepository || isAnnotated) && !excludedByAnnotation;
}
/**
* Determines the qualifiers of the given type.
*/
private Set<Annotation> getQualifiers(final Class<?> type) {
Set<Annotation> qualifiers = new HashSet<Annotation>();
Annotation[] annotations = type.getAnnotations();
for (Annotation annotation : annotations) {
Class<? extends Annotation> annotationType = annotation.annotationType();
if (annotationType.isAnnotationPresent(Qualifier.class)) {
qualifiers.add(annotation);
}
}
// Add @Default qualifier if no qualifier is specified.
if (qualifiers.isEmpty()) {
qualifiers.add(DefaultAnnotationLiteral.INSTANCE);
}
// Add @Any qualifier.
qualifiers.add(AnyAnnotationLiteral.INSTANCE);
return qualifiers;
}
/**
* Provides access to all repository types as well as their qualifiers.
*
* @return
*/
protected Iterable<Entry<Class<?>, Set<Annotation>>> getRepositoryTypes() {
return repositoryTypes.entrySet();
}
private static class DefaultAnnotationLiteral extends AnnotationLiteral<Default> implements Default {
private static final long serialVersionUID = 511359421048623933L;
private static final DefaultAnnotationLiteral INSTANCE = new DefaultAnnotationLiteral();
}
private static class AnyAnnotationLiteral extends AnnotationLiteral<Any> implements Any {
private static final long serialVersionUID = 7261821376671361463L;
private static final AnyAnnotationLiteral INSTANCE = new AnyAnnotationLiteral();
}
}