@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) {
CouchDbClient client;
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 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()
+ ", passwordHash=" + NameUtil.md5(password + CouchDBProvider.class.getName())
+ ", maxConnections=" + properties.getMaxConnections() + ", connectionTimeout="
+ properties.getConnectionTimeout() + ", socketTimeout=" + properties.getSocketTimeout();
} 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;
}
} 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) {
if (protocol != null && protocol.length() > 0) {
protocol = protocol.toLowerCase();
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].");
}
int portInt = protocol.equals("https") ? HTTPS : HTTP;
if (port != null && port.length() > 0) {
try {
portInt = Integer.parseInt(port);
} catch (final NumberFormatException ignore) {
// we don't care
}
} else {
LOGGER.warn("No port specified, using default port [{}] for protocol [{}].", portInt, protocol);
}
if (server == null || server.length() == 0) {
server = "localhost";
LOGGER.warn("No server specified, using default server localhost.");
}
if (username == null || username.length() == 0 || password == null || password.length() == 0) {
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 + ", passwordHash="
+ NameUtil.md5(password + CouchDBProvider.class.getName());
} else {
LOGGER.error("No factory method was provided so the database name is required.");
return null;
}