String username = initProps.getProperty(F_MONGO_USERNAME);
String password = initProps.getProperty(F_MONGO_PASSWORD);
String dbName = initProps.getProperty(F_DATABASE);
if (dbName == null) {
throw new DataImportHandlerException(SEVERE,
"database can not be null");
}
try {
Mongo mongo = new Mongo(host, port);
this.mongo = mongo.getDB(dbName);
if (username != null) {
boolean auth = this.mongo.authenticate(username,
password.toCharArray());
if (!auth) {
throw new DataImportHandlerException(SEVERE,
"auth failed with username: " + username
+ ", password: " + password);
}
}
logger.info(String.format("mongodb [%s:%d]@%s inited", host, port, dbName));
} catch (Exception e) {
throw new DataImportHandlerException(SEVERE, "init mongodb failed");
}
}