From 8a5a0208005084d909999c7eadf788a369f44512 Mon Sep 17 00:00:00 2001 From: jzuriaga Date: Mon, 14 Sep 2020 14:09:48 +0200 Subject: [PATCH] Add CommandListener implementors to the mongoDB configuration. --- .../deployment/CommandListenerBuildItem.java | 18 ++++++++ .../deployment/MongoClientProcessor.java | 18 ++++++-- .../quarkus/mongodb/MockCommandListener.java | 18 ++++++++ .../mongodb/MongoCommandListenerTest.java | 46 +++++++++++++++++++ .../mongodb/runtime/MongoClientRecorder.java | 7 ++- .../mongodb/runtime/MongoClientSupport.java | 8 +++- .../quarkus/mongodb/runtime/MongoClients.java | 18 ++++++++ 7 files changed, 127 insertions(+), 6 deletions(-) create mode 100644 extensions/mongodb-client/deployment/src/main/java/io/quarkus/mongodb/deployment/CommandListenerBuildItem.java create mode 100644 extensions/mongodb-client/deployment/src/test/java/io/quarkus/mongodb/MockCommandListener.java create mode 100644 extensions/mongodb-client/deployment/src/test/java/io/quarkus/mongodb/MongoCommandListenerTest.java diff --git a/extensions/mongodb-client/deployment/src/main/java/io/quarkus/mongodb/deployment/CommandListenerBuildItem.java b/extensions/mongodb-client/deployment/src/main/java/io/quarkus/mongodb/deployment/CommandListenerBuildItem.java new file mode 100644 index 0000000000000..159bb5d856c8f --- /dev/null +++ b/extensions/mongodb-client/deployment/src/main/java/io/quarkus/mongodb/deployment/CommandListenerBuildItem.java @@ -0,0 +1,18 @@ +package io.quarkus.mongodb.deployment; + +import java.util.List; + +import io.quarkus.builder.item.SimpleBuildItem; + +public final class CommandListenerBuildItem extends SimpleBuildItem { + + private List commandListenerClassNames; + + public CommandListenerBuildItem(List commandListenerClassNames) { + this.commandListenerClassNames = commandListenerClassNames; + } + + public List getCommandListenerClassNames() { + return commandListenerClassNames; + } +} 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 68434bbd8275b..533fa7446a622 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 @@ -23,6 +23,7 @@ import org.jboss.jandex.IndexView; import com.mongodb.client.MongoClient; +import com.mongodb.event.CommandListener; import com.mongodb.event.ConnectionPoolListener; import io.quarkus.arc.deployment.AdditionalBeanBuildItem; @@ -82,11 +83,21 @@ BsonDiscriminatorBuildItem collectBsonDiscriminators(CombinedIndexBuildItem inde } @BuildStep - List addCodecsAndDiscriminatorsToNative(CodecProviderBuildItem codecProviders, - BsonDiscriminatorBuildItem bsonDiscriminators) { + CommandListenerBuildItem collectCommandListeners(CombinedIndexBuildItem indexBuildItem) { + Collection commandListenerClasses = indexBuildItem.getIndex() + .getAllKnownImplementors(DotName.createSimple(CommandListener.class.getName())); + List names = commandListenerClasses.stream().map(ci -> ci.name().toString()).collect(Collectors.toList()); + return new CommandListenerBuildItem(names); + } + + @BuildStep + List addCodecsAndDiscriminatorsAndListenersToNative(CodecProviderBuildItem codecProviders, + BsonDiscriminatorBuildItem bsonDiscriminators, + CommandListenerBuildItem commandListeners) { List reflectiveClassNames = new ArrayList<>(); reflectiveClassNames.addAll(codecProviders.getCodecProviderClassNames()); reflectiveClassNames.addAll(bsonDiscriminators.getBsonDiscriminatorClassNames()); + reflectiveClassNames.addAll(commandListeners.getCommandListenerClassNames()); return reflectiveClassNames.stream() .map(s -> new ReflectiveClassBuildItem(true, true, false, s)) @@ -149,6 +160,7 @@ void build( SslNativeConfigBuildItem sslNativeConfig, CodecProviderBuildItem codecProvider, BsonDiscriminatorBuildItem bsonDiscriminator, + CommandListenerBuildItem commandListener, List connectionPoolListenerProvider, BuildProducer mongoConnections, BuildProducer syntheticBeanBuildItemBuildProducer, @@ -171,7 +183,7 @@ void build( syntheticBeanBuildItemBuildProducer.produce(SyntheticBeanBuildItem.configure(MongoClientSupport.class) .scope(Singleton.class) .supplier(recorder.mongoClientSupportSupplier(codecProvider.getCodecProviderClassNames(), - bsonDiscriminator.getBsonDiscriminatorClassNames(), + bsonDiscriminator.getBsonDiscriminatorClassNames(), commandListener.getCommandListenerClassNames(), poolListenerList, sslNativeConfig.isExplicitlyDisabled())) .done()); diff --git a/extensions/mongodb-client/deployment/src/test/java/io/quarkus/mongodb/MockCommandListener.java b/extensions/mongodb-client/deployment/src/test/java/io/quarkus/mongodb/MockCommandListener.java new file mode 100644 index 0000000000000..5557bfd515a81 --- /dev/null +++ b/extensions/mongodb-client/deployment/src/test/java/io/quarkus/mongodb/MockCommandListener.java @@ -0,0 +1,18 @@ +package io.quarkus.mongodb; + +import java.util.ArrayList; +import java.util.List; + +import com.mongodb.event.CommandListener; +import com.mongodb.event.CommandStartedEvent; + +public class MockCommandListener implements CommandListener { + + public static final List EVENTS = new ArrayList<>(); + + @Override + public void commandStarted(CommandStartedEvent startedEvent) { + EVENTS.add(startedEvent.getCommandName()); + } + +} \ No newline at end of file diff --git a/extensions/mongodb-client/deployment/src/test/java/io/quarkus/mongodb/MongoCommandListenerTest.java b/extensions/mongodb-client/deployment/src/test/java/io/quarkus/mongodb/MongoCommandListenerTest.java new file mode 100644 index 0000000000000..cd0edff5a0a3f --- /dev/null +++ b/extensions/mongodb-client/deployment/src/test/java/io/quarkus/mongodb/MongoCommandListenerTest.java @@ -0,0 +1,46 @@ +package io.quarkus.mongodb; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasItems; +import static org.hamcrest.Matchers.hasSize; + +import javax.inject.Inject; + +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import com.mongodb.client.MongoClient; + +import io.quarkus.test.QuarkusUnitTest; + +public class MongoCommandListenerTest extends MongoTestBase { + + @Inject + MongoClient client; + + @RegisterExtension + static final QuarkusUnitTest config = new QuarkusUnitTest() + .setArchiveProducer( + () -> ShrinkWrap.create(JavaArchive.class).addClasses(MongoTestBase.class, MockCommandListener.class)) + .withConfigurationResource("default-mongoclient.properties"); + + @AfterEach + void cleanup() { + if (client != null) { + client.close(); + } + } + + @Test + void testClientInitialization() { + assertThat(client.listDatabaseNames().first()).isNotEmpty(); + assertThat(MockCommandListener.EVENTS, hasSize(1)); + assertThat(MockCommandListener.EVENTS, hasItems(equalTo("listDatabases"))); + } + +} 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 c37986c231b99..1e67a94c450f9 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 @@ -22,16 +22,19 @@ public class MongoClientRecorder { public Supplier mongoClientSupportSupplier(List codecProviders, List bsonDiscriminators, - List> connectionPoolListenerSuppliers, boolean disableSslSupport) { + List commandListeners, List> connectionPoolListenerSuppliers, + boolean disableSslSupport) { return new Supplier() { @Override public MongoClientSupport get() { + List connectionPoolListeners = new ArrayList<>(connectionPoolListenerSuppliers.size()); for (Supplier item : connectionPoolListenerSuppliers) { connectionPoolListeners.add(item.get()); } - return new MongoClientSupport(codecProviders, bsonDiscriminators, connectionPoolListeners, disableSslSupport); + return new MongoClientSupport(codecProviders, bsonDiscriminators, commandListeners, + connectionPoolListeners, disableSslSupport); } }; } diff --git a/extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/runtime/MongoClientSupport.java b/extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/runtime/MongoClientSupport.java index 77bd5ced1b1fb..aeb2bf8525161 100644 --- a/extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/runtime/MongoClientSupport.java +++ b/extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/runtime/MongoClientSupport.java @@ -9,13 +9,15 @@ public class MongoClientSupport { private final List codecProviders; private final List bsonDiscriminators; private final List connectionPoolListeners; + private final List commandListeners; private final boolean disableSslSupport; public MongoClientSupport(List codecProviders, List bsonDiscriminators, - List connectionPoolListeners, boolean disableSslSupport) { + List commandListeners, List connectionPoolListeners, boolean disableSslSupport) { this.codecProviders = codecProviders; this.bsonDiscriminators = bsonDiscriminators; this.connectionPoolListeners = connectionPoolListeners; + this.commandListeners = commandListeners; this.disableSslSupport = disableSslSupport; } @@ -31,6 +33,10 @@ public List getConnectionPoolListeners() { return connectionPoolListeners; } + public List getCommandListeners() { + return commandListeners; + } + public boolean isDisableSslSupport() { return disableSslSupport; } diff --git a/extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/runtime/MongoClients.java b/extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/runtime/MongoClients.java index 116024e810960..d2fa81641856c 100644 --- a/extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/runtime/MongoClients.java +++ b/extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/runtime/MongoClients.java @@ -44,6 +44,7 @@ import com.mongodb.connection.ServerSettings; import com.mongodb.connection.SocketSettings; import com.mongodb.connection.SslSettings; +import com.mongodb.event.CommandListener; import com.mongodb.event.ConnectionPoolListener; import io.quarkus.mongodb.impl.ReactiveMongoClientImpl; @@ -249,6 +250,8 @@ private MongoClientSettings createMongoConfiguration(MongoClientConfig config) { CodecRegistries.fromProviders(providers)); settings.codecRegistry(registry); + settings.commandListenerList(getCommandListeners(mongoClientSupport.getCommandListeners())); + config.applicationName.ifPresent(settings::applicationName); if (config.credentials != null) { @@ -384,6 +387,21 @@ private List getCodecProviders(List classNames) { return providers; } + private List getCommandListeners(List classNames) { + List listeners = new ArrayList<>(); + for (String name : classNames) { + try { + Class clazz = Thread.currentThread().getContextClassLoader().loadClass(name); + Constructor clazzConstructor = clazz.getConstructor(); + listeners.add((CommandListener) clazzConstructor.newInstance()); + } catch (Exception e) { + LOGGER.warnf(e, "Unable to load the command listener class %s", name); + } + } + + return listeners; + } + @PreDestroy public void stop() { for (MongoClient client : mongoclients.values()) {