Search in sources :

Example 1 with MongoSKServer

use of fr.romitou.mongosk.elements.MongoSKServer in project MongoSK by Romitou.

the class ExprMongoServer method get.

@SuppressWarnings("unchecked")
@Override
protected MongoSKServer[] get(@Nonnull final Event e) {
    String rawConnectionString = (exprRawConnectionString instanceof VariableString) ? ((VariableString) exprRawConnectionString).toUnformattedString(e) : ((Expression<String>) exprRawConnectionString).getSingle(e);
    if (rawConnectionString == null)
        return new MongoSKServer[0];
    // Validate the connection string.
    ConnectionString connectionString;
    try {
        connectionString = new ConnectionString(rawConnectionString);
    } catch (IllegalArgumentException ex) {
        LoggerHelper.severe("Your connection string is invalid: " + ex.getMessage().toLowerCase());
        return new MongoSKServer[0];
    } catch (MongoClientException ex) {
        LoggerHelper.severe("An error occurred while creating the client: " + ex.getMessage().toLowerCase());
        return new MongoSKServer[0];
    }
    // Create listeners.
    MongoCommandListener mongoCommandListener = new MongoCommandListener();
    // Build the Mongo client settings.
    MongoClientSettings.Builder settings = MongoClientSettings.builder().applyConnectionString(connectionString).applyToSocketSettings(builder -> builder.connectTimeout(CONNECT_TIMEOUT, TimeUnit.MILLISECONDS).readTimeout(READ_TIMEOUT, TimeUnit.MILLISECONDS)).applicationName(connectionString.getApplicationName() == null ? "MongoSK/" + MongoSK.getInstance().getDescription().getVersion() : connectionString.getApplicationName()).addCommandListener(mongoCommandListener);
    MongoClient mongoClient = MongoClients.create(settings.build());
    // Get the most pertinent displayed name for this client.
    String displayedName = connectionString.getApplicationName() == null ? connectionString.getHosts().get(0) : connectionString.getApplicationName();
    // Create the MongoSKServer object.
    MongoSKServer mongoSKServer = new MongoSKServer(displayedName, mongoClient);
    // Add this server to the listeners (we must do this because the client is only created after the builder).
    mongoCommandListener.setMongoSKServer(mongoSKServer);
    // Return the MongoSKServer linked with the listeners.
    return new MongoSKServer[] { mongoSKServer };
}
Also used : MongoClient(com.mongodb.reactivestreams.client.MongoClient) MongoClientException(com.mongodb.MongoClientException) MongoSKServer(fr.romitou.mongosk.elements.MongoSKServer) VariableString(ch.njol.skript.lang.VariableString) MongoCommandListener(fr.romitou.mongosk.listeners.MongoCommandListener) ConnectionString(com.mongodb.ConnectionString) VariableString(ch.njol.skript.lang.VariableString) MongoClientSettings(com.mongodb.MongoClientSettings) ConnectionString(com.mongodb.ConnectionString)

Example 2 with MongoSKServer

use of fr.romitou.mongosk.elements.MongoSKServer in project MongoSK by Romitou.

the class ExprMongoDatabase method get.

@Override
protected MongoSKDatabase[] get(@Nonnull final Event e) {
    String databaseName = exprDatabaseName.getSingle(e);
    MongoSKServer mongoSKServer = exprMongoSKServer.getSingle(e);
    if (databaseName == null || mongoSKServer == null)
        return new MongoSKDatabase[0];
    MongoDatabase mongoDatabase = mongoSKServer.getMongoClient().getDatabase(databaseName);
    if (mongoDatabase == null)
        return new MongoSKDatabase[0];
    return new MongoSKDatabase[] { new MongoSKDatabase(mongoDatabase) };
}
Also used : MongoSKServer(fr.romitou.mongosk.elements.MongoSKServer) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) MongoSKDatabase(fr.romitou.mongosk.elements.MongoSKDatabase)

Example 3 with MongoSKServer

use of fr.romitou.mongosk.elements.MongoSKServer in project MongoSK by Romitou.

the class ExprAllMongoDatabases method get.

@Override
protected MongoSKDatabase[] get(@Nonnull final Event e) {
    MongoSKServer mongoSKServer = exprMongoSKServer.getSingle(e);
    if (mongoSKServer == null)
        return new MongoSKDatabase[0];
    SubscriberHelpers.ObservableSubscriber<Document> observableSubscriber = new SubscriberHelpers.OperationSubscriber<>();
    mongoSKServer.getMongoClient().listDatabases().subscribe(observableSubscriber);
    return observableSubscriber.get().stream().map(doc -> doc.getString("name")).map(name -> mongoSKServer.getMongoClient().getDatabase(name)).map(MongoSKDatabase::new).toArray(MongoSKDatabase[]::new);
}
Also used : SubscriberHelpers(fr.romitou.mongosk.SubscriberHelpers) Document(org.bson.Document) Name(ch.njol.skript.doc.Name) MongoSKServer(fr.romitou.mongosk.elements.MongoSKServer) Event(org.bukkit.event.Event) SkriptParser(ch.njol.skript.lang.SkriptParser) MongoSKDatabase(fr.romitou.mongosk.elements.MongoSKDatabase) ExpressionType(ch.njol.skript.lang.ExpressionType) Since(ch.njol.skript.doc.Since) Expression(ch.njol.skript.lang.Expression) Examples(ch.njol.skript.doc.Examples) Skript(ch.njol.skript.Skript) Description(ch.njol.skript.doc.Description) Kleenean(ch.njol.util.Kleenean) SimpleExpression(ch.njol.skript.lang.util.SimpleExpression) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) MongoSKServer(fr.romitou.mongosk.elements.MongoSKServer) SubscriberHelpers(fr.romitou.mongosk.SubscriberHelpers) Document(org.bson.Document)

Aggregations

MongoSKServer (fr.romitou.mongosk.elements.MongoSKServer)3 MongoSKDatabase (fr.romitou.mongosk.elements.MongoSKDatabase)2 Skript (ch.njol.skript.Skript)1 Description (ch.njol.skript.doc.Description)1 Examples (ch.njol.skript.doc.Examples)1 Name (ch.njol.skript.doc.Name)1 Since (ch.njol.skript.doc.Since)1 Expression (ch.njol.skript.lang.Expression)1 ExpressionType (ch.njol.skript.lang.ExpressionType)1 SkriptParser (ch.njol.skript.lang.SkriptParser)1 VariableString (ch.njol.skript.lang.VariableString)1 SimpleExpression (ch.njol.skript.lang.util.SimpleExpression)1 Kleenean (ch.njol.util.Kleenean)1 ConnectionString (com.mongodb.ConnectionString)1 MongoClientException (com.mongodb.MongoClientException)1 MongoClientSettings (com.mongodb.MongoClientSettings)1 MongoClient (com.mongodb.reactivestreams.client.MongoClient)1 MongoDatabase (com.mongodb.reactivestreams.client.MongoDatabase)1 SubscriberHelpers (fr.romitou.mongosk.SubscriberHelpers)1 MongoCommandListener (fr.romitou.mongosk.listeners.MongoCommandListener)1