Example usage for org.springframework.data.mongodb.core SimpleMongoDbFactory SimpleMongoDbFactory

List of usage examples for org.springframework.data.mongodb.core SimpleMongoDbFactory SimpleMongoDbFactory

Introduction

In this page you can find the example usage for org.springframework.data.mongodb.core SimpleMongoDbFactory SimpleMongoDbFactory.

Prototype

private SimpleMongoDbFactory(MongoClient mongoClient, String databaseName, boolean mongoInstanceCreated) 

Source Link

Usage

From source file:com.restfiddle.config.persistence.PersistenceConfig.java

public @Bean MongoDbFactory mongoDbFactory() throws Exception {
    UserCredentials userCredentials = new UserCredentials(env.getProperty("mongodb.username"),
            env.getProperty("mongodb.password"));
    return new SimpleMongoDbFactory(
            new MongoClient(env.getProperty("mongodb.host"), env.getProperty("mongodb.port", Integer.class)),
            env.getProperty("mongodb.name"), userCredentials);
}

From source file:io.github.autsia.crowly.config.PersistenceConfig.java

@Override
public SimpleMongoDbFactory mongoDbFactory() throws Exception {
    UserCredentials userCredentials = new UserCredentials(user, password);
    return new SimpleMongoDbFactory(mongo(), getDatabaseName(), userCredentials);
}

From source file:com.appleframework.monitor.model.Project.java

public MongoTemplate fetchMongoTemplate() {

    try {/*  w ww . j  a va  2 s  .com*/
        Mongo mongo;
        if (MONGO_MAP.containsKey(mongoUri)) {
            mongo = MONGO_MAP.get(mongoUri);

        } else {
            mongo = new Mongo(new MongoURI(mongoUri));
            MONGO_MAP.put(mongoUri, mongo);

        }

        MongoURI uri = new MongoURI(mongoUri);
        return new MongoTemplate(new SimpleMongoDbFactory(mongo, uri.getDatabase(),
                new UserCredentials(uri.getUsername(), parseChars(uri.getPassword()))));

    } catch (Exception e) {
        logger.error("mongo db error ,uri={}", mongoUri, e);
        return null;
    }

}

From source file:org.grails.datastore.mapping.mongo.MongoDatastore.java

protected void createMongoTemplate(PersistentEntity entity, Mongo mongoInstance) {
    DocumentMappingContext dc = (DocumentMappingContext) getMappingContext();
    String collectionName = entity.getDecapitalizedName();
    String databaseName = dc.getDefaultDatabaseName();
    @SuppressWarnings("unchecked")
    ClassMapping<MongoCollection> mapping = entity.getMapping();
    final MongoCollection mongoCollection = mapping.getMappedForm() != null ? mapping.getMappedForm() : null;

    if (mongoCollection != null) {
        if (mongoCollection.getCollection() != null) {
            collectionName = mongoCollection.getCollection();
        }//from  w  w w. j a  v  a  2 s  .  com
        if (mongoCollection.getDatabase() != null) {
            databaseName = mongoCollection.getDatabase();
        }
    }

    final SimpleMongoDbFactory dbf;

    String username = read(String.class, USERNAME, connectionDetails, null);
    String password = read(String.class, PASSWORD, connectionDetails, null);

    if (username != null && password != null) {
        UserCredentials uc = new UserCredentials(username, password);
        dbf = new SimpleMongoDbFactory(mongoInstance, databaseName, uc);
    } else {
        dbf = new SimpleMongoDbFactory(mongoInstance, databaseName);
    }

    final MongoTemplate mt = new MongoTemplate(dbf);

    if (mongoCollection != null) {
        final WriteConcern writeConcern = mongoCollection.getWriteConcern();
        if (writeConcern != null) {
            final String collectionNameToUse = collectionName;
            mt.executeInSession(new DbCallback<Object>() {
                public Object doInDB(DB db) throws MongoException, DataAccessException {
                    if (writeConcern != null) {
                        DBCollection collection = db.getCollection(collectionNameToUse);
                        collection.setWriteConcern(writeConcern);
                    }
                    return null;
                }
            });
        }
    }

    mongoTemplates.put(entity, mt);
    mongoCollections.put(entity, collectionName);

    initializeIndices(entity, mt);
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

/**
 * Constructor used for a template configuration with user credentials in the form of
 * {@link org.springframework.data.authentication.UserCredentials}
 * //w  w w  .j a  v a 2s  .  c o m
 * @param mongo
 * @param databaseName
 * @param userCredentials
 */
public MongoTemplate(Mongo mongo, String databaseName, UserCredentials userCredentials) {
    this(new SimpleMongoDbFactory(mongo, databaseName, userCredentials));
}