|
|
|
@ -51,9 +51,11 @@ import com.mongodb.MongoURI; |
|
|
|
* @author Viktor Khoroshko |
|
|
|
* @author Viktor Khoroshko |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class MongoDbFactoryParser extends AbstractBeanDefinitionParser { |
|
|
|
public class MongoDbFactoryParser extends AbstractBeanDefinitionParser { |
|
|
|
|
|
|
|
|
|
|
|
private static final Set<String> MONGO_URI_ALLOWED_ADDITIONAL_ATTRIBUTES; |
|
|
|
private static final Set<String> MONGO_URI_ALLOWED_ADDITIONAL_ATTRIBUTES; |
|
|
|
|
|
|
|
|
|
|
|
static { |
|
|
|
static { |
|
|
|
|
|
|
|
|
|
|
|
Set<String> mongoUriAllowedAdditionalAttributes = new HashSet<String>(); |
|
|
|
Set<String> mongoUriAllowedAdditionalAttributes = new HashSet<String>(); |
|
|
|
mongoUriAllowedAdditionalAttributes.add("id"); |
|
|
|
mongoUriAllowedAdditionalAttributes.add("id"); |
|
|
|
mongoUriAllowedAdditionalAttributes.add("write-concern"); |
|
|
|
mongoUriAllowedAdditionalAttributes.add("write-concern"); |
|
|
|
@ -84,20 +86,10 @@ public class MongoDbFactoryParser extends AbstractBeanDefinitionParser { |
|
|
|
BeanDefinitionBuilder dbFactoryBuilder = BeanDefinitionBuilder.genericBeanDefinition(SimpleMongoDbFactory.class); |
|
|
|
BeanDefinitionBuilder dbFactoryBuilder = BeanDefinitionBuilder.genericBeanDefinition(SimpleMongoDbFactory.class); |
|
|
|
setPropertyValue(dbFactoryBuilder, element, "write-concern", "writeConcern"); |
|
|
|
setPropertyValue(dbFactoryBuilder, element, "write-concern", "writeConcern"); |
|
|
|
|
|
|
|
|
|
|
|
BeanDefinition mongoUri = getMongoUri(element); |
|
|
|
BeanDefinition mongoUri = getMongoUri(element, parserContext); |
|
|
|
|
|
|
|
|
|
|
|
if (mongoUri != null) { |
|
|
|
if (mongoUri != null) { |
|
|
|
int allowedAttributesCount = 1; |
|
|
|
|
|
|
|
for (String attribute : MONGO_URI_ALLOWED_ADDITIONAL_ATTRIBUTES) { |
|
|
|
|
|
|
|
if (element.hasAttribute(attribute)) { |
|
|
|
|
|
|
|
allowedAttributesCount++; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (element.getAttributes().getLength() > allowedAttributesCount) { |
|
|
|
|
|
|
|
parserContext.getReaderContext().error("Configure either Mongo URI or details individually!", |
|
|
|
|
|
|
|
parserContext.extractSource(element)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
dbFactoryBuilder.addConstructorArgValue(mongoUri); |
|
|
|
dbFactoryBuilder.addConstructorArgValue(mongoUri); |
|
|
|
return getSourceBeanDefinition(dbFactoryBuilder, parserContext, element); |
|
|
|
return getSourceBeanDefinition(dbFactoryBuilder, parserContext, element); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -170,12 +162,15 @@ public class MongoDbFactoryParser extends AbstractBeanDefinitionParser { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Creates a {@link BeanDefinition} for a {@link MongoURI} or {@link MongoClientURI} depending on configured |
|
|
|
* Creates a {@link BeanDefinition} for a {@link MongoURI} or {@link MongoClientURI} depending on configured |
|
|
|
* attributes. |
|
|
|
* attributes. <br /> |
|
|
|
|
|
|
|
* Errors when configured element contains {@literal uri} or {@literal client-uri} along with other attributes except |
|
|
|
|
|
|
|
* {@literal write-concern} and/or {@literal id}. |
|
|
|
* |
|
|
|
* |
|
|
|
* @param element must not be {@literal null}. |
|
|
|
* @param element must not be {@literal null}. |
|
|
|
|
|
|
|
* @param parserContext |
|
|
|
* @return {@literal null} in case no client-/uri defined. |
|
|
|
* @return {@literal null} in case no client-/uri defined. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private BeanDefinition getMongoUri(Element element) { |
|
|
|
private BeanDefinition getMongoUri(Element element, ParserContext parserContext) { |
|
|
|
|
|
|
|
|
|
|
|
boolean hasClientUri = element.hasAttribute("client-uri"); |
|
|
|
boolean hasClientUri = element.hasAttribute("client-uri"); |
|
|
|
|
|
|
|
|
|
|
|
@ -183,6 +178,21 @@ public class MongoDbFactoryParser extends AbstractBeanDefinitionParser { |
|
|
|
return null; |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int allowedAttributesCount = 1; |
|
|
|
|
|
|
|
for (String attribute : MONGO_URI_ALLOWED_ADDITIONAL_ATTRIBUTES) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (element.hasAttribute(attribute)) { |
|
|
|
|
|
|
|
allowedAttributesCount++; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (element.getAttributes().getLength() > allowedAttributesCount) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
parserContext.getReaderContext().error( |
|
|
|
|
|
|
|
"Configure either " + (hasClientUri ? "Mongo Client URI" : "Mongo URI") + " or details individually!", |
|
|
|
|
|
|
|
parserContext.extractSource(element)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Class<?> type = hasClientUri ? MongoClientURI.class : MongoURI.class; |
|
|
|
Class<?> type = hasClientUri ? MongoClientURI.class : MongoURI.class; |
|
|
|
String uri = hasClientUri ? element.getAttribute("client-uri") : element.getAttribute("uri"); |
|
|
|
String uri = hasClientUri ? element.getAttribute("client-uri") : element.getAttribute("uri"); |
|
|
|
|
|
|
|
|
|
|
|
|