1 /*
2  * Copyright 2012-2020 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.data.repository.config;
17
18 import lombok.NonNull;
19 import lombok.RequiredArgsConstructor;
20
21 import java.util.Optional;
22
23 import org.springframework.beans.factory.config.BeanDefinition;
24 import org.springframework.core.type.classreading.MetadataReaderFactory;
25 import org.springframework.core.type.filter.TypeFilter;
26 import org.springframework.data.config.ConfigurationUtils;
27 import org.springframework.data.repository.query.QueryLookupStrategy.Key;
28 import org.springframework.data.util.Streamable;
29 import org.springframework.lang.Nullable;
30 import org.springframework.util.Assert;
31 import org.springframework.util.ClassUtils;
32 import org.springframework.util.StringUtils;
33
34 /**
35  * Default implementation of {@link RepositoryConfiguration}.
36  *
37  * @author Oliver Gierke
38  * @author Jens Schauder
39  * @author Mark Paluch
40  */

41 @RequiredArgsConstructor
42 public class DefaultRepositoryConfiguration<T extends RepositoryConfigurationSource>
43         implements RepositoryConfiguration<T> {
44
45     public static final String DEFAULT_REPOSITORY_IMPLEMENTATION_POSTFIX = "Impl";
46     public static final Key DEFAULT_QUERY_LOOKUP_STRATEGY = Key.CREATE_IF_NOT_FOUND;
47
48     private final @NonNull T configurationSource;
49     private final @NonNull BeanDefinition definition;
50     private final @NonNull RepositoryConfigurationExtension extension;
51
52     /*
53      * (non-Javadoc)
54      * @see org.springframework.data.repository.config.RepositoryConfiguration#getBeanId()
55      */

56     public String getBeanId() {
57         return StringUtils.uncapitalize(ClassUtils.getShortName(getRepositoryBaseClassName().orElseThrow(
58                 () -> new IllegalStateException("Can't create bean identifier without a repository base class defined!"))));
59     }
60
61     /*
62      * (non-Javadoc)
63      * @see org.springframework.data.repository.config.RepositoryConfiguration#getQueryLookupStrategyKey()
64      */

65     public Object getQueryLookupStrategyKey() {
66         return configurationSource.getQueryLookupStrategyKey().orElse(DEFAULT_QUERY_LOOKUP_STRATEGY);
67     }
68
69     /*
70      * (non-Javadoc)
71      * @see org.springframework.data.repository.config.RepositoryConfiguration#getBasePackages()
72      */

73     public Streamable<String> getBasePackages() {
74         return configurationSource.getBasePackages();
75     }
76
77     /*
78      * (non-Javadoc)
79      * @see org.springframework.data.repository.config.RepositoryConfiguration#getImplementationBasePackages()
80      */

81     @Override
82     public Streamable<String> getImplementationBasePackages() {
83         return Streamable.of(ClassUtils.getPackageName(getRepositoryInterface()));
84     }
85
86     /*
87      * (non-Javadoc)
88      * @see org.springframework.data.repository.config.RepositoryConfiguration#getRepositoryInterface()
89      */

90     public String getRepositoryInterface() {
91         return ConfigurationUtils.getRequiredBeanClassName(definition);
92     }
93
94     /*
95      * (non-Javadoc)
96      * @see org.springframework.data.repository.config.RepositoryConfiguration#getConfigSource()
97      */

98     public RepositoryConfigurationSource getConfigSource() {
99         return configurationSource;
100     }
101
102     /*
103      * (non-Javadoc)
104      * @see org.springframework.data.repository.config.RepositoryConfiguration#getNamedQueryLocation()
105      */

106     public Optional<String> getNamedQueriesLocation() {
107         return configurationSource.getNamedQueryLocation();
108     }
109
110     /*
111      * (non-Javadoc)
112      * @see org.springframework.data.repository.config.RepositoryConfiguration#getImplementationClassName()
113      */

114     public String getImplementationClassName() {
115         return ClassUtils.getShortName(getRepositoryInterface()).concat(
116                 configurationSource.getRepositoryImplementationPostfix().orElse(DEFAULT_REPOSITORY_IMPLEMENTATION_POSTFIX));
117     }
118
119     /*
120      * (non-Javadoc)
121      * @see org.springframework.data.repository.config.RepositoryConfiguration#getImplementationBeanName()
122      */

123     public String getImplementationBeanName() {
124         return configurationSource.generateBeanName(definition)
125                 + configurationSource.getRepositoryImplementationPostfix().orElse("Impl");
126     }
127
128     /*
129      * (non-Javadoc)
130      * @see org.springframework.data.repository.config.RepositoryConfiguration#getSource()
131      */

132     @Nullable
133     @Override
134     public Object getSource() {
135         return configurationSource.getSource();
136     }
137
138     /*
139      * (non-Javadoc)
140      * @see org.springframework.data.repository.config.RepositoryConfiguration#getConfigurationSource()
141      */

142     @Override
143     public T getConfigurationSource() {
144         return configurationSource;
145     }
146
147     /*
148      * (non-Javadoc)
149      * @see org.springframework.data.repository.config.RepositoryConfiguration#getRepositoryBaseClassName()
150      */

151     @Override
152     public Optional<String> getRepositoryBaseClassName() {
153         return configurationSource.getRepositoryBaseClassName();
154     }
155
156     /*
157      * (non-Javadoc)
158      * @see org.springframework.data.repository.config.RepositoryConfiguration#getRepositoryFactoryBeanClassName()
159      */

160     @Override
161     public String getRepositoryFactoryBeanClassName() {
162
163         return configurationSource.getRepositoryFactoryBeanClassName()
164                 .orElseGet(extension::getRepositoryFactoryBeanClassName);
165     }
166
167     /*
168      * (non-Javadoc)
169      * @see org.springframework.data.repository.config.RepositoryConfiguration#isLazyInit()
170      */

171     @Override
172     public boolean isLazyInit() {
173         return definition.isLazyInit() || !configurationSource.getBootstrapMode().equals(BootstrapMode.DEFAULT);
174     }
175
176     /*
177      * (non-Javadoc)
178      * @see org.springframework.data.repository.config.RepositoryConfiguration#isPrimary()
179      */

180     @Override
181     public boolean isPrimary() {
182         return definition.isPrimary();
183     }
184
185     /*
186      * (non-Javadoc)
187      * @see org.springframework.data.repository.config.RepositoryConfiguration#getExcludeFilters()
188      */

189     @Override
190     public Streamable<TypeFilter> getExcludeFilters() {
191         return configurationSource.getExcludeFilters();
192     }
193
194     /*
195      * (non-Javadoc)
196      * @see org.springframework.data.repository.config.RepositoryConfiguration#toImplementationDetectionConfiguration(org.springframework.core.type.classreading.MetadataReaderFactory)
197      */

198     @Override
199     public ImplementationDetectionConfiguration toImplementationDetectionConfiguration(MetadataReaderFactory factory) {
200
201         Assert.notNull(factory, "MetadataReaderFactory must not be null!");
202
203         return configurationSource.toImplementationDetectionConfiguration(factory);
204     }
205
206     /*
207      * (non-Javadoc)
208      * @see org.springframework.data.repository.config.RepositoryConfiguration#toLookupConfiguration(org.springframework.core.type.classreading.MetadataReaderFactory)
209      */

210     @Override
211     public ImplementationLookupConfiguration toLookupConfiguration(MetadataReaderFactory factory) {
212
213         Assert.notNull(factory, "MetadataReaderFactory must not be null!");
214
215         return toImplementationDetectionConfiguration(factory).forRepositoryConfiguration(this);
216     }
217
218     /*
219      * (non-Javadoc)
220      * @see org.springframework.data.repository.config.RepositoryConfiguration#getResourceDescription()
221      */

222     @Override
223     @org.springframework.lang.NonNull
224     public String getResourceDescription() {
225         return String.format("%s defined in %s", getRepositoryInterface(), configurationSource.getResourceDescription());
226     }
227 }
228