Skip to content

Commit

Permalink
Merge pull request #11784 from geoand/#11765
Browse files Browse the repository at this point in the history
Fix reactive Mongo client CDI handling
  • Loading branch information
geoand authored Sep 1, 2020
2 parents 2ec63d0 + dd765d9 commit 56bd273
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -263,19 +263,19 @@ 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();
}

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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<BuildChainBuilder> buildCustomizer() {
return new Consumer<BuildChainBuilder>() {
// 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<MongoClientBuildItem> mongoClientBuildItems = context.consumeMulti(MongoClientBuildItem.class);
context.produce(new FeatureBuildItem("dummy"));
}).consumes(MongoClientBuildItem.class)
.produces(FeatureBuildItem.class)
.build();
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
quarkus.mongodb.connection-string=mongodb://localhost:27018
quarkus.mongodb.second.connection-string=mongodb://localhost:27018
Original file line number Diff line number Diff line change
@@ -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 = "<default>";
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public MongoClientSupport get() {
}

public Supplier<MongoClient> mongoClientSupplier(String clientName,
@SuppressWarnings("usused") MongodbConfig mongodbConfig) {
@SuppressWarnings("unused") MongodbConfig mongodbConfig) {
MongoClient mongoClient = Arc.container().instance(MongoClients.class).get().createMongoClient(clientName);
return new Supplier<MongoClient>() {
@Override
Expand All @@ -48,7 +48,7 @@ public MongoClient get() {
}

public Supplier<ReactiveMongoClient> reactiveMongoClientSupplier(String clientName,
@SuppressWarnings("usused") MongodbConfig mongodbConfig) {
@SuppressWarnings("unused") MongodbConfig mongodbConfig) {
ReactiveMongoClient reactiveMongoClient = Arc.container().instance(MongoClients.class).get()
.createReactiveMongoClient(clientName);
return new Supplier<ReactiveMongoClient>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,8 +25,9 @@ public static String beanName(MongoEntity entity) {
return MongoClientBeanUtil.DEFAULT_MONGOCLIENT_NAME;
}

public static <T> T clientFromArc(MongoEntity entity, Class<T> clientClass) {
T mongoClient = Arc.container().instance(clientClass, clientLiteral(beanName(entity))).get();
public static <T> T clientFromArc(MongoEntity entity, Class<T> clientClass, boolean isReactive) {
T mongoClient = Arc.container().instance(clientClass, MongoClientBeanUtil.clientLiteral(beanName(entity), isReactive))
.get();
if (mongoClient != null) {
return mongoClient;
}
Expand All @@ -55,14 +53,6 @@ public static <T> T clientFromArc(MongoEntity entity, Class<T> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down

0 comments on commit 56bd273

Please sign in to comment.