18
18
import static org .springframework .data .config .ParsingUtils .*;
19
19
import static org .springframework .data .mongodb .config .MongoParsingUtils .*;
20
20
21
+ import java .util .Collections ;
22
+ import java .util .HashSet ;
23
+ import java .util .Set ;
24
+
21
25
import org .springframework .beans .factory .BeanDefinitionStoreException ;
22
26
import org .springframework .beans .factory .config .BeanDefinition ;
23
27
import org .springframework .beans .factory .parsing .BeanComponentDefinition ;
39
43
40
44
/**
41
45
* {@link BeanDefinitionParser} to parse {@code db-factory} elements into {@link BeanDefinition}s.
42
- *
46
+ *
43
47
* @author Jon Brisbin
44
48
* @author Oliver Gierke
45
49
* @author Thomas Darimont
46
50
* @author Christoph Strobl
47
51
* @author Viktor Khoroshko
48
52
*/
49
53
public 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
+ }
50
63
51
64
/*
52
65
* (non-Javadoc)
@@ -74,10 +87,14 @@ protected AbstractBeanDefinition parseInternal(Element element, ParserContext pa
74
87
BeanDefinition mongoUri = getMongoUri (element );
75
88
76
89
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 ) {
81
98
parserContext .getReaderContext ().error ("Configure either Mongo URI or details individually!" ,
82
99
parserContext .extractSource (element ));
83
100
}
@@ -115,7 +132,7 @@ protected AbstractBeanDefinition parseInternal(Element element, ParserContext pa
115
132
/**
116
133
* Registers a default {@link BeanDefinition} of a {@link Mongo} instance and returns the name under which the
117
134
* {@link Mongo} instance was registered under.
118
- *
135
+ *
119
136
* @param element must not be {@literal null}.
120
137
* @param parserContext must not be {@literal null}.
121
138
* @return
@@ -131,7 +148,7 @@ private BeanDefinition registerMongoBeanDefinition(Element element, ParserContex
131
148
132
149
/**
133
150
* Returns a {@link BeanDefinition} for a {@link UserCredentials} object.
134
- *
151
+ *
135
152
* @param element
136
153
* @return the {@link BeanDefinition} or {@literal null} if neither username nor password given.
137
154
*/
@@ -154,7 +171,7 @@ private BeanDefinition getUserCredentialsBeanDefinition(Element element, ParserC
154
171
/**
155
172
* Creates a {@link BeanDefinition} for a {@link MongoURI} or {@link MongoClientURI} depending on configured
156
173
* attributes.
157
- *
174
+ *
158
175
* @param element must not be {@literal null}.
159
176
* @return {@literal null} in case no client-/uri defined.
160
177
*/
0 commit comments