1515 */
1616package org .springframework .data .repository .cdi ;
1717
18+ import lombok .Getter ;
1819import lombok .RequiredArgsConstructor ;
1920
20- import java .io .IOException ;
21- import java .util .Arrays ;
22- import java .util .Collections ;
2321import java .util .Optional ;
2422import java .util .stream .Stream ;
2523
26- import javax .enterprise .inject .CreationException ;
2724import javax .enterprise .inject .UnsatisfiedResolutionException ;
2825
29- import org .springframework .beans .factory .config .BeanDefinition ;
3026import org .springframework .beans .factory .support .AbstractBeanDefinition ;
31- import org .springframework .core .env .Environment ;
3227import org .springframework .core .env .StandardEnvironment ;
3328import org .springframework .core .io .ResourceLoader ;
3429import org .springframework .core .io .support .PathMatchingResourcePatternResolver ;
35- import org .springframework .core .type .ClassMetadata ;
3630import org .springframework .core .type .classreading .CachingMetadataReaderFactory ;
3731import org .springframework .core .type .classreading .MetadataReaderFactory ;
38- import org .springframework .core .type .filter .AnnotationTypeFilter ;
3932import org .springframework .core .type .filter .TypeFilter ;
40- import org .springframework .data .repository .NoRepositoryBean ;
4133import org .springframework .data .repository .config .CustomRepositoryImplementationDetector ;
4234import org .springframework .data .repository .config .FragmentMetadata ;
35+ import org .springframework .data .repository .config .ImplementationDetectionConfiguration ;
36+ import org .springframework .data .repository .config .ImplementationLookupConfiguration ;
4337import org .springframework .data .repository .config .RepositoryFragmentConfiguration ;
44- import org .springframework .data .repository .config .RepositoryFragmentDiscovery ;
4538import org .springframework .data .util .Optionals ;
4639import org .springframework .data .util .Streamable ;
4740import org .springframework .lang .Nullable ;
@@ -61,6 +54,7 @@ public class CdiRepositoryContext {
6154 private final ClassLoader classLoader ;
6255 private final CustomRepositoryImplementationDetector detector ;
6356 private final MetadataReaderFactory metadataReaderFactory ;
57+ private final FragmentMetadata metdata ;
6458
6559 /**
6660 * Create a new {@link CdiRepositoryContext} given {@link ClassLoader} and initialize
@@ -69,16 +63,8 @@ public class CdiRepositoryContext {
6963 * @param classLoader must not be {@literal null}.
7064 */
7165 public CdiRepositoryContext (ClassLoader classLoader ) {
72-
73- Assert .notNull (classLoader , "ClassLoader must not be null!" );
74-
75- this .classLoader = classLoader ;
76-
77- Environment environment = new StandardEnvironment ();
78- ResourceLoader resourceLoader = new PathMatchingResourcePatternResolver (classLoader );
79-
80- this .metadataReaderFactory = new CachingMetadataReaderFactory (resourceLoader );
81- this .detector = new CustomRepositoryImplementationDetector (metadataReaderFactory , environment , resourceLoader );
66+ this (classLoader , new CustomRepositoryImplementationDetector (new StandardEnvironment (),
67+ new PathMatchingResourcePatternResolver (classLoader )));
8268 }
8369
8470 /**
@@ -97,6 +83,7 @@ public CdiRepositoryContext(ClassLoader classLoader, CustomRepositoryImplementat
9783
9884 this .classLoader = classLoader ;
9985 this .metadataReaderFactory = new CachingMetadataReaderFactory (resourceLoader );
86+ this .metdata = new FragmentMetadata (metadataReaderFactory );
10087 this .detector = detector ;
10188 }
10289
@@ -130,14 +117,11 @@ Class<?> loadClass(String className) {
130117 Stream <RepositoryFragmentConfiguration > getRepositoryFragments (CdiRepositoryConfiguration configuration ,
131118 Class <?> repositoryInterface ) {
132119
133- ClassMetadata classMetadata = getClassMetadata (metadataReaderFactory , repositoryInterface .getName ());
134-
135- RepositoryFragmentDiscovery fragmentConfiguration = new CdiRepositoryFragmentDiscovery (configuration );
120+ CdiImplementationDetectionConfiguration config = new CdiImplementationDetectionConfiguration (configuration ,
121+ metadataReaderFactory );
136122
137- return Arrays .stream (classMetadata .getInterfaceNames ()) //
138- .filter (it -> FragmentMetadata .isCandidate (it , metadataReaderFactory )) //
139- .map (it -> FragmentMetadata .of (it , fragmentConfiguration )) //
140- .map (this ::detectRepositoryFragmentConfiguration ) //
123+ return metdata .getFragmentInterfaces (repositoryInterface .getName ()) //
124+ .map (it -> detectRepositoryFragmentConfiguration (it , config )) //
141125 .flatMap (Optionals ::toStream );
142126 }
143127
@@ -152,26 +136,22 @@ Stream<RepositoryFragmentConfiguration> getRepositoryFragments(CdiRepositoryConf
152136 Optional <Class <?>> getCustomImplementationClass (Class <?> repositoryType ,
153137 CdiRepositoryConfiguration cdiRepositoryConfiguration ) {
154138
155- String className = getCustomImplementationClassName (repositoryType , cdiRepositoryConfiguration );
139+ ImplementationDetectionConfiguration configuration = new CdiImplementationDetectionConfiguration (
140+ cdiRepositoryConfiguration , metadataReaderFactory );
141+ ImplementationLookupConfiguration lookup = configuration .forFragment (repositoryType .getName ());
156142
157- Optional <AbstractBeanDefinition > beanDefinition = detector .detectCustomImplementation ( //
158- className , //
159- className , Collections .singleton (repositoryType .getPackage ().getName ()), //
160- Collections .emptySet (), //
161- BeanDefinition ::getBeanClassName );
143+ Optional <AbstractBeanDefinition > beanDefinition = detector .detectCustomImplementation (lookup );
162144
163145 return beanDefinition .map (this ::loadBeanClass );
164146 }
165147
166- private Optional <RepositoryFragmentConfiguration > detectRepositoryFragmentConfiguration (
167- FragmentMetadata configuration ) {
148+ private Optional <RepositoryFragmentConfiguration > detectRepositoryFragmentConfiguration (String fragmentInterfaceName ,
149+ CdiImplementationDetectionConfiguration config ) {
168150
169- String className = configuration .getFragmentImplementationClassName ();
151+ ImplementationLookupConfiguration lookup = config .forFragment (fragmentInterfaceName );
152+ Optional <AbstractBeanDefinition > beanDefinition = detector .detectCustomImplementation (lookup );
170153
171- Optional <AbstractBeanDefinition > beanDefinition = detector .detectCustomImplementation (className , null ,
172- configuration .getBasePackages (), configuration .getExclusions (), BeanDefinition ::getBeanClassName );
173-
174- return beanDefinition .map (bd -> new RepositoryFragmentConfiguration (configuration .getFragmentInterfaceName (), bd ));
154+ return beanDefinition .map (bd -> new RepositoryFragmentConfiguration (fragmentInterfaceName , bd ));
175155 }
176156
177157 @ Nullable
@@ -182,45 +162,37 @@ private Class<?> loadBeanClass(AbstractBeanDefinition definition) {
182162 return beanClassName == null ? null : loadClass (beanClassName );
183163 }
184164
185- private static ClassMetadata getClassMetadata (MetadataReaderFactory metadataReaderFactory , String className ) {
186-
187- try {
188- return metadataReaderFactory .getMetadataReader (className ).getClassMetadata ();
189- } catch (IOException e ) {
190- throw new CreationException (String .format ("Cannot parse %s metadata." , className ), e );
191- }
192- }
193-
194- private static String getCustomImplementationClassName (Class <?> repositoryType ,
195- CdiRepositoryConfiguration cdiRepositoryConfiguration ) {
196-
197- String configuredPostfix = cdiRepositoryConfiguration .getRepositoryImplementationPostfix ();
198- Assert .hasText (configuredPostfix , "Configured repository postfix must not be null or empty!" );
199-
200- return ClassUtils .getShortName (repositoryType ) + configuredPostfix ;
201- }
202-
203165 @ RequiredArgsConstructor
204- private static class CdiRepositoryFragmentDiscovery implements RepositoryFragmentDiscovery {
166+ private static class CdiImplementationDetectionConfiguration implements ImplementationDetectionConfiguration {
205167
206168 private final CdiRepositoryConfiguration configuration ;
169+ private final @ Getter MetadataReaderFactory metadataReaderFactory ;
207170
208171 /*
209172 * (non-Javadoc)
210- * @see org.springframework.data.repository.config.RepositoryFragmentDiscovery#getExcludeFilters ()
173+ * @see org.springframework.data.repository.config.CustomRepositoryImplementationDetector.ImplementationDetectionConfiguration#getImplementationPostfix ()
211174 */
212175 @ Override
213- public Streamable < TypeFilter > getExcludeFilters () {
214- return Streamable . of ( new AnnotationTypeFilter ( NoRepositoryBean . class ) );
176+ public String getImplementationPostfix () {
177+ return configuration . getRepositoryImplementationPostfix ( );
215178 }
216179
217180 /*
218181 * (non-Javadoc)
219- * @see org.springframework.data.repository.config.RepositoryFragmentDiscovery#getRepositoryImplementationPostfix ()
182+ * @see org.springframework.data.repository.config.CustomRepositoryImplementationDetector.ImplementationDetectionConfiguration#getBasePackages ()
220183 */
221184 @ Override
222- public Optional <String > getRepositoryImplementationPostfix () {
223- return Optional .of (configuration .getRepositoryImplementationPostfix ());
185+ public Streamable <String > getBasePackages () {
186+ return Streamable .empty ();
187+ }
188+
189+ /*
190+ * (non-Javadoc)
191+ * @see org.springframework.data.repository.config.CustomRepositoryImplementationDetector.ImplementationDetectionConfiguration#getExcludeFilters()
192+ */
193+ @ Override
194+ public Streamable <TypeFilter > getExcludeFilters () {
195+ return Streamable .empty ();
224196 }
225197 }
226198}
0 commit comments