diff --git a/extensions/mongodb-client/deployment/src/main/java/io/quarkus/mongodb/deployment/MongoClientProcessor.java b/extensions/mongodb-client/deployment/src/main/java/io/quarkus/mongodb/deployment/MongoClientProcessor.java index 5eed0dcd22a69..68434bbd8275b 100644 --- a/extensions/mongodb-client/deployment/src/main/java/io/quarkus/mongodb/deployment/MongoClientProcessor.java +++ b/extensions/mongodb-client/deployment/src/main/java/io/quarkus/mongodb/deployment/MongoClientProcessor.java @@ -249,7 +249,7 @@ private SyntheticBeanBuildItem createBlockingSyntheticBean(MongoClientRecorder r .supplier(recorder.mongoClientSupplier(clientName, mongodbConfig)) .setRuntimeInit(); - return applyCommonBeanConfig(makeUnremovable, clientName, addMongoClientQualifier, configurator); + return applyCommonBeanConfig(makeUnremovable, clientName, addMongoClientQualifier, configurator, false); } private SyntheticBeanBuildItem createReactiveSyntheticBean(MongoClientRecorder recorder, MongodbConfig mongodbConfig, @@ -263,11 +263,11 @@ private SyntheticBeanBuildItem createReactiveSyntheticBean(MongoClientRecorder r .supplier(recorder.reactiveMongoClientSupplier(clientName, mongodbConfig)) .setRuntimeInit(); - return applyCommonBeanConfig(makeUnremovable, clientName, addMongoClientQualifier, configurator); + return applyCommonBeanConfig(makeUnremovable, clientName, addMongoClientQualifier, configurator, true); } private SyntheticBeanBuildItem applyCommonBeanConfig(boolean makeUnremovable, String clientName, - boolean addMongoClientQualifier, SyntheticBeanBuildItem.ExtendedBeanConfigurator configurator) { + boolean addMongoClientQualifier, SyntheticBeanBuildItem.ExtendedBeanConfigurator configurator, boolean isReactive) { if (makeUnremovable) { configurator.unremovable(); } @@ -275,7 +275,7 @@ private SyntheticBeanBuildItem applyCommonBeanConfig(boolean makeUnremovable, St if (MongoClientBeanUtil.isDefault(clientName)) { configurator.addQualifier(Default.class); } else { - String namedQualifier = MongoClientBeanUtil.namedQualifier(clientName, false); + String namedQualifier = MongoClientBeanUtil.namedQualifier(clientName, isReactive); configurator.addQualifier().annotation(DotNames.NAMED).addValue("value", namedQualifier).done(); if (addMongoClientQualifier) { configurator.addQualifier().annotation(MONGO_CLIENT_ANNOTATION).addValue("value", clientName).done(); diff --git a/extensions/mongodb-client/deployment/src/test/java/io/quarkus/mongodb/MongoNamedClientClientBuildItemConsumerTest.java b/extensions/mongodb-client/deployment/src/test/java/io/quarkus/mongodb/MongoNamedClientClientBuildItemConsumerTest.java new file mode 100644 index 0000000000000..fabf5e9317fa1 --- /dev/null +++ b/extensions/mongodb-client/deployment/src/test/java/io/quarkus/mongodb/MongoNamedClientClientBuildItemConsumerTest.java @@ -0,0 +1,65 @@ +package io.quarkus.mongodb; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Collections; +import java.util.List; +import java.util.function.Consumer; + +import javax.enterprise.inject.Default; +import javax.enterprise.inject.literal.NamedLiteral; + +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import com.mongodb.client.MongoClient; + +import io.quarkus.arc.Arc; +import io.quarkus.builder.BuildChainBuilder; +import io.quarkus.deployment.builditem.ApplicationArchivesBuildItem; +import io.quarkus.deployment.builditem.FeatureBuildItem; +import io.quarkus.mongodb.deployment.MongoClientBuildItem; +import io.quarkus.mongodb.deployment.MongoClientNameBuildItem; +import io.quarkus.mongodb.reactive.ReactiveMongoClient; +import io.quarkus.test.QuarkusUnitTest; + +public class MongoNamedClientClientBuildItemConsumerTest { + + @RegisterExtension + static QuarkusUnitTest runner = new QuarkusUnitTest() + .setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class).addClasses(MongoTestBase.class)) + .withConfigurationResource("named-mongoclient.properties") + .addBuildChainCustomizer(buildCustomizer()); + + @Test + public void testContainerHasBeans() { + assertThat(Arc.container().instance(MongoClient.class, Default.Literal.INSTANCE).get()).isNotNull(); + assertThat(Arc.container().instance(MongoClient.class, NamedLiteral.of("second")).get()).isNotNull(); + assertThat(Arc.container().instance(ReactiveMongoClient.class, Default.Literal.INSTANCE).get()).isNotNull(); + assertThat(Arc.container().instance(ReactiveMongoClient.class, NamedLiteral.of("secondreactive")).get()).isNotNull(); + } + + protected static Consumer buildCustomizer() { + return new Consumer() { + // This represents the extension. + @Override + public void accept(BuildChainBuilder builder) { + builder.addBuildStep(context -> { + ApplicationArchivesBuildItem archive = context.consume(ApplicationArchivesBuildItem.class); + context.produce(Collections.singletonList(new MongoClientNameBuildItem("second"))); + }).consumes(ApplicationArchivesBuildItem.class) + .produces(MongoClientNameBuildItem.class) + .build(); + + builder.addBuildStep(context -> { + List mongoClientBuildItems = context.consumeMulti(MongoClientBuildItem.class); + context.produce(new FeatureBuildItem("dummy")); + }).consumes(MongoClientBuildItem.class) + .produces(FeatureBuildItem.class) + .build(); + } + }; + } +} diff --git a/extensions/mongodb-client/deployment/src/test/resources/named-mongoclient.properties b/extensions/mongodb-client/deployment/src/test/resources/named-mongoclient.properties new file mode 100644 index 0000000000000..6ff9b32dc51ce --- /dev/null +++ b/extensions/mongodb-client/deployment/src/test/resources/named-mongoclient.properties @@ -0,0 +1,2 @@ +quarkus.mongodb.connection-string=mongodb://localhost:27018 +quarkus.mongodb.second.connection-string=mongodb://localhost:27018 diff --git a/extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/runtime/MongoClientBeanUtil.java b/extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/runtime/MongoClientBeanUtil.java index a410976f65927..c65b6e2d4b34e 100644 --- a/extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/runtime/MongoClientBeanUtil.java +++ b/extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/runtime/MongoClientBeanUtil.java @@ -1,5 +1,9 @@ package io.quarkus.mongodb.runtime; +import javax.enterprise.inject.Default; +import javax.enterprise.inject.literal.NamedLiteral; +import javax.enterprise.util.AnnotationLiteral; + public final class MongoClientBeanUtil { public static final String DEFAULT_MONGOCLIENT_NAME = ""; @@ -18,4 +22,12 @@ public static String namedQualifier(String clientName, boolean isReactive) { } return isReactive ? clientName + REACTIVE_CLIENT_NAME_SUFFIX : clientName; } + + @SuppressWarnings("rawtypes") + public static AnnotationLiteral clientLiteral(String clientName, boolean isReactive) { + if (MongoClientBeanUtil.isDefault(clientName)) { + return Default.Literal.INSTANCE; + } + return NamedLiteral.of(namedQualifier(clientName, isReactive)); + } } diff --git a/extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/runtime/MongoClientRecorder.java b/extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/runtime/MongoClientRecorder.java index d478cffd657c5..c37986c231b99 100644 --- a/extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/runtime/MongoClientRecorder.java +++ b/extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/runtime/MongoClientRecorder.java @@ -37,7 +37,7 @@ public MongoClientSupport get() { } public Supplier mongoClientSupplier(String clientName, - @SuppressWarnings("usused") MongodbConfig mongodbConfig) { + @SuppressWarnings("unused") MongodbConfig mongodbConfig) { MongoClient mongoClient = Arc.container().instance(MongoClients.class).get().createMongoClient(clientName); return new Supplier() { @Override @@ -48,7 +48,7 @@ public MongoClient get() { } public Supplier reactiveMongoClientSupplier(String clientName, - @SuppressWarnings("usused") MongodbConfig mongodbConfig) { + @SuppressWarnings("unused") MongodbConfig mongodbConfig) { ReactiveMongoClient reactiveMongoClient = Arc.container().instance(MongoClients.class).get() .createReactiveMongoClient(clientName); return new Supplier() { diff --git a/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/reactive/runtime/ReactiveMongoOperations.java b/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/reactive/runtime/ReactiveMongoOperations.java index 38671bff4399f..bd4c3ae51c7b8 100644 --- a/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/reactive/runtime/ReactiveMongoOperations.java +++ b/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/reactive/runtime/ReactiveMongoOperations.java @@ -302,7 +302,7 @@ private ReactiveMongoCollection mongoCollection(Object entity) { } private ReactiveMongoDatabase mongoDatabase(MongoEntity entity) { - ReactiveMongoClient mongoClient = clientFromArc(entity, ReactiveMongoClient.class); + ReactiveMongoClient mongoClient = clientFromArc(entity, ReactiveMongoClient.class, true); if (entity != null && !entity.database().isEmpty()) { return mongoClient.getDatabase(entity.database()); } diff --git a/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/runtime/BeanUtils.java b/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/runtime/BeanUtils.java index 6f7c5c6ed2eb0..d0287d7b66007 100644 --- a/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/runtime/BeanUtils.java +++ b/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/runtime/BeanUtils.java @@ -2,9 +2,6 @@ import java.lang.annotation.Annotation; -import javax.enterprise.inject.Default; -import javax.enterprise.inject.literal.NamedLiteral; -import javax.enterprise.util.AnnotationLiteral; import javax.inject.Named; import io.quarkus.arc.Arc; @@ -28,8 +25,9 @@ public static String beanName(MongoEntity entity) { return MongoClientBeanUtil.DEFAULT_MONGOCLIENT_NAME; } - public static T clientFromArc(MongoEntity entity, Class clientClass) { - T mongoClient = Arc.container().instance(clientClass, clientLiteral(beanName(entity))).get(); + public static T clientFromArc(MongoEntity entity, Class clientClass, boolean isReactive) { + T mongoClient = Arc.container().instance(clientClass, MongoClientBeanUtil.clientLiteral(beanName(entity), isReactive)) + .get(); if (mongoClient != null) { return mongoClient; } @@ -55,14 +53,6 @@ public static T clientFromArc(MongoEntity entity, Class clientClass) { } } - @SuppressWarnings("rawtypes") - private static AnnotationLiteral clientLiteral(String name) { - if (name.startsWith(MongoClientBeanUtil.DEFAULT_MONGOCLIENT_NAME)) { - return Default.Literal.INSTANCE; - } - return NamedLiteral.of(name); - } - public static String getDatabaseName(MongoEntity entity, String clientBeanName) { MongoClients mongoClients = Arc.container().instance(MongoClients.class).get(); MongoClientConfig matchingMongoClientConfig = mongoClients.getMatchingMongoClientConfig(clientBeanName); diff --git a/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/runtime/MongoOperations.java b/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/runtime/MongoOperations.java index a2cc1e8782e25..e40b104f6b709 100644 --- a/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/runtime/MongoOperations.java +++ b/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/runtime/MongoOperations.java @@ -279,7 +279,7 @@ private MongoCollection mongoCollection(Object entity) { } private MongoDatabase mongoDatabase(MongoEntity entity) { - MongoClient mongoClient = clientFromArc(entity, MongoClient.class); + MongoClient mongoClient = clientFromArc(entity, MongoClient.class, false); if (entity != null && !entity.database().isEmpty()) { return mongoClient.getDatabase(entity.database()); }