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