diff --git a/checkstyle.xml b/checkstyle.xml index 1636763..a43fe73 100644 --- a/checkstyle.xml +++ b/checkstyle.xml @@ -1,7 +1,7 @@ - + 4.0.0 org.seedstack.addons.mongodb mongodb - 4.0.0-SNAPSHOT + 5.0.0-SNAPSHOT - mongodb-core - + mongodb-core + org.mongodb - mongodb-driver-legacy + mongodb-driver-sync ${mongodb.version} provided diff --git a/core/src/main/java/org/seedstack/mongodb/MongoDbConfig.java b/core/src/main/java/org/seedstack/mongodb/MongoDbConfig.java index 1a0e7b0..175f8eb 100644 --- a/core/src/main/java/org/seedstack/mongodb/MongoDbConfig.java +++ b/core/src/main/java/org/seedstack/mongodb/MongoDbConfig.java @@ -1,5 +1,5 @@ /* - * Copyright © 2013-2021, The SeedStack authors + * Copyright © 2013-2024, The SeedStack authors * * 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 diff --git a/core/src/main/java/org/seedstack/mongodb/internal/MongoDbErrorCode.java b/core/src/main/java/org/seedstack/mongodb/internal/MongoDbErrorCode.java index cfd4aab..20a1627 100644 --- a/core/src/main/java/org/seedstack/mongodb/internal/MongoDbErrorCode.java +++ b/core/src/main/java/org/seedstack/mongodb/internal/MongoDbErrorCode.java @@ -1,5 +1,5 @@ /* - * Copyright © 2013-2021, The SeedStack authors + * Copyright © 2013-2024, The SeedStack authors * * 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 diff --git a/core/src/main/java/org/seedstack/mongodb/internal/MongoDbManager.java b/core/src/main/java/org/seedstack/mongodb/internal/MongoDbManager.java index 8ac2e8d..87b7a78 100644 --- a/core/src/main/java/org/seedstack/mongodb/internal/MongoDbManager.java +++ b/core/src/main/java/org/seedstack/mongodb/internal/MongoDbManager.java @@ -1,5 +1,5 @@ /* - * Copyright © 2013-2021, The SeedStack authors + * Copyright © 2013-2024, The SeedStack authors * * 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 @@ -7,15 +7,12 @@ */ 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; @@ -23,22 +20,31 @@ 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 mongoClients = new HashMap<>(); private final Map 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()); } } @@ -50,8 +56,10 @@ 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 serverAddresses = buildServerAddresses(clientName, clientConfig.getHosts()); if (serverAddresses.isEmpty()) { @@ -59,23 +67,15 @@ private MongoClient createMongoClient(String clientName, MongoDbConfig.ClientCon .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)); } @@ -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 { @@ -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 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 buildServerAddresses(String clientName, List addresses) { @@ -127,61 +152,8 @@ List buildServerAddresses(String clientName, List 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 options = BuilderSupplier.of(MongoClientOptions.builder()); + private BuilderSupplier options = BuilderSupplier + .of(MongoClientSettings.builder()); } } diff --git a/core/src/main/java/org/seedstack/mongodb/internal/MongoDbModule.java b/core/src/main/java/org/seedstack/mongodb/internal/MongoDbModule.java index 0e865f2..85facf3 100644 --- a/core/src/main/java/org/seedstack/mongodb/internal/MongoDbModule.java +++ b/core/src/main/java/org/seedstack/mongodb/internal/MongoDbModule.java @@ -1,5 +1,5 @@ /* - * Copyright © 2013-2021, The SeedStack authors + * Copyright © 2013-2024, The SeedStack authors * * 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 diff --git a/core/src/main/java/org/seedstack/mongodb/internal/MongoDbPlugin.java b/core/src/main/java/org/seedstack/mongodb/internal/MongoDbPlugin.java index 72bf828..8ec46ac 100644 --- a/core/src/main/java/org/seedstack/mongodb/internal/MongoDbPlugin.java +++ b/core/src/main/java/org/seedstack/mongodb/internal/MongoDbPlugin.java @@ -1,5 +1,5 @@ /* - * Copyright © 2013-2021, The SeedStack authors + * Copyright © 2013-2024, The SeedStack authors * * 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 @@ -7,19 +7,21 @@ */ 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); @@ -47,9 +49,9 @@ public InitState initialize(InitContext initContext) { MONGO_DB_MANAGER.registerClient(clientName, clientConfig, coffig); - for (Map.Entry dbEntry : clientConfig.getDatabases().entrySet()) { + for (Map.Entry 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)) { diff --git a/core/src/main/resources/org/seedstack/mongodb/MongoDbConfig.properties b/core/src/main/resources/org/seedstack/mongodb/MongoDbConfig.properties index 5d35148..8e2c8f7 100644 --- a/core/src/main/resources/org/seedstack/mongodb/MongoDbConfig.properties +++ b/core/src/main/resources/org/seedstack/mongodb/MongoDbConfig.properties @@ -1,5 +1,5 @@ # -# Copyright © 2013-2021, The SeedStack authors +# Copyright © 2013-2024, The SeedStack authors # # 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 diff --git a/core/src/main/resources/org/seedstack/mongodb/internal/MongoDbErrorCode.properties b/core/src/main/resources/org/seedstack/mongodb/internal/MongoDbErrorCode.properties index ac00c07..b7c3367 100644 --- a/core/src/main/resources/org/seedstack/mongodb/internal/MongoDbErrorCode.properties +++ b/core/src/main/resources/org/seedstack/mongodb/internal/MongoDbErrorCode.properties @@ -1,5 +1,5 @@ # -# Copyright © 2013-2021, The SeedStack authors +# Copyright © 2013-2024, The SeedStack authors # # 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 diff --git a/core/src/test/java/org/seedstack/mongodb/MongoDbIT.java b/core/src/test/java/org/seedstack/mongodb/MongoDbIT.java index d2a54e0..cc08516 100644 --- a/core/src/test/java/org/seedstack/mongodb/MongoDbIT.java +++ b/core/src/test/java/org/seedstack/mongodb/MongoDbIT.java @@ -1,5 +1,5 @@ /* - * Copyright © 2013-2021, The SeedStack authors + * Copyright © 2013-2024, The SeedStack authors * * 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 @@ -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; diff --git a/core/src/test/resources/application.yaml b/core/src/test/resources/application.yaml index 9b3367e..c59b004 100644 --- a/core/src/test/resources/application.yaml +++ b/core/src/test/resources/application.yaml @@ -1,5 +1,5 @@ # -# Copyright © 2013-2021, The SeedStack authors +# Copyright © 2013-2024, The SeedStack authors # # 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 diff --git a/morphia/pom.xml b/morphia/pom.xml index f756567..965317c 100644 --- a/morphia/pom.xml +++ b/morphia/pom.xml @@ -1,6 +1,6 @@ - + 4.0.0 org.seedstack.poms parent-internal - 4.0.1 + 4.1.0 org.seedstack.addons.mongodb mongodb - 4.0.0-SNAPSHOT + 5.0.0-SNAPSHOT pom - 3.12.0 - 4.1.1 - 1.6.1 + 3.14.0 + 5.0.1 + 2.4.13 4.4.0 true @@ -53,7 +54,9 @@ Public Domain WTFPL - + IGNORED_LICENSE @@ -65,8 +68,10 @@ Apache 2|Apache License, version 2.0 Apache 2|Apache License Version 2.0 Apache 2|Apache 2.0 + Apache 2|Apache-2.0 BSD|The New BSD License - BSD|https://svn.codehaus.org/proxytoys/trunk/LICENSE.txt + BSD|BSD-3-Clause + BSD|https://svn.codehaus.org/proxytoys/trunk/LICENSE.txt CDDL|CDDL + GPLv2 with classpath exception LGPL 3.0|GNU Lesser Public License MIT License|The MIT License @@ -74,6 +79,7 @@ MIT License|MIT license IGNORED_LICENSE|MPL 1.1 IGNORED_LICENSE|LGPL 2.1 + IGNORED_LICENSE|GNU LESSER GENERAL PUBLIC LICENSE, Version 2.1 @@ -81,6 +87,17 @@ + + + + + org.jboss.logging + jboss-logging + 3.5.3.Final + + + + org.seedstack.seed