Search in sources :

Example 1 with MongoCommandListener

use of fr.romitou.mongosk.listeners.MongoCommandListener 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)

Aggregations

VariableString (ch.njol.skript.lang.VariableString)1 ConnectionString (com.mongodb.ConnectionString)1 MongoClientException (com.mongodb.MongoClientException)1 MongoClientSettings (com.mongodb.MongoClientSettings)1 MongoClient (com.mongodb.reactivestreams.client.MongoClient)1 MongoSKServer (fr.romitou.mongosk.elements.MongoSKServer)1 MongoCommandListener (fr.romitou.mongosk.listeners.MongoCommandListener)1