Search in sources :

Example 71 with MongoClientURI

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();
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) MongoClientURI(com.mongodb.MongoClientURI) Nullable(org.springframework.lang.Nullable)

Example 72 with MongoClientURI

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"));
}
Also used : MongoDbFactory(org.springframework.data.mongodb.MongoDbFactory) MongoClientURI(com.mongodb.MongoClientURI) Test(org.junit.Test)

Example 73 with MongoClientURI

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"));
}
Also used : MongoClientURI(com.mongodb.MongoClientURI) Test(org.junit.Test)

Aggregations

MongoClientURI (com.mongodb.MongoClientURI)73 MongoClient (com.mongodb.MongoClient)29 DBCollection (com.mongodb.DBCollection)12 Test (org.junit.Test)11 BasicDBObject (com.mongodb.BasicDBObject)10 MongoClientURIBuilder (com.mongodb.hadoop.util.MongoClientURIBuilder)8 List (java.util.List)8 Configuration (org.apache.hadoop.conf.Configuration)8 DBObject (com.mongodb.DBObject)7 ArrayList (java.util.ArrayList)7 MongoDatabase (com.mongodb.client.MongoDatabase)6 InputSplit (org.apache.hadoop.mapreduce.InputSplit)6 Document (org.bson.Document)6 DB (com.mongodb.DB)5 MongoInputSplit (com.mongodb.hadoop.input.MongoInputSplit)5 IOException (java.io.IOException)5 MongoConnection (org.apache.jackrabbit.oak.plugins.document.util.MongoConnection)5 OptionParser (joptsimple.OptionParser)4 OptionSet (joptsimple.OptionSet)4 DocumentNodeStore (org.apache.jackrabbit.oak.plugins.document.DocumentNodeStore)4