use of org.cubeengine.module.bigdata.MongoDBConfiguration.Authentication in project modules-extra by CubeEngine.
the class Bigdata method aquireClient.
private MongoClient aquireClient(String db) {
if (mongoClient != null) {
return mongoClient;
}
try {
Authentication authConfig = config.authentication;
ServerAddress address = new ServerAddress(InetAddress.getByName(this.config.host), this.config.port);
List<MongoCredential> credentialList = Collections.emptyList();
if (authConfig != null && authConfig.username != null && authConfig.password != null) {
MongoCredential credential = MongoCredential.createCredential(authConfig.username, db, authConfig.password.toCharArray());
credentialList = singletonList(credential);
}
MongoClientOptions options = MongoClientOptions.builder().connectTimeout(this.config.connectionTimeout).build();
mongoClient = new MongoClient(address, credentialList, options);
// Check if available by pinging the database...
mongoClient.getDatabase(db).runCommand(new Document("ping", 1));
return mongoClient;
} catch (UnknownHostException e) {
throw new IllegalStateException("Invalid host", e);
}
}
use of org.cubeengine.module.bigdata.MongoDBConfiguration.Authentication in project modules-extra by CubeEngine.
the class Bigdata method acquireClient.
private MongoClient acquireClient(String db) {
if (mongoClient != null) {
return mongoClient;
}
Authentication authConfig = config.authentication;
final ConnectionString connectionString = new ConnectionString("mongodb://" + this.config.host + ":" + this.config.port + "/?connectTimeoutMS=" + this.config.connectionTimeout + "&socketTimeoutMS=" + this.config.connectionTimeout);
final Builder settingsBuilder = MongoClientSettings.builder().applyConnectionString(connectionString).applyToClusterSettings(b -> {
b.mode(ClusterConnectionMode.SINGLE);
b.serverSelectionTimeout(1, TimeUnit.SECONDS);
}).uuidRepresentation(UuidRepresentation.STANDARD);
if (authConfig != null && authConfig.username != null && authConfig.password != null) {
MongoCredential credential = MongoCredential.createCredential(authConfig.username, db, authConfig.password.toCharArray());
settingsBuilder.credential(credential);
mongoClient = MongoClients.create(settingsBuilder.build());
} else {
mongoClient = MongoClients.create(settingsBuilder.build());
}
// Check if available by pinging the database...
mongoClient.getDatabase(db).runCommand(new Document("ping", 1));
return mongoClient;
}
Aggregations