Skip to content

Commit

Permalink
Add CommandListener implementors to the mongoDB configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
jzuriaga committed Sep 30, 2020
1 parent 943e909 commit 8a5a020
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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<String> commandListenerClassNames;

public CommandListenerBuildItem(List<String> commandListenerClassNames) {
this.commandListenerClassNames = commandListenerClassNames;
}

public List<String> getCommandListenerClassNames() {
return commandListenerClassNames;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -82,11 +83,21 @@ BsonDiscriminatorBuildItem collectBsonDiscriminators(CombinedIndexBuildItem inde
}

@BuildStep
List<ReflectiveClassBuildItem> addCodecsAndDiscriminatorsToNative(CodecProviderBuildItem codecProviders,
BsonDiscriminatorBuildItem bsonDiscriminators) {
CommandListenerBuildItem collectCommandListeners(CombinedIndexBuildItem indexBuildItem) {
Collection<ClassInfo> commandListenerClasses = indexBuildItem.getIndex()
.getAllKnownImplementors(DotName.createSimple(CommandListener.class.getName()));
List<String> names = commandListenerClasses.stream().map(ci -> ci.name().toString()).collect(Collectors.toList());
return new CommandListenerBuildItem(names);
}

@BuildStep
List<ReflectiveClassBuildItem> addCodecsAndDiscriminatorsAndListenersToNative(CodecProviderBuildItem codecProviders,
BsonDiscriminatorBuildItem bsonDiscriminators,
CommandListenerBuildItem commandListeners) {
List<String> 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))
Expand Down Expand Up @@ -149,6 +160,7 @@ void build(
SslNativeConfigBuildItem sslNativeConfig,
CodecProviderBuildItem codecProvider,
BsonDiscriminatorBuildItem bsonDiscriminator,
CommandListenerBuildItem commandListener,
List<MongoConnectionPoolListenerBuildItem> connectionPoolListenerProvider,
BuildProducer<MongoConnectionNameBuildItem> mongoConnections,
BuildProducer<SyntheticBeanBuildItem> syntheticBeanBuildItemBuildProducer,
Expand All @@ -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());

Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> EVENTS = new ArrayList<>();

@Override
public void commandStarted(CommandStartedEvent startedEvent) {
EVENTS.add(startedEvent.getCommandName());
}

}
Original file line number Diff line number Diff line change
@@ -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")));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@
public class MongoClientRecorder {

public Supplier<MongoClientSupport> mongoClientSupportSupplier(List<String> codecProviders, List<String> bsonDiscriminators,
List<Supplier<ConnectionPoolListener>> connectionPoolListenerSuppliers, boolean disableSslSupport) {
List<String> commandListeners, List<Supplier<ConnectionPoolListener>> connectionPoolListenerSuppliers,
boolean disableSslSupport) {
return new Supplier<MongoClientSupport>() {
@Override
public MongoClientSupport get() {

List<ConnectionPoolListener> connectionPoolListeners = new ArrayList<>(connectionPoolListenerSuppliers.size());
for (Supplier<ConnectionPoolListener> item : connectionPoolListenerSuppliers) {
connectionPoolListeners.add(item.get());
}

return new MongoClientSupport(codecProviders, bsonDiscriminators, connectionPoolListeners, disableSslSupport);
return new MongoClientSupport(codecProviders, bsonDiscriminators, commandListeners,
connectionPoolListeners, disableSslSupport);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ public class MongoClientSupport {
private final List<String> codecProviders;
private final List<String> bsonDiscriminators;
private final List<ConnectionPoolListener> connectionPoolListeners;
private final List<String> commandListeners;
private final boolean disableSslSupport;

public MongoClientSupport(List<String> codecProviders, List<String> bsonDiscriminators,
List<ConnectionPoolListener> connectionPoolListeners, boolean disableSslSupport) {
List<String> commandListeners, List<ConnectionPoolListener> connectionPoolListeners, boolean disableSslSupport) {
this.codecProviders = codecProviders;
this.bsonDiscriminators = bsonDiscriminators;
this.connectionPoolListeners = connectionPoolListeners;
this.commandListeners = commandListeners;
this.disableSslSupport = disableSslSupport;
}

Expand All @@ -31,6 +33,10 @@ public List<ConnectionPoolListener> getConnectionPoolListeners() {
return connectionPoolListeners;
}

public List<String> getCommandListeners() {
return commandListeners;
}

public boolean isDisableSslSupport() {
return disableSslSupport;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -384,6 +387,21 @@ private List<CodecProvider> getCodecProviders(List<String> classNames) {
return providers;
}

private List<CommandListener> getCommandListeners(List<String> classNames) {
List<CommandListener> 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()) {
Expand Down

0 comments on commit 8a5a020

Please sign in to comment.