Add support for MongoDB URI (#1330)

Implements #1311
This commit is contained in:
Clément P 2020-05-01 21:28:52 +02:00 committed by GitHub
parent faba022a7b
commit 6a28ba0b1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 13 deletions

View File

@ -103,6 +103,11 @@ public class Settings implements ConfigObject {
@ConfigEntry(path = "general.database.prefix-character", since = "1.13.0") @ConfigEntry(path = "general.database.prefix-character", since = "1.13.0")
private String databasePrefix = ""; private String databasePrefix = "";
@ConfigComment("MongoDB client connection URI to override default connection options.")
@ConfigComment("See: https://docs.mongodb.com/manual/reference/connection-string/")
@ConfigEntry(path = "general.database.mongodb-connection-uri", since = "1.14.0")
private String mongodbConnectionUri = "";
@ConfigComment("Allow FTB Autonomous Activator to work (will allow a pseudo player [CoFH] to place and break blocks and hang items)") @ConfigComment("Allow FTB Autonomous Activator to work (will allow a pseudo player [CoFH] to place and break blocks and hang items)")
@ConfigComment("Add other fake player names here if required") @ConfigComment("Add other fake player names here if required")
@ConfigEntry(path = "general.fakeplayers", experimental = true) @ConfigEntry(path = "general.fakeplayers", experimental = true)
@ -661,4 +666,23 @@ public class Settings implements ConfigObject {
public void setKeepPreviousIslandOnReset(boolean keepPreviousIslandOnReset) { public void setKeepPreviousIslandOnReset(boolean keepPreviousIslandOnReset) {
this.keepPreviousIslandOnReset = keepPreviousIslandOnReset; this.keepPreviousIslandOnReset = keepPreviousIslandOnReset;
} }
/**
* Returns a MongoDB client connection URI to override default connection options.
*
* @return mongodb client connection.
* @see <a href="https://docs.mongodb.com/manual/reference/connection-string/">MongoDB Documentation</a>
* @since 1.14.0
*/
public String getMongodbConnectionUri() {
return mongodbConnectionUri;
}
/**
* Set the MongoDB client connection URI.
* @param mongodbConnectionUri connection URI.
*/
public void setMongodbConnectionUri(String mongodbConnectionUri) {
this.mongodbConnectionUri = mongodbConnectionUri;
}
} }

View File

@ -32,7 +32,7 @@ public class MongoDBDatabase implements DatabaseSetup {
plugin.getSettings().getDatabaseUsername(), plugin.getSettings().getDatabaseUsername(),
plugin.getSettings().getDatabasePassword(), plugin.getSettings().getDatabasePassword(),
plugin.getSettings().isUseSSL() plugin.getSettings().isUseSSL()
)); ), plugin.getSettings().getMongodbConnectionUri());
} }
return new MongoDBDatabaseHandler<>(plugin, type, connector); return new MongoDBDatabaseHandler<>(plugin, type, connector);
} }

View File

@ -3,13 +3,10 @@ package world.bentobox.bentobox.database.mongodb;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import com.mongodb.*;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNull;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoDatabase;
import world.bentobox.bentobox.database.DatabaseConnectionSettingsImpl; import world.bentobox.bentobox.database.DatabaseConnectionSettingsImpl;
@ -18,15 +15,17 @@ import world.bentobox.bentobox.database.DatabaseConnector;
public class MongoDBDatabaseConnector implements DatabaseConnector { public class MongoDBDatabaseConnector implements DatabaseConnector {
private MongoClient client; private MongoClient client;
private DatabaseConnectionSettingsImpl dbSettings; private final DatabaseConnectionSettingsImpl dbSettings;
private Set<Class<?>> types = new HashSet<>(); private final String mongoDbConnectionURI;
private final Set<Class<?>> types = new HashSet<>();
/** /**
* Class for MySQL database connections using the settings provided * Class for MySQL database connections using the settings provided
* @param dbSettings - database settings * @param dbSettings - database settings
*/ */
MongoDBDatabaseConnector(DatabaseConnectionSettingsImpl dbSettings) { MongoDBDatabaseConnector(DatabaseConnectionSettingsImpl dbSettings, String mongoDbConnectionURI) {
this.dbSettings = dbSettings; this.dbSettings = dbSettings;
this.mongoDbConnectionURI = mongoDbConnectionURI;
} }
@Override @Override
@ -34,11 +33,16 @@ public class MongoDBDatabaseConnector implements DatabaseConnector {
types.add(type); types.add(type);
// Only get one client // Only get one client
if (client == null) { if (client == null) {
MongoCredential credential = MongoCredential.createCredential(dbSettings.getUsername(), if(mongoDbConnectionURI == null || mongoDbConnectionURI.isEmpty()){
dbSettings.getDatabaseName(), MongoCredential credential = MongoCredential.createCredential(dbSettings.getUsername(),
dbSettings.getPassword().toCharArray()); dbSettings.getDatabaseName(),
MongoClientOptions options = MongoClientOptions.builder().sslEnabled(dbSettings.isUseSSL()).build(); dbSettings.getPassword().toCharArray());
client = new MongoClient(new ServerAddress(dbSettings.getHost(), dbSettings.getPort()), credential,options); MongoClientOptions options = MongoClientOptions.builder().sslEnabled(dbSettings.isUseSSL()).build();
client = new MongoClient(new ServerAddress(dbSettings.getHost(), dbSettings.getPort()), credential,options);
}else {
client = new MongoClient(new MongoClientURI(this.mongoDbConnectionURI));
}
} }
return client.getDatabase(dbSettings.getDatabaseName()); return client.getDatabase(dbSettings.getDatabaseName());
} }

View File

@ -54,6 +54,9 @@ general:
name: bentobox name: bentobox
username: username username: username
password: password password: password
# MongoDB client connection URI to override default connection options.
# See: https://docs.mongodb.com/manual/reference/connection-string/
mongodb-connection-uri: ''
# How often the data will be saved to file in mins. Default is 5 minutes. # How often the data will be saved to file in mins. Default is 5 minutes.
# This helps prevent issues if the server crashes. # This helps prevent issues if the server crashes.
# Data is also saved at important points in the game. # Data is also saved at important points in the game.