Search in sources :

Example 1 with Authentication

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);
    }
}
Also used : MongoClient(com.mongodb.MongoClient) UnknownHostException(java.net.UnknownHostException) MongoCredential(com.mongodb.MongoCredential) MongoClientOptions(com.mongodb.MongoClientOptions) Authentication(org.cubeengine.module.bigdata.MongoDBConfiguration.Authentication) ServerAddress(com.mongodb.ServerAddress) Document(org.bson.Document)

Example 2 with Authentication

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;
}
Also used : Document(org.bson.Document) Authentication(org.cubeengine.module.bigdata.MongoDBConfiguration.Authentication) MongoCredential(com.mongodb.MongoCredential) MongoClient(com.mongodb.client.MongoClient) MongoClients(com.mongodb.client.MongoClients) MongoDatabase(com.mongodb.client.MongoDatabase) StoppingEngineEvent(org.spongepowered.api.event.lifecycle.StoppingEngineEvent) UuidRepresentation(org.bson.UuidRepresentation) Module(org.cubeengine.processor.Module) TimeUnit(java.util.concurrent.TimeUnit) Builder(com.mongodb.MongoClientSettings.Builder) ConnectionString(com.mongodb.ConnectionString) ClusterConnectionMode(com.mongodb.connection.ClusterConnectionMode) Server(org.spongepowered.api.Server) MongoClientSettings(com.mongodb.MongoClientSettings) StartedEngineEvent(org.spongepowered.api.event.lifecycle.StartedEngineEvent) ModuleConfig(org.cubeengine.libcube.service.filesystem.ModuleConfig) Listener(org.spongepowered.api.event.Listener) Singleton(com.google.inject.Singleton) MongoCredential(com.mongodb.MongoCredential) Authentication(org.cubeengine.module.bigdata.MongoDBConfiguration.Authentication) Builder(com.mongodb.MongoClientSettings.Builder) ConnectionString(com.mongodb.ConnectionString) Document(org.bson.Document)

Aggregations

MongoCredential (com.mongodb.MongoCredential)2 Document (org.bson.Document)2 Authentication (org.cubeengine.module.bigdata.MongoDBConfiguration.Authentication)2 Singleton (com.google.inject.Singleton)1 ConnectionString (com.mongodb.ConnectionString)1 MongoClient (com.mongodb.MongoClient)1 MongoClientOptions (com.mongodb.MongoClientOptions)1 MongoClientSettings (com.mongodb.MongoClientSettings)1 Builder (com.mongodb.MongoClientSettings.Builder)1 ServerAddress (com.mongodb.ServerAddress)1 MongoClient (com.mongodb.client.MongoClient)1 MongoClients (com.mongodb.client.MongoClients)1 MongoDatabase (com.mongodb.client.MongoDatabase)1 ClusterConnectionMode (com.mongodb.connection.ClusterConnectionMode)1 UnknownHostException (java.net.UnknownHostException)1 TimeUnit (java.util.concurrent.TimeUnit)1 UuidRepresentation (org.bson.UuidRepresentation)1 ModuleConfig (org.cubeengine.libcube.service.filesystem.ModuleConfig)1 Module (org.cubeengine.processor.Module)1 Server (org.spongepowered.api.Server)1