From 7d8a2b2d56c112d6fc690f2d6ab72f741d7da0bc Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Tue, 19 May 2015 09:57:10 +0200 Subject: [PATCH] DATAMONGO-1218 - Deprecate non-MongoClient related configuration options in XML namespace. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We added deprecation hints to the description sections of elements and attributes within the spring-mongo.xsd of 1.7. Also we’ve added (for 1.8) a configuration attribute to db-factory allowing to set a client-uri creating a MongoClientURI instead of a MongoURI that will be passed on to MongoDbFactory. Just as 'uri', 'client-uri' will not allow additional configuration options like username, password next to it. Original pull request: #296 --- .../mongodb/config/MongoDbFactoryParser.java | 53 +- .../main/resources/META-INF/spring.schemas | 3 +- .../data/mongodb/config/spring-mongo-1.7.xsd | 34 +- .../data/mongodb/config/spring-mongo-1.8.xsd | 894 ++++++++++++++++++ .../MongoDbFactoryParserIntegrationTests.java | 24 + .../mongo-client-uri-and-details.xml | 10 + .../resources/namespace/mongo-client-uri.xml | 10 + 7 files changed, 999 insertions(+), 29 deletions(-) create mode 100644 spring-data-mongodb/src/main/resources/org/springframework/data/mongodb/config/spring-mongo-1.8.xsd create mode 100644 spring-data-mongodb/src/test/resources/namespace/mongo-client-uri-and-details.xml create mode 100644 spring-data-mongodb/src/test/resources/namespace/mongo-client-uri.xml diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoDbFactoryParser.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoDbFactoryParser.java index 7a23c03fe..629423a6b 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoDbFactoryParser.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoDbFactoryParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2014 by the original author(s). + * Copyright 2011-2015 by the original author(s). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,6 +34,7 @@ import org.springframework.util.StringUtils; import org.w3c.dom.Element; import com.mongodb.Mongo; +import com.mongodb.MongoClientURI; import com.mongodb.MongoURI; /** @@ -42,6 +43,7 @@ import com.mongodb.MongoURI; * @author Jon Brisbin * @author Oliver Gierke * @author Thomas Darimont + * @author Christoph Strobl */ public class MongoDbFactoryParser extends AbstractBeanDefinitionParser { @@ -64,29 +66,28 @@ public class MongoDbFactoryParser extends AbstractBeanDefinitionParser { @Override protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) { - Object source = parserContext.extractSource(element); - - BeanComponentDefinitionBuilder helper = new BeanComponentDefinitionBuilder(element, parserContext); - - String uri = element.getAttribute("uri"); - String mongoRef = element.getAttribute("mongo-ref"); - String dbname = element.getAttribute("dbname"); - - BeanDefinition userCredentials = getUserCredentialsBeanDefinition(element, parserContext); - // Common setup BeanDefinitionBuilder dbFactoryBuilder = BeanDefinitionBuilder.genericBeanDefinition(SimpleMongoDbFactory.class); setPropertyValue(dbFactoryBuilder, element, "write-concern", "writeConcern"); - if (StringUtils.hasText(uri)) { - if (StringUtils.hasText(mongoRef) || StringUtils.hasText(dbname) || userCredentials != null) { - parserContext.getReaderContext().error("Configure either Mongo URI or details individually!", source); - } + BeanDefinition mongoUri = getMongoUri(element); - dbFactoryBuilder.addConstructorArgValue(getMongoUri(uri)); + if (mongoUri != null) { + if (element.getAttributes().getLength() >= 2 && !element.hasAttribute("write-concern")) { + parserContext.getReaderContext().error("Configure either Mongo URI or details individually!", + parserContext.extractSource(element)); + } + dbFactoryBuilder.addConstructorArgValue(mongoUri); return getSourceBeanDefinition(dbFactoryBuilder, parserContext, element); } + BeanComponentDefinitionBuilder helper = new BeanComponentDefinitionBuilder(element, parserContext); + + String mongoRef = element.getAttribute("mongo-ref"); + String dbname = element.getAttribute("dbname"); + + BeanDefinition userCredentials = getUserCredentialsBeanDefinition(element, parserContext); + // Defaulting if (StringUtils.hasText(mongoRef)) { dbFactoryBuilder.addConstructorArgReference(mongoRef); @@ -147,14 +148,24 @@ public class MongoDbFactoryParser extends AbstractBeanDefinitionParser { } /** - * Creates a {@link BeanDefinition} for a {@link MongoURI}. + * Creates a {@link BeanDefinition} for a {@link MongoURI} or {@link MongoClientURI} depending on configured + * attributes. * - * @param uri - * @return + * @param element must not be {@literal null}. + * @return {@literal null} in case no client-/uri defined. */ - private BeanDefinition getMongoUri(String uri) { + private BeanDefinition getMongoUri(Element element) { + + boolean hasClientUri = element.hasAttribute("client-uri"); + + if (!hasClientUri && !element.hasAttribute("uri")) { + return null; + } + + Class type = hasClientUri ? MongoClientURI.class : MongoURI.class; + String uri = hasClientUri ? element.getAttribute("client-uri") : element.getAttribute("uri"); - BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(MongoURI.class); + BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(type); builder.addConstructorArgValue(uri); return builder.getBeanDefinition(); diff --git a/spring-data-mongodb/src/main/resources/META-INF/spring.schemas b/spring-data-mongodb/src/main/resources/META-INF/spring.schemas index 5c6d28969..26a5caba1 100644 --- a/spring-data-mongodb/src/main/resources/META-INF/spring.schemas +++ b/spring-data-mongodb/src/main/resources/META-INF/spring.schemas @@ -5,4 +5,5 @@ http\://www.springframework.org/schema/data/mongo/spring-mongo-1.3.xsd=org/sprin http\://www.springframework.org/schema/data/mongo/spring-mongo-1.4.xsd=org/springframework/data/mongodb/config/spring-mongo-1.4.xsd http\://www.springframework.org/schema/data/mongo/spring-mongo-1.5.xsd=org/springframework/data/mongodb/config/spring-mongo-1.5.xsd http\://www.springframework.org/schema/data/mongo/spring-mongo-1.7.xsd=org/springframework/data/mongodb/config/spring-mongo-1.7.xsd -http\://www.springframework.org/schema/data/mongo/spring-mongo.xsd=org/springframework/data/mongodb/config/spring-mongo-1.7.xsd +http\://www.springframework.org/schema/data/mongo/spring-mongo-1.8.xsd=org/springframework/data/mongodb/config/spring-mongo-1.8.xsd +http\://www.springframework.org/schema/data/mongo/spring-mongo.xsd=org/springframework/data/mongodb/config/spring-mongo-1.8.xsd diff --git a/spring-data-mongodb/src/main/resources/org/springframework/data/mongodb/config/spring-mongo-1.7.xsd b/spring-data-mongodb/src/main/resources/org/springframework/data/mongodb/config/spring-mongo-1.7.xsd index d70e26be5..82602b33f 100644 --- a/spring-data-mongodb/src/main/resources/org/springframework/data/mongodb/config/spring-mongo-1.7.xsd +++ b/spring-data-mongodb/src/main/resources/org/springframework/data/mongodb/config/spring-mongo-1.7.xsd @@ -18,7 +18,7 @@ @@ -31,7 +31,7 @@ Defines a Mongo instance used for accessing MongoDB'. @@ -72,14 +72,14 @@ The name of the database to connect to. Default is 'db'. @@ -93,14 +93,14 @@ The host to connect to a MongoDB server. Default is localhost @@ -382,6 +382,11 @@ The name of the Mongo object that determines what server to monitor. (by default --> + + + @@ -439,6 +444,11 @@ The comma delimited list of host:port entries to use for replica set/pairs. + + + + + + @@ -593,10 +608,15 @@ The comma delimited list of username:password@database entries to use for authen + + + diff --git a/spring-data-mongodb/src/main/resources/org/springframework/data/mongodb/config/spring-mongo-1.8.xsd b/spring-data-mongodb/src/main/resources/org/springframework/data/mongodb/config/spring-mongo-1.8.xsd new file mode 100644 index 000000000..2beb2fc55 --- /dev/null +++ b/spring-data-mongodb/src/main/resources/org/springframework/data/mongodb/config/spring-mongo-1.8.xsd @@ -0,0 +1,894 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + The WriteConcern that will be the default value used when asking the MongoDbFactory for a DB object + + + + + + + + + + + + + + The reference to a MongoTemplate. Will default to 'mongoTemplate'. + + + + + + + Enables creation of indexes for queries that get derived from the method name + and thus reference domain class properties. Defaults to false. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + The reference to a DbFactory. + + + + + + + + + + + + The reference to a MongoTypeMapper to be used by this MappingMongoConverter. + + + + + + + The reference to a MappingContext. Will default to 'mappingContext'. + + + + + + + Disables JSR-303 validation on MongoDB documents before they are saved. By default it is set to false. + + + + + + + + + + Enables abbreviating the field names for domain class properties to the + first character of their camel case names, e.g. fooBar -> fb. Defaults to false. + + + + + + + + + + The reference to a FieldNamingStrategy. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + The WriteConcern that will be the default value used when asking the MongoDbFactory for a DB object + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + A reference to a custom converter. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + The reference to a DbFactory. + + + + + + + + + + + + The WriteConcern that will be the default value used when asking the MongoDbFactory for a DB object + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + The reference to a DbFactory. + + + + + + + + + + + + + + + + diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/MongoDbFactoryParserIntegrationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/MongoDbFactoryParserIntegrationTests.java index b81c4942d..5cab74f13 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/MongoDbFactoryParserIntegrationTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/MongoDbFactoryParserIntegrationTests.java @@ -41,6 +41,7 @@ import org.springframework.test.util.ReflectionTestUtils; import com.mongodb.DB; import com.mongodb.Mongo; import com.mongodb.MongoClient; +import com.mongodb.MongoClientURI; import com.mongodb.MongoURI; import com.mongodb.WriteConcern; @@ -174,6 +175,29 @@ public class MongoDbFactoryParserIntegrationTests { reader.loadBeanDefinitions(new ClassPathResource("namespace/mongo-uri-and-details.xml")); } + /** + * @see DATAMONGO-1218 + */ + @Test + public void setsUpMongoDbFactoryUsingAMongoClientUri() { + + reader.loadBeanDefinitions(new ClassPathResource("namespace/mongo-client-uri.xml")); + BeanDefinition definition = factory.getBeanDefinition("mongoDbFactory"); + ConstructorArgumentValues constructorArguments = definition.getConstructorArgumentValues(); + + assertThat(constructorArguments.getArgumentCount(), is(1)); + ValueHolder argument = constructorArguments.getArgumentValue(0, MongoClientURI.class); + assertThat(argument, is(notNullValue())); + } + + /** + * @see DATAMONGO-1218 + */ + @Test(expected = BeanDefinitionParsingException.class) + public void rejectsClientUriPlusDetailedConfiguration() { + reader.loadBeanDefinitions(new ClassPathResource("namespace/mongo-client-uri-and-details.xml")); + } + private static void assertWriteConcern(ClassPathXmlApplicationContext ctx, WriteConcern expectedWriteConcern) { SimpleMongoDbFactory dbFactory = ctx.getBean("first", SimpleMongoDbFactory.class); diff --git a/spring-data-mongodb/src/test/resources/namespace/mongo-client-uri-and-details.xml b/spring-data-mongodb/src/test/resources/namespace/mongo-client-uri-and-details.xml new file mode 100644 index 000000000..c9c9f622e --- /dev/null +++ b/spring-data-mongodb/src/test/resources/namespace/mongo-client-uri-and-details.xml @@ -0,0 +1,10 @@ + + + + + + diff --git a/spring-data-mongodb/src/test/resources/namespace/mongo-client-uri.xml b/spring-data-mongodb/src/test/resources/namespace/mongo-client-uri.xml new file mode 100644 index 000000000..3fa5daff8 --- /dev/null +++ b/spring-data-mongodb/src/test/resources/namespace/mongo-client-uri.xml @@ -0,0 +1,10 @@ + + + + + +