in log4j-couchdb/src/main/java/org/apache/logging/log4j/couchdb/CouchDbProvider.java [85:166]
public static CouchDbProvider createNoSqlProvider(
@PluginAttribute("databaseName") final String databaseName,
@PluginAttribute("protocol") String protocol,
@PluginAttribute(value = "server", defaultString = "localhost") @ValidHost final String server,
@PluginAttribute(value = "port", defaultString = "0") @ValidPort final String port,
@PluginAttribute("username") final String username,
@PluginAttribute(value = "password", sensitive = true) final String password,
@PluginAttribute("factoryClassName") final String factoryClassName,
@PluginAttribute("factoryMethodName") final String factoryMethodName) {
CouchDbClient client;
String description;
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 CouchDbClient) {
client = (CouchDbClient) object;
description = "uri=" + client.getDBUri();
} else if (object instanceof CouchDbProperties) {
final CouchDbProperties properties = (CouchDbProperties) object;
client = new CouchDbClient(properties);
description = "uri=" + client.getDBUri() + ", username=" + properties.getUsername()
+ ", maxConnections=" + properties.getMaxConnections() + ", connectionTimeout="
+ properties.getConnectionTimeout() + ", socketTimeout=" + properties.getSocketTimeout();
} 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;
}
} 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)) {
if (protocol != null && protocol.length() > 0) {
protocol = toRootLowerCase(protocol);
if (!protocol.equals("http") && !protocol.equals("https")) {
LOGGER.error("Only protocols [http] and [https] are supported, [{}] specified.", protocol);
return null;
}
} else {
protocol = "http";
LOGGER.warn("No protocol specified, using default port [http].");
}
final int portInt = TypeConverters.convert(port, int.class, protocol.equals("https") ? HTTPS : HTTP);
if (Strings.isEmpty(username) || Strings.isEmpty(password)) {
LOGGER.error("You must provide a username and password for the CouchDB provider.");
return null;
}
client = new CouchDbClient(databaseName, false, protocol, server, portInt, username, password);
description = "uri=" + client.getDBUri() + ", username=" + username;
} else {
LOGGER.error("No factory method was provided so the database name is required.");
return null;
}
return new CouchDbProvider(client, description);
}