Skip to content

Commit

Permalink
added support for server URI strings for use with mongoDB clusters, c…
Browse files Browse the repository at this point in the history
…loses #2
  • Loading branch information
albogdano committed Feb 7, 2019
1 parent 14f62f7 commit d9425c3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -38,6 +38,10 @@ WAR file `para-x.y.z.war`. Para will look for plugins inside `lib` and pick up t

Here are all the configuration properties for this plugin (these go inside your `application.conf`):
```ini
# setting the URI will override host:port below
# URI is left blank by default
para.mongodb.uri = ""

para.mongodb.host = "localhost"
para.mongodb.port = 27017
para.mongodb.database = "MyApp"
Expand All @@ -47,6 +51,11 @@ para.mongodb.ssl_enabled = false
para.mongodb.ssl_allow_all = false
```

You have the option to set either the server URI as a string (e.g. `mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]`) or set the
host and port combination for a single server instance. The first option allows you to specify multiple server hosts.
If the URI has a non-blank value in the configuration file, it will override the host+port settings.
For detils about the server URI syntax, read the docs for [MongoClientURI](https://mongodb.github.io/mongo-java-driver/3.4/javadoc/com/mongodb/MongoClientURI.html).

Finally, set the config property:
```
para.dao = "MongoDBDAO"
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/com/erudika/para/persistence/MongoDBUtils.java
Expand Up @@ -45,6 +45,7 @@ public final class MongoDBUtils {
private static final Logger logger = LoggerFactory.getLogger(MongoDBUtils.class);
private static MongoClient mongodbClient;
private static MongoDatabase mongodb;
private static final String DBURI = Config.getConfigParam("mongodb.uri", "");
private static final String DBHOST = Config.getConfigParam("mongodb.host", "localhost");
private static final int DBPORT = Config.getConfigInt("mongodb.port", 27017);
private static final boolean SSL = Config.getConfigBoolean("mongodb.ssl_enabled", false);
Expand All @@ -63,9 +64,14 @@ public static MongoDatabase getClient() {
if (mongodb != null) {
return mongodb;
}

logger.info("MongoDB host: " + DBHOST + ":" + DBPORT + ", database: " + DBNAME);
ServerAddress s = new ServerAddress(DBHOST, DBPORT);
ServerAddress s;
if (StringUtils.isBlank(DBURI)) {
logger.info("MongoDB host: " + DBHOST + ":" + DBPORT + ", database: " + DBNAME);
s = new ServerAddress(DBHOST, DBPORT);
} else {
logger.info("MongoDB host: " + DBURI + ", database: " + DBNAME);
s = new ServerAddress(DBURI);
}
MongoClientOptions options = MongoClientOptions.builder().
sslEnabled(SSL).sslInvalidHostNameAllowed(SSL_ALLOW_ALL).build();

Expand Down

1 comment on commit d9425c3

@natrem
Copy link

@natrem natrem commented on d9425c3 Feb 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried locally (with a similar implementation) and the credentials and database are also ignored -> need to update documentation on this.
I'll check your code but I'm pretty sure it's working fine.
Beware of the logs: DBURI can contain credentials.
Something like DBURI.replaceAll("mongodb://.*@", "mongodb://<user:password>@") will work for the log.

I'll compare with code from work and update tomorrow.
Thanks for the work.

Please sign in to comment.