private final Mongo connect_(String host, String port) {
String[] ha = host.split("[,\\s;]+");
String[] pa = port.split("[,\\s;]+");
int len = ha.length;
if (len != pa.length)
throw new ConfigurationException(
"host and ports number does not match");
if (1 == len) {
try {
return new Mongo(ha[0], Integer.parseInt(pa[0]));
} catch (Exception e) {
throw new ConfigurationException(String.format("Cannot connect to mongodb at %s:%s", host, port));
}
}
List<ServerAddress> addrs = new ArrayList<ServerAddress>(ha.length);
for (int i = 0; i < len; ++i) {
try {
addrs.add(new ServerAddress(ha[i], Integer.parseInt(pa[i])));
} catch (Exception e) {
error(e, "Error creating mongo connection to %s:%s", host, port);
}
}
if (addrs.isEmpty()) {
throw new ConfigurationException("Cannot connect to mongodb: no replica can be connected");
}
return new Mongo(addrs);
}