Skip to content

Upgrade MongoDB Driver to 5.0.1 and Morphia to 2.4 #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion checkstyle.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<!--

Copyright © 2013-2021, The SeedStack authors <http://seedstack.org>
Copyright © 2013-2024, The SeedStack authors <http://seedstack.org>

This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down
15 changes: 8 additions & 7 deletions core/pom.xml
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
<!--

Copyright © 2013-2021, The SeedStack authors <http://seedstack.org>
Copyright © 2013-2024, The SeedStack authors <http://seedstack.org>

This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.seedstack.addons.mongodb</groupId>
<artifactId>mongodb</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>5.0.0-SNAPSHOT</version>
</parent>

<artifactId>mongodb-core</artifactId>
<artifactId>mongodb-core</artifactId>

<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-legacy</artifactId>
<artifactId>mongodb-driver-sync</artifactId>
<version>${mongodb.version}</version>
<scope>provided</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2013-2021, The SeedStack authors <http://seedstack.org>
* Copyright © 2013-2024, The SeedStack authors <http://seedstack.org>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2013-2021, The SeedStack authors <http://seedstack.org>
* Copyright © 2013-2024, The SeedStack authors <http://seedstack.org>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down
154 changes: 63 additions & 91 deletions core/src/main/java/org/seedstack/mongodb/internal/MongoDbManager.java
Original file line number Diff line number Diff line change
@@ -1,44 +1,50 @@
/*
* Copyright © 2013-2021, The SeedStack authors <http://seedstack.org>
* Copyright © 2013-2024, The SeedStack authors <http://seedstack.org>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package org.seedstack.mongodb.internal;

import com.google.common.base.Preconditions;
import com.google.inject.Module;
import com.mongodb.AuthenticationMechanism;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoClientURI;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoDatabase;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.seedstack.coffig.BuilderSupplier;
import org.seedstack.coffig.Coffig;
import org.seedstack.mongodb.MongoDbConfig;
import org.seedstack.seed.SeedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.common.base.Preconditions;
import com.google.inject.Module;
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;

class MongoDbManager {
private static final Logger LOGGER = LoggerFactory.getLogger(MongoDbManager.class);
private final Map<String, MongoClient> mongoClients = new HashMap<>();
private final Map<String, MongoDatabase> mongoDatabases = new HashMap<>();

protected MongoClient doCreateClient(String clientName, MongoDbConfig.ClientConfig clientConfig, Coffig coffig) {
protected MongoClient doCreateClient(
String clientName, MongoDbConfig.ClientConfig clientConfig, Coffig coffig) {
AllOptions allOptions = coffig.get(AllOptions.class, String.format("mongoDb.clients.%s", clientName));
if (clientConfig.isConfiguredByUri()) {
return new MongoClient(new MongoClientURI(clientConfig.getUri(), allOptions.options.get()));
return MongoClients.create(
allOptions.options
.get()
.applyConnectionString(new ConnectionString(clientConfig.getUri()))
.build());
} else {
return createMongoClient(clientName, clientConfig, allOptions.options.get().build());
return createMongoClient(clientName, clientConfig, allOptions.options.get());
}
}

Expand All @@ -50,32 +56,26 @@ protected void doClose(MongoClient client) {
client.close();
}

private MongoClient createMongoClient(String clientName, MongoDbConfig.ClientConfig clientConfig,
MongoClientOptions mongoClientOptions) {
private MongoClient createMongoClient(
String clientName,
MongoDbConfig.ClientConfig clientConfig,
MongoClientSettings.Builder mongoClientSettingsBuilder) {
List<ServerAddress> serverAddresses = buildServerAddresses(clientName, clientConfig.getHosts());

if (serverAddresses.isEmpty()) {
throw SeedException.createNew(MongoDbErrorCode.MISSING_HOSTS_CONFIGURATION)
.put("clientName", clientName);
}

MongoCredential mongoCredential = buildMongoCredential(clientName, clientConfig.getCredentials());
if (mongoCredential == null) {
if (serverAddresses.size() == 1) {
return new MongoClient(serverAddresses.get(0), mongoClientOptions);
} else {
return new MongoClient(serverAddresses, mongoClientOptions);
}
} else {
if (serverAddresses.size() == 1) {
return new MongoClient(serverAddresses.get(0), mongoCredential, mongoClientOptions);
} else {
return new MongoClient(serverAddresses, mongoCredential, mongoClientOptions);
}
}
ConnectionString connectionString = buildConnectionString(clientName, serverAddresses,
clientConfig.getCredentials());

return MongoClients.create(mongoClientSettingsBuilder.applyConnectionString(connectionString).build());

}

public void registerClient(String clientName, MongoDbConfig.ClientConfig clientConfig, Coffig coffig) {
public void registerClient(
String clientName, MongoDbConfig.ClientConfig clientConfig, Coffig coffig) {
LOGGER.info("Creating MongoDB client {}", clientName);
mongoClients.put(clientName, doCreateClient(clientName, clientConfig, coffig));
}
Expand All @@ -93,7 +93,10 @@ public void shutdown() {
try {
doClose(mongoClientEntry.getValue());
} catch (Exception e) {
LOGGER.error(String.format("Unable to properly close MongoDB client %s", mongoClientEntry.getKey()), e);
LOGGER.error(
String.format(
"Unable to properly close MongoDB client %s", mongoClientEntry.getKey()),
e);
}
}
} finally {
Expand All @@ -103,7 +106,29 @@ public void shutdown() {
}

public Module getModule() {
return new MongoDbModule<>(com.mongodb.MongoClient.class, MongoDatabase.class, mongoClients, mongoDatabases);
return new MongoDbModule<>(
com.mongodb.client.MongoClient.class, MongoDatabase.class, mongoClients, mongoDatabases);
}

ConnectionString buildConnectionString(String clientName, List<ServerAddress> serverAddresses,
String mongoCredential) {

StringBuilder builder = new StringBuilder();
builder.append("mongodb://");

if (mongoCredential != null) {
builder.append(mongoCredential);
builder.append("@");
}
if (serverAddresses.size() == 1) {
builder.append(serverAddresses.get(0));
} else {
builder.append(serverAddresses.stream()
.map(x -> x.toString())
.collect(Collectors.joining(",")));
}
LOGGER.info("Connection string", builder);
return new ConnectionString(builder.toString());
}

List<ServerAddress> buildServerAddresses(String clientName, List<String> addresses) {
Expand All @@ -127,61 +152,8 @@ List<ServerAddress> buildServerAddresses(String clientName, List<String> address
return serverAddresses;
}

MongoCredential buildMongoCredential(String clientName, String credential) {
if (credential == null || credential.isEmpty()) {
return null;
} else {
String[] elements = credential.split(":", 3);
if (elements.length == 3) {
String[] sourceElements = elements[0].split("/", 2);
if (sourceElements.length == 2) {
return buildMongoCredential(clientName, elements[1], elements[2], sourceElements[1], sourceElements[0]);
} else if (sourceElements.length == 1) {
return buildMongoCredential(clientName, elements[1], elements[2], sourceElements[0], null);
} else {
throw SeedException.createNew(MongoDbErrorCode.INVALID_CREDENTIAL_SYNTAX)
.put("credential", credential)
.put("clientName", clientName);
}
} else {
throw SeedException.createNew(MongoDbErrorCode.INVALID_CREDENTIAL_SYNTAX)
.put("credential", credential)
.put("clientName", clientName);
}
}
}

MongoCredential buildMongoCredential(String clientName, String user, String password, String source, String mechanism) {
if (mechanism != null) {
AuthenticationMechanism authenticationMechanism = AuthenticationMechanism.fromMechanismName(mechanism);
switch (authenticationMechanism) {
case PLAIN:
return MongoCredential.createPlainCredential(user, source, password.toCharArray());
case SCRAM_SHA_1:
return MongoCredential.createScramSha1Credential(user, source, password.toCharArray());
case SCRAM_SHA_256:
return MongoCredential.createScramSha256Credential(user, source, password.toCharArray());
case MONGODB_AWS:
return MongoCredential.createAwsCredential(user, password.toCharArray());
case MONGODB_X509:
return MongoCredential.createMongoX509Credential(user);
case GSSAPI:
return MongoCredential.createGSSAPICredential(user);
default:
throw SeedException.createNew(MongoDbErrorCode.UNSUPPORTED_AUTHENTICATION_MECHANISM)
.put("clientName", clientName)
.put("mechanism", authenticationMechanism.getMechanismName());
}
} else {
return MongoCredential.createCredential(
user,
source,
password.toCharArray()
);
}
}

private static class AllOptions {
private BuilderSupplier<MongoClientOptions.Builder> options = BuilderSupplier.of(MongoClientOptions.builder());
private BuilderSupplier<MongoClientSettings.Builder> options = BuilderSupplier
.of(MongoClientSettings.builder());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2013-2021, The SeedStack authors <http://seedstack.org>
* Copyright © 2013-2024, The SeedStack authors <http://seedstack.org>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
/*
* Copyright © 2013-2021, The SeedStack authors <http://seedstack.org>
* Copyright © 2013-2024, The SeedStack authors <http://seedstack.org>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package org.seedstack.mongodb.internal;

import io.nuun.kernel.api.plugin.InitState;
import io.nuun.kernel.api.plugin.context.InitContext;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import org.seedstack.coffig.Coffig;
import org.seedstack.mongodb.MongoDbConfig;
import org.seedstack.mongodb.MongoDbConfig.ClientConfig;
import org.seedstack.seed.SeedException;
import org.seedstack.seed.core.internal.AbstractSeedPlugin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import io.nuun.kernel.api.plugin.InitState;
import io.nuun.kernel.api.plugin.context.InitContext;

public class MongoDbPlugin extends AbstractSeedPlugin {
private static final Logger LOGGER = LoggerFactory.getLogger(MongoDbPlugin.class);
Expand Down Expand Up @@ -47,9 +49,9 @@ public InitState initialize(InitContext initContext) {

MONGO_DB_MANAGER.registerClient(clientName, clientConfig, coffig);

for (Map.Entry<String, MongoDbConfig.ClientConfig.DatabaseConfig> dbEntry : clientConfig.getDatabases().entrySet()) {
for (Map.Entry<String, ClientConfig.DatabaseConfig> dbEntry : clientConfig.getDatabases().entrySet()) {
String dbName = dbEntry.getKey();
MongoDbConfig.ClientConfig.DatabaseConfig dbConfig = dbEntry.getValue();
ClientConfig.DatabaseConfig dbConfig = dbEntry.getValue();
String alias = Optional.ofNullable(dbConfig.getAlias()).orElse(dbName);

if (allDbNames.contains(alias)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright © 2013-2021, The SeedStack authors <http://seedstack.org>
# Copyright © 2013-2024, The SeedStack authors <http://seedstack.org>
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright © 2013-2021, The SeedStack authors <http://seedstack.org>
# Copyright © 2013-2024, The SeedStack authors <http://seedstack.org>
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down
4 changes: 2 additions & 2 deletions core/src/test/java/org/seedstack/mongodb/MongoDbIT.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2013-2021, The SeedStack authors <http://seedstack.org>
* Copyright © 2013-2024, The SeedStack authors <http://seedstack.org>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand All @@ -9,7 +9,7 @@

import static org.assertj.core.api.Assertions.assertThat;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoDatabase;
import javax.inject.Inject;
import javax.inject.Named;
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright © 2013-2021, The SeedStack authors <http://seedstack.org>
# Copyright © 2013-2024, The SeedStack authors <http://seedstack.org>
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down
Loading