1818import static org .springframework .data .config .ParsingUtils .*;
1919import static org .springframework .data .mongodb .config .MongoParsingUtils .*;
2020
21+ import java .util .Collections ;
22+ import java .util .HashSet ;
23+ import java .util .Set ;
24+
2125import org .springframework .beans .factory .BeanDefinitionStoreException ;
2226import org .springframework .beans .factory .config .BeanDefinition ;
2327import org .springframework .beans .factory .parsing .BeanComponentDefinition ;
3943
4044/**
4145 * {@link BeanDefinitionParser} to parse {@code db-factory} elements into {@link BeanDefinition}s.
42- *
46+ *
4347 * @author Jon Brisbin
4448 * @author Oliver Gierke
4549 * @author Thomas Darimont
4650 * @author Christoph Strobl
4751 * @author Viktor Khoroshko
4852 */
4953public class MongoDbFactoryParser extends AbstractBeanDefinitionParser {
54+ private static final Set <String > MONGO_URI_ALLOWED_ADDITIONAL_ATTRIBUTES ;
55+
56+ static {
57+ Set <String > mongoUriAllowedAdditionalAttributes = new HashSet <String >();
58+ mongoUriAllowedAdditionalAttributes .add ("id" );
59+ mongoUriAllowedAdditionalAttributes .add ("write-concern" );
60+
61+ MONGO_URI_ALLOWED_ADDITIONAL_ATTRIBUTES = Collections .unmodifiableSet (mongoUriAllowedAdditionalAttributes );
62+ }
5063
5164 /*
5265 * (non-Javadoc)
@@ -74,10 +87,14 @@ protected AbstractBeanDefinition parseInternal(Element element, ParserContext pa
7487 BeanDefinition mongoUri = getMongoUri (element );
7588
7689 if (mongoUri != null ) {
77- if (element .hasAttribute ("mongo-ref" ) || element .hasAttribute ("dbname" )
78- || element .hasAttribute ("authentication-dbname" )
79- || element .hasAttribute ("port" ) || element .hasAttribute ("host" )
80- || element .hasAttribute ("username" ) || element .hasAttribute ("password" )) {
90+ int allowedAttributesCount = 1 ;
91+ for (String attribute : MONGO_URI_ALLOWED_ADDITIONAL_ATTRIBUTES ) {
92+ if (element .hasAttribute (attribute )) {
93+ allowedAttributesCount ++;
94+ }
95+ }
96+
97+ if (element .getAttributes ().getLength () > allowedAttributesCount ) {
8198 parserContext .getReaderContext ().error ("Configure either Mongo URI or details individually!" ,
8299 parserContext .extractSource (element ));
83100 }
@@ -115,7 +132,7 @@ protected AbstractBeanDefinition parseInternal(Element element, ParserContext pa
115132 /**
116133 * Registers a default {@link BeanDefinition} of a {@link Mongo} instance and returns the name under which the
117134 * {@link Mongo} instance was registered under.
118- *
135+ *
119136 * @param element must not be {@literal null}.
120137 * @param parserContext must not be {@literal null}.
121138 * @return
@@ -131,7 +148,7 @@ private BeanDefinition registerMongoBeanDefinition(Element element, ParserContex
131148
132149 /**
133150 * Returns a {@link BeanDefinition} for a {@link UserCredentials} object.
134- *
151+ *
135152 * @param element
136153 * @return the {@link BeanDefinition} or {@literal null} if neither username nor password given.
137154 */
@@ -154,7 +171,7 @@ private BeanDefinition getUserCredentialsBeanDefinition(Element element, ParserC
154171 /**
155172 * Creates a {@link BeanDefinition} for a {@link MongoURI} or {@link MongoClientURI} depending on configured
156173 * attributes.
157- *
174+ *
158175 * @param element must not be {@literal null}.
159176 * @return {@literal null} in case no client-/uri defined.
160177 */
0 commit comments