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"));
}
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());
}
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));
}
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;
}
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());
}
}
Aggregations