Skip to content

Commit e1869ab

Browse files
DATAMONGO-1293 - Polishing.
Move configuration parsing error into method actually responsible for reading uri/client-uri attributes. Original Pull Request: #328
1 parent c7be5bf commit e1869ab

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoDbFactoryParser.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@
5151
* @author Viktor Khoroshko
5252
*/
5353
public class MongoDbFactoryParser extends AbstractBeanDefinitionParser {
54+
5455
private static final Set<String> MONGO_URI_ALLOWED_ADDITIONAL_ATTRIBUTES;
5556

5657
static {
58+
5759
Set<String> mongoUriAllowedAdditionalAttributes = new HashSet<String>();
5860
mongoUriAllowedAdditionalAttributes.add("id");
5961
mongoUriAllowedAdditionalAttributes.add("write-concern");
@@ -84,20 +86,10 @@ protected AbstractBeanDefinition parseInternal(Element element, ParserContext pa
8486
BeanDefinitionBuilder dbFactoryBuilder = BeanDefinitionBuilder.genericBeanDefinition(SimpleMongoDbFactory.class);
8587
setPropertyValue(dbFactoryBuilder, element, "write-concern", "writeConcern");
8688

87-
BeanDefinition mongoUri = getMongoUri(element);
89+
BeanDefinition mongoUri = getMongoUri(element, parserContext);
8890

8991
if (mongoUri != null) {
90-
int allowedAttributesCount = 1;
91-
for (String attribute : MONGO_URI_ALLOWED_ADDITIONAL_ATTRIBUTES) {
92-
if (element.hasAttribute(attribute)) {
93-
allowedAttributesCount++;
94-
}
95-
}
9692

97-
if (element.getAttributes().getLength() > allowedAttributesCount) {
98-
parserContext.getReaderContext().error("Configure either Mongo URI or details individually!",
99-
parserContext.extractSource(element));
100-
}
10193
dbFactoryBuilder.addConstructorArgValue(mongoUri);
10294
return getSourceBeanDefinition(dbFactoryBuilder, parserContext, element);
10395
}
@@ -170,19 +162,37 @@ private BeanDefinition getUserCredentialsBeanDefinition(Element element, ParserC
170162

171163
/**
172164
* Creates a {@link BeanDefinition} for a {@link MongoURI} or {@link MongoClientURI} depending on configured
173-
* attributes.
165+
* attributes. <br />
166+
* Errors when configured element contains {@literal uri} or {@literal client-uri} along with other attributes except
167+
* {@literal write-concern} and/or {@literal id}.
174168
*
175169
* @param element must not be {@literal null}.
170+
* @param parserContext
176171
* @return {@literal null} in case no client-/uri defined.
177172
*/
178-
private BeanDefinition getMongoUri(Element element) {
173+
private BeanDefinition getMongoUri(Element element, ParserContext parserContext) {
179174

180175
boolean hasClientUri = element.hasAttribute("client-uri");
181176

182177
if (!hasClientUri && !element.hasAttribute("uri")) {
183178
return null;
184179
}
185180

181+
int allowedAttributesCount = 1;
182+
for (String attribute : MONGO_URI_ALLOWED_ADDITIONAL_ATTRIBUTES) {
183+
184+
if (element.hasAttribute(attribute)) {
185+
allowedAttributesCount++;
186+
}
187+
}
188+
189+
if (element.getAttributes().getLength() > allowedAttributesCount) {
190+
191+
parserContext.getReaderContext().error(
192+
"Configure either " + (hasClientUri ? "Mongo Client URI" : "Mongo URI") + " or details individually!",
193+
parserContext.extractSource(element));
194+
}
195+
186196
Class<?> type = hasClientUri ? MongoClientURI.class : MongoURI.class;
187197
String uri = hasClientUri ? element.getAttribute("client-uri") : element.getAttribute("uri");
188198

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/MongoDbFactoryParserIntegrationTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ public void rejectsClientUriPlusDetailedConfiguration() {
204204
*/
205205
@Test
206206
public void setsUpClientUriWithId() {
207+
207208
reader.loadBeanDefinitions(new ClassPathResource("namespace/mongo-client-uri-and-id.xml"));
208209
BeanDefinition definition = factory.getBeanDefinition("testMongo");
209210
ConstructorArgumentValues constructorArguments = definition.getConstructorArgumentValues();
@@ -218,6 +219,7 @@ public void setsUpClientUriWithId() {
218219
*/
219220
@Test
220221
public void setsUpUriWithId() {
222+
221223
reader.loadBeanDefinitions(new ClassPathResource("namespace/mongo-uri-and-id.xml"));
222224
BeanDefinition definition = factory.getBeanDefinition("testMongo");
223225
ConstructorArgumentValues constructorArguments = definition.getConstructorArgumentValues();

0 commit comments

Comments
 (0)