@PluginAttr("port") final String port,
@PluginAttr("username") final String username,
@PluginAttr("password") final String password,
@PluginAttr("factoryClassName") final String factoryClassName,
@PluginAttr("factoryMethodName") final String factoryMethodName) {
DB database;
String description;
if (factoryClassName != null && factoryClassName.length() > 0 &&
factoryMethodName != null && factoryMethodName.length() > 0) {
try {
final Class<?> factoryClass = Class.forName(factoryClassName);
final Method method = factoryClass.getMethod(factoryMethodName);
final Object object = method.invoke(null);
if (object instanceof DB) {
database = (DB) object;
} else if (object instanceof MongoClient) {
if (databaseName != null && databaseName.length() > 0) {
database = ((MongoClient) object).getDB(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);
return null;
} else {
LOGGER.error("The factory method [{}.{}()] returned an unsupported type [{}].", factoryClassName,
factoryMethodName, object.getClass().getName());
return null;
}
description = "database=" + database.getName();
final List<ServerAddress> addresses = database.getMongo().getAllAddress();
if (addresses.size() == 1) {
description += ", server=" + addresses.get(0).getHost() + ", port=" + addresses.get(0).getPort();
} else {
description += ", servers=[";
for (final ServerAddress address : addresses) {
description += " { " + address.getHost() + ", " + address.getPort() + " } ";
}
description += "]";
}
} 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 (databaseName != null && databaseName.length() > 0) {
description = "database=" + databaseName;
try {
if (server != null && server.length() > 0) {
int portInt = 0;
if (port != null && port.length() > 0) {
try {
portInt = Integer.parseInt(port);
} catch (final NumberFormatException ignore) {
// we don't care
}
}
description += ", server=" + server;
if (portInt > 0) {
description += ", port=" + portInt;
database = new MongoClient(server, portInt).getDB(databaseName);
} else {
database = new MongoClient(server).getDB(databaseName);
}
} else {
database = new MongoClient().getDB(databaseName);
}
} catch (final Exception e) {
LOGGER.error("Failed to obtain a database instance from the MongoClient at server [{}] and "
+ "port [{}].", server, port);
return null;
}
} else {
LOGGER.error("No factory method was provided so the database name is required.");
return null;
}
if (!database.isAuthenticated()) {
if (username != null && username.length() > 0 && password != null && password.length() > 0) {
description += ", username=" + username + ", passwordHash="
+ NameUtil.md5(password + MongoDBProvider.class.getName());
} else {
LOGGER.error("The database is not already authenticated so you must supply a username and password "