use of com.mongodb.MongoClientURI in project spring-data-mongodb by spring-projects.
the class MongoDbFactoryParser method getMongoUri.
/**
* Creates a {@link BeanDefinition} for a {@link MongoURI} or {@link MongoClientURI} depending on configured
* 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 parserContext
* @return {@literal null} in case no client-/uri defined.
*/
@Nullable
private BeanDefinition getMongoUri(Element element, ParserContext parserContext) {
boolean hasClientUri = element.hasAttribute("client-uri");
if (!hasClientUri && !element.hasAttribute("uri")) {
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 = MongoClientURI.class;
String uri = hasClientUri ? element.getAttribute("client-uri") : element.getAttribute("uri");
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(type);
builder.addConstructorArgValue(uri);
return builder.getBeanDefinition();
}
use of com.mongodb.MongoClientURI in project spring-data-mongodb by spring-projects.
the class SimpleMongoDbFactoryUnitTests method mongoUriConstructor.
// DATADOC-295
@Test
@SuppressWarnings("deprecation")
public void mongoUriConstructor() throws UnknownHostException {
MongoClientURI mongoURI = new MongoClientURI("mongodb://myUsername:myPassword@localhost/myDatabase.myCollection");
MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoURI);
assertThat(getField(mongoDbFactory, "databaseName").toString(), is("myDatabase"));
}
use of com.mongodb.MongoClientURI in project spring-data-mongodb by spring-projects.
the class SimpleMongoDbFactoryUnitTests method constructsMongoClientAccordingToMongoUri.
// DATAMONGO-1158
@Test
public void constructsMongoClientAccordingToMongoUri() throws UnknownHostException {
MongoClientURI uri = new MongoClientURI("mongodb://myUserName:myPassWord@127.0.0.1:27017/myDataBase.myCollection");
SimpleMongoDbFactory factory = new SimpleMongoDbFactory(uri);
assertThat(getField(factory, "databaseName").toString(), is("myDataBase"));
}
Aggregations