|
51 | 51 | * @author Viktor Khoroshko
|
52 | 52 | */
|
53 | 53 | public class MongoDbFactoryParser extends AbstractBeanDefinitionParser {
|
| 54 | + |
54 | 55 | private static final Set<String> MONGO_URI_ALLOWED_ADDITIONAL_ATTRIBUTES;
|
55 | 56 |
|
56 | 57 | static {
|
| 58 | + |
57 | 59 | Set<String> mongoUriAllowedAdditionalAttributes = new HashSet<String>();
|
58 | 60 | mongoUriAllowedAdditionalAttributes.add("id");
|
59 | 61 | mongoUriAllowedAdditionalAttributes.add("write-concern");
|
@@ -84,20 +86,10 @@ protected AbstractBeanDefinition parseInternal(Element element, ParserContext pa
|
84 | 86 | BeanDefinitionBuilder dbFactoryBuilder = BeanDefinitionBuilder.genericBeanDefinition(SimpleMongoDbFactory.class);
|
85 | 87 | setPropertyValue(dbFactoryBuilder, element, "write-concern", "writeConcern");
|
86 | 88 |
|
87 |
| - BeanDefinition mongoUri = getMongoUri(element); |
| 89 | + BeanDefinition mongoUri = getMongoUri(element, parserContext); |
88 | 90 |
|
89 | 91 | 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 |
| - } |
96 | 92 |
|
97 |
| - if (element.getAttributes().getLength() > allowedAttributesCount) { |
98 |
| - parserContext.getReaderContext().error("Configure either Mongo URI or details individually!", |
99 |
| - parserContext.extractSource(element)); |
100 |
| - } |
101 | 93 | dbFactoryBuilder.addConstructorArgValue(mongoUri);
|
102 | 94 | return getSourceBeanDefinition(dbFactoryBuilder, parserContext, element);
|
103 | 95 | }
|
@@ -170,19 +162,37 @@ private BeanDefinition getUserCredentialsBeanDefinition(Element element, ParserC
|
170 | 162 |
|
171 | 163 | /**
|
172 | 164 | * 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}. |
174 | 168 | *
|
175 | 169 | * @param element must not be {@literal null}.
|
| 170 | + * @param parserContext |
176 | 171 | * @return {@literal null} in case no client-/uri defined.
|
177 | 172 | */
|
178 |
| - private BeanDefinition getMongoUri(Element element) { |
| 173 | + private BeanDefinition getMongoUri(Element element, ParserContext parserContext) { |
179 | 174 |
|
180 | 175 | boolean hasClientUri = element.hasAttribute("client-uri");
|
181 | 176 |
|
182 | 177 | if (!hasClientUri && !element.hasAttribute("uri")) {
|
183 | 178 | return null;
|
184 | 179 | }
|
185 | 180 |
|
| 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 | + |
186 | 196 | Class<?> type = hasClientUri ? MongoClientURI.class : MongoURI.class;
|
187 | 197 | String uri = hasClientUri ? element.getAttribute("client-uri") : element.getAttribute("uri");
|
188 | 198 |
|
|
0 commit comments