Search in sources :

Example 51 with MongoClient

use of com.mongodb.MongoClient in project spring-data-mongodb by spring-projects.

the class MongoNamespaceTests method testSecondMongoDbFactory.

@Test
public void testSecondMongoDbFactory() {
    assertTrue(ctx.containsBean("secondMongoDbFactory"));
    MongoDbFactory dbf = (MongoDbFactory) ctx.getBean("secondMongoDbFactory");
    MongoClient mongo = (MongoClient) getField(dbf, "mongoClient");
    assertEquals("127.0.0.1", mongo.getAddress().getHost());
    assertEquals(27017, mongo.getAddress().getPort());
    assertEquals("database", getField(dbf, "databaseName"));
}
Also used : MongoClient(com.mongodb.MongoClient) MongoDbFactory(org.springframework.data.mongodb.MongoDbFactory) Test(org.junit.Test)

Example 52 with MongoClient

use of com.mongodb.MongoClient in project spring-data-mongodb by spring-projects.

the class MongoNamespaceTests method testMongoSingletonWithPropertyPlaceHolders.

@Test
@SuppressWarnings("deprecation")
public void testMongoSingletonWithPropertyPlaceHolders() throws Exception {
    assertTrue(ctx.containsBean("mongoClient"));
    MongoClientFactoryBean mfb = (MongoClientFactoryBean) ctx.getBean("&mongoClient");
    String host = (String) getField(mfb, "host");
    Integer port = (Integer) getField(mfb, "port");
    assertEquals("127.0.0.1", host);
    assertEquals(new Integer(27017), port);
    MongoClient mongo = mfb.getObject();
    MongoClientOptions mongoOpts = mongo.getMongoClientOptions();
    assertEquals(8, mongoOpts.getConnectionsPerHost());
    assertEquals(1000, mongoOpts.getConnectTimeout());
    assertEquals(1500, mongoOpts.getMaxWaitTime());
    assertEquals(1500, mongoOpts.getSocketTimeout());
    assertEquals(4, mongoOpts.getThreadsAllowedToBlockForConnectionMultiplier());
// TODO: check the damned defaults
// assertEquals("w", mongoOpts.getWriteConcern().getW());
// assertEquals(0, mongoOpts.getWriteConcern().getWtimeout());
// assertEquals(true, mongoOpts.getWriteConcern().fsync());
}
Also used : MongoClient(com.mongodb.MongoClient) MongoClientFactoryBean(org.springframework.data.mongodb.core.MongoClientFactoryBean) MongoClientOptions(com.mongodb.MongoClientOptions) Test(org.junit.Test)

Example 53 with MongoClient

use of com.mongodb.MongoClient in project cas by apereo.

the class MongoDbConnectionFactory method buildMongoDbClient.

private Mongo buildMongoDbClient(final BaseMongoDbProperties mongo) {
    if (StringUtils.isNotBlank(mongo.getClientUri())) {
        LOGGER.debug("Using MongoDb client URI [{}] to connect to MongoDb instance", mongo.getClientUri());
        return buildMongoDbClient(mongo.getClientUri(), buildMongoDbClientOptions(mongo));
    }
    final String[] serverAddresses = mongo.getHost().split(",");
    if (serverAddresses == null || serverAddresses.length == 0) {
        throw new BeanCreationException("Unable to build a MongoDb client without any hosts/servers defined");
    }
    List<ServerAddress> servers = new ArrayList<>();
    if (serverAddresses.length > 1) {
        LOGGER.debug("Multiple MongoDb server addresses are defined. Ignoring port [{}], " + "assuming ports are defined as part of the address", mongo.getPort());
        servers = Arrays.stream(serverAddresses).filter(StringUtils::isNotBlank).map(ServerAddress::new).collect(Collectors.toList());
    } else {
        final int port = mongo.getPort() > 0 ? mongo.getPort() : DEFAULT_PORT;
        LOGGER.debug("Found single MongoDb server address [{}] using port [{}]" + mongo.getHost(), port);
        final ServerAddress addr = new ServerAddress(mongo.getHost(), port);
        servers.add(addr);
    }
    final MongoCredential credential = buildMongoCredential(mongo);
    return new MongoClient(servers, CollectionUtils.wrap(credential), buildMongoDbClientOptions(mongo));
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) MongoClient(com.mongodb.MongoClient) MongoCredential(com.mongodb.MongoCredential) ServerAddress(com.mongodb.ServerAddress) ArrayList(java.util.ArrayList)

Example 54 with MongoClient

use of com.mongodb.MongoClient in project cas by apereo.

the class CasMongoAuthenticationConfiguration method mongoAuthenticatorProfileService.

@ConditionalOnMissingBean(name = "mongoAuthenticatorProfileService")
@Bean
public MongoProfileService mongoAuthenticatorProfileService() {
    final MongoAuthenticationProperties mongo = casProperties.getAuthn().getMongo();
    final MongoClientURI uri = new MongoClientURI(mongo.getMongoHostUri());
    final MongoClient client = new MongoClient(uri);
    LOGGER.info("Connected to MongoDb instance @ [{}] using database [{}]", uri.getHosts(), uri.getDatabase());
    final SpringSecurityPasswordEncoder encoder = new SpringSecurityPasswordEncoder(PasswordEncoderUtils.newPasswordEncoder(mongo.getPasswordEncoder()));
    final MongoProfileService auth = new MongoProfileService(client, mongo.getAttributes());
    auth.setUsersCollection(mongo.getCollectionName());
    auth.setUsersDatabase(uri.getDatabase());
    auth.setUsernameAttribute(mongo.getUsernameAttribute());
    auth.setPasswordAttribute(mongo.getPasswordAttribute());
    auth.setPasswordEncoder(encoder);
    return auth;
}
Also used : MongoProfileService(org.pac4j.mongo.profile.service.MongoProfileService) MongoClient(com.mongodb.MongoClient) SpringSecurityPasswordEncoder(org.pac4j.core.credentials.password.SpringSecurityPasswordEncoder) MongoClientURI(com.mongodb.MongoClientURI) MongoAuthenticationProperties(org.apereo.cas.configuration.model.support.mongo.MongoAuthenticationProperties) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 55 with MongoClient

use of com.mongodb.MongoClient in project apex-malhar by apache.

the class MongoDBOutputOperator method setup.

/**
 * At setup time, init last completed windowId from maxWindowTable
 *
 * @param context
 */
@Override
public void setup(OperatorContext context) {
    operatorId = context.getId();
    try {
        mongoClient = new MongoClient(hostName);
        db = mongoClient.getDB(dataBase);
        if (userName != null && passWord != null) {
            db.authenticate(userName, passWord.toCharArray());
        }
        initLastWindowInfo();
        for (String table : tableList) {
            tableToDocumentList.put(table, new ArrayList<DBObject>());
            tableToDocument.put(table, new BasicDBObject());
        }
    } catch (UnknownHostException ex) {
        logger.debug(ex.toString());
    }
}
Also used : MongoClient(com.mongodb.MongoClient) BasicDBObject(com.mongodb.BasicDBObject) UnknownHostException(java.net.UnknownHostException) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject)

Aggregations

MongoClient (com.mongodb.MongoClient)126 Test (org.junit.Test)31 MongoClientURI (com.mongodb.MongoClientURI)29 Document (org.bson.Document)26 ServerAddress (com.mongodb.ServerAddress)21 MongoDatabase (com.mongodb.client.MongoDatabase)21 Before (org.junit.Before)20 BasicDBObject (com.mongodb.BasicDBObject)11 ArrayList (java.util.ArrayList)11 MongoCredential (com.mongodb.MongoCredential)9 MongoException (com.mongodb.MongoException)8 DB (com.mongodb.DB)7 DBCollection (com.mongodb.DBCollection)7 UnknownHostException (java.net.UnknownHostException)7 MongoTemplate (org.springframework.data.mongodb.core.MongoTemplate)7 DBObject (com.mongodb.DBObject)6 MongoClientOptions (com.mongodb.MongoClientOptions)6 IOException (java.io.IOException)5 List (java.util.List)5 MongoDbFactory (org.springframework.data.mongodb.MongoDbFactory)5