in log4j-mongodb3/src/main/java/org/apache/logging/log4j/mongodb3/MongoDbProvider.java [130:223]
public MongoDbProvider build() {
MongoDatabase database;
String description;
MongoClient mongoClient = null;
if (Strings.isNotEmpty(factoryClassName) && Strings.isNotEmpty(factoryMethodName)) {
try {
final Class<?> factoryClass = LoaderUtil.loadClass(factoryClassName);
final Method method = factoryClass.getMethod(factoryMethodName);
final Object object = method.invoke(null);
if (object instanceof MongoDatabase) {
database = (MongoDatabase) object;
} else if (object instanceof MongoClient) {
if (Strings.isNotEmpty(databaseName)) {
database = ((MongoClient) object).getDatabase(databaseName);
} else {
LOGGER.error("The factory method [{}.{}()] returned a MongoClient so the database name is "
+ "required.", factoryClassName, factoryMethodName);
return null;
}
} else {
if (object == null) {
LOGGER.error("The factory method [{}.{}()] returned null.", factoryClassName,
factoryMethodName);
} else {
LOGGER.error("The factory method [{}.{}()] returned an unsupported type [{}].",
factoryClassName, factoryMethodName, object.getClass().getName());
}
return null;
}
final String databaseName = database.getName();
description = "database=" + databaseName;
} catch (final ClassNotFoundException e) {
LOGGER.error("The factory class [{}] could not be loaded.", factoryClassName, e);
return null;
} catch (final NoSuchMethodException e) {
LOGGER.error("The factory class [{}] does not have a no-arg method named [{}].", factoryClassName,
factoryMethodName, e);
return null;
} catch (final Exception e) {
LOGGER.error("The factory method [{}.{}()] could not be invoked.", factoryClassName,
factoryMethodName, e);
return null;
}
} else if (Strings.isNotEmpty(databaseName)) {
MongoCredential mongoCredential = null;
description = "database=" + databaseName;
if (Strings.isNotEmpty(userName) && Strings.isNotEmpty(password)) {
description += ", username=" + userName;
mongoCredential = MongoCredential.createCredential(userName, databaseName, password.toCharArray());
}
try {
final int portInt = TypeConverters.convert(port, int.class, DEFAULT_PORT);
description += ", server=" + server + ", port=" + portInt;
final WriteConcern writeConcern = toWriteConcern(writeConcernConstant, writeConcernConstantClassName);
// @formatter:off
final MongoClientOptions options = MongoClientOptions.builder()
.codecRegistry(CODEC_REGISTRIES)
.writeConcern(writeConcern)
.build();
// @formatter:on
final ServerAddress serverAddress = new ServerAddress(server, portInt);
mongoClient = mongoCredential == null ?
// @formatter:off
new MongoClient(serverAddress, options) :
new MongoClient(serverAddress, mongoCredential, options);
// @formatter:on
database = mongoClient.getDatabase(databaseName);
} catch (final Exception e) {
LOGGER.error("Failed to obtain a database instance from the MongoClient at server [{}] and "
+ "port [{}].", server, port);
close(mongoClient);
return null;
}
} else {
LOGGER.error("No factory method was provided so the database name is required.");
close(mongoClient);
return null;
}
try {
database.listCollectionNames().first(); // Check if the database actually requires authentication
} catch (final Exception e) {
LOGGER.error(
"The database is not up, or you are not authenticated, try supplying a username and password to the MongoDB provider.",
e);
close(mongoClient);
return null;
}
return new MongoDbProvider(mongoClient, database, collectionName, capped, collectionSize, description);
}