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 23, 2020
1 parent b7092f2 commit 3b26679
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 46 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
@@ -1,21 +1,22 @@
package io.quarkus.mongodb;

import javax.enterprise.context.ApplicationScoped;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;

import com.mongodb.event.CommandListener;
import com.mongodb.event.CommandStartedEvent;

import io.quarkus.test.Mock;

@Mock
@ApplicationScoped
public class MockCommandListener implements CommandListener {

private CommandStartedEvent commandStartedEvent;

@Override
public void commandStarted(CommandStartedEvent event) {
this.commandStartedEvent = event;
public void commandStarted(CommandStartedEvent startedEvent) {
this.commandStartedEvent = startedEvent;
assertThat(startedEvent, notNullValue());
assertThat(startedEvent.getCommandName(), anyOf(equalTo("listDatabases"), equalTo("endSessions")));
}

public CommandStartedEvent getCommandStartedEvent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.junit.jupiter.api.extension.RegisterExtension;

import com.mongodb.client.MongoClient;
import com.mongodb.event.CommandStartedEvent;

import io.quarkus.test.QuarkusUnitTest;

Expand All @@ -20,9 +19,6 @@ public class MongoCommandListenerTest extends MongoTestBase {
@Inject
MongoClient client;

@Inject
MockCommandListener listener;

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.setArchiveProducer(
Expand All @@ -39,10 +35,6 @@ void cleanup() {
@Test
void testClientInitialization() {
assertThat(client.listDatabaseNames().first()).isNotEmpty();
assertThat(listener).isNotNull();
CommandStartedEvent startedEvent = listener.getCommandStartedEvent();
assertThat(startedEvent).isNotNull();
assertThat(startedEvent.getCommandName()).isEqualTo("listDatabases");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import javax.enterprise.util.AnnotationLiteral;

import com.mongodb.client.MongoClient;
import com.mongodb.event.CommandListener;
import com.mongodb.event.ConnectionPoolListener;

import io.quarkus.arc.Arc;
Expand All @@ -22,7 +21,7 @@
@Recorder
public class MongoClientRecorder {

public Supplier<MongoClientSupport> mongoClientSupportSupplier(List<String> codecProviders, List<String> bsonDiscriminators,
public Supplier<MongoClientSupport> mongoClientSupportSupplier(List<String> codecProviders, List<String> bsonDiscriminators, List<String> commandListeners,
List<Supplier<ConnectionPoolListener>> connectionPoolListenerSuppliers, boolean disableSslSupport) {
return new Supplier<MongoClientSupport>() {
@Override
Expand All @@ -32,14 +31,9 @@ public MongoClientSupport get() {
for (Supplier<ConnectionPoolListener> item : connectionPoolListenerSuppliers) {
connectionPoolListeners.add(item.get());
}
final CommandListener commandListener = Arc.container().instance(CommandListener.class).get();

MongoClientSupport mongoClientSupport = new MongoClientSupport(codecProviders, bsonDiscriminators,
return new MongoClientSupport(codecProviders, bsonDiscriminators, commandListeners,
connectionPoolListeners, disableSslSupport);
if (null != commandListener) {
mongoClientSupport.addCommandListener(commandListener);
}
return mongoClientSupport;
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,19 @@
package io.quarkus.mongodb.runtime;

import java.util.ArrayList;
import java.util.List;

import com.mongodb.event.CommandListener;
import com.mongodb.event.ConnectionPoolListener;

public class MongoClientSupport {

private final List<String> codecProviders;
private final List<String> bsonDiscriminators;
private final List<ConnectionPoolListener> connectionPoolListeners;
private final List<CommandListener> commandListeners;
private final List<String> commandListeners;
private final boolean disableSslSupport;

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

public MongoClientSupport(List<String> codecProviders, List<String> bsonDiscriminators,
List<ConnectionPoolListener> connectionPoolListeners, List<CommandListener> commandListeners,
boolean disableSslSupport) {
List<String> commandListeners, List<ConnectionPoolListener> connectionPoolListeners, boolean disableSslSupport) {
this.codecProviders = codecProviders;
this.bsonDiscriminators = bsonDiscriminators;
this.connectionPoolListeners = connectionPoolListeners;
Expand All @@ -45,14 +33,10 @@ public List<ConnectionPoolListener> getConnectionPoolListeners() {
return connectionPoolListeners;
}

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

public void addCommandListener(CommandListener commandListener) {
commandListeners.add(commandListener);
}

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,7 +250,11 @@ private MongoClientSettings createMongoConfiguration(MongoClientConfig config) {
CodecRegistries.fromProviders(providers));
settings.codecRegistry(registry);

settings.commandListenerList(mongoClientSupport.getCommandListeners());
List<CommandListener> listeners = new ArrayList<>();
if (!mongoClientSupport.getCommandListeners().isEmpty()) {
listeners.addAll(getCommandListeners(mongoClientSupport.getCommandListeners()));
}
settings.commandListenerList(listeners);

config.applicationName.ifPresent(settings::applicationName);

Expand Down Expand Up @@ -386,6 +391,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 3b26679

Please sign in to comment.