public void connect() {
try {
ConnectDialog dialog = (ConnectDialog) getBoundUnit(Item.connectDialog);
ProgressDialog progress = (ProgressDialog) getBoundUnit(Item.connectProgressDialog);
MongoClient mongo = null;
List<String> dbs = new ArrayList<String>();
String uri = dialog.getStringFieldValue(ConnectDialog.Item.uri);
if (!uri.trim().isEmpty()) {
if (!uri.startsWith(MongoURI.MONGODB_PREFIX)) {
uri = MongoURI.MONGODB_PREFIX + uri;
}
MongoClientURI muri = new MongoClientURI(uri);
mongo = new MongoClient(muri);
String db = muri.getDatabase();
if (db != null && !db.trim().isEmpty()) {
dbs.add(db.trim());
}
} else {
String servers = dialog.getStringFieldValue(ConnectDialog.Item.servers);
if (servers.trim().isEmpty()) {
return;
}
String[] serverList = servers.split(",");
ArrayList<ServerAddress> addrs = new ArrayList<ServerAddress>();
for (String server : serverList) {
String[] tmp = server.split(":");
if (tmp.length > 1) {
addrs.add(new ServerAddress(tmp[0], Integer.valueOf(tmp[1]).intValue()));
} else {
addrs.add(new ServerAddress(tmp[0]));
}
}
if ("Direct".equals(dialog.getStringFieldValue(ConnectDialog.Item.connectionMode)))
mongo = new MongoClient(addrs.get(0), dialog.getMongoClientOptions());
else
mongo = new MongoClient(addrs, dialog.getMongoClientOptions());
String sdbs = dialog.getStringFieldValue(ConnectDialog.Item.databases);
if (!sdbs.trim().isEmpty()) {
for (String db : sdbs.split(",")) {
dbs.add(db.trim());
}
}
}
if (dbs.size() == 0) {
dbs = null;
}
String user = dialog.getStringFieldValue(ConnectDialog.Item.user).trim();
String password = dialog.getStringFieldValue(ConnectDialog.Item.password);
if (!user.isEmpty()) {
// authenticate against all dbs
if (dbs != null) {
for (String db : dbs) {
mongo.getDB(db).authenticate(user, password.toCharArray());
}
} else {
mongo.getDB("admin").authenticate(user, password.toCharArray());
}
}
final MongoClient fmongo = mongo;
final List<String> fdbs = dbs;
// doing in background can mean concurrent modification, but dialog is modal so unlikely
progress.show(new ProgressDialogWorker(progress) {
@Override
protected void finished() {