sLoop:
for (String server : serverList) {
server = server.trim();
String[] tokens = server.split("/");
if (tokens.length != 2) {
new InfoDialog(null, "Error", null, "Server format must be like 'hostname:port/shard', one by line").show();
return;
}
server = tokens[0];
String shard = tokens[1];
if (primaryShard == null) {
primaryShard = shard;
}
ServerAddress addr = new ServerAddress(server);
// filter out if replset already exists
for (MongoClient replset : mongoToShard.keySet()) {
if (replset.getServerAddressList().contains(addr)) {
continue sLoop;
}
}
list.clear();
list.add(addr);
MongoClient mongo = new MongoClient(list);
// UMongo.instance.addMongoClient(mongo, null);
// make request to force server detection
mongo.getDatabaseNames();
mongoToShard.put(mongo, shard);
String desc = null;
if (!mongo.getDatabaseNames().contains(db)
|| !mongo.getDB(db).getCollectionNames().contains(col)) {
desc = "Collection not present!";
} else {
// try to see if shard key has index
DBObject index = mongo.getDB(db).getCollection("system.indexes").findOne(new BasicDBObject("key", shardKey));
if (index != null) {
desc = "shard key found";
} else {
desc = "shard key NOT found!";
}
}
txt += mongo.toString() + " shard=" + shard + " - " + desc + "\n";
BasicDBObject shardObj = new BasicDBObject("servers", mongo.toString());
shardObj.put("status", desc);
if (shardList.containsField(shard)) {
new InfoDialog(null, "Error", null, "Duplicate Shard name " + shard).show();
return;
}
shardList.put(shard, shardObj);
}
result.put("shards", shardList);