Exemplo n.º 1
0
  public MongoManager(boolean isTest) {
    try {
      CommonsProperties.init("anhalytics.properties", isTest);
    } catch (Exception e) {
      LOGGER.error(e.getMessage());
    }

    try {
      mongo =
          new MongoClient(CommonsProperties.getMongodbServer(), CommonsProperties.getMongodbPort());

      if (!mongo.getDatabaseNames().contains(CommonsProperties.getMongodbDb())) {
        LOGGER.info(
            "MongoDB database "
                + CommonsProperties.getMongodbDb()
                + " does not exist and will be created");
      }
    } catch (MongoException | IOException ex) {
      throw new ServiceException("MongoDB is not UP, the process will be halted.");
    }
  }
Exemplo n.º 2
0
 /**
  * Initializes database if it exists and create it otherwise.
  *
  * @param dbName
  */
 protected void initDatabase() {
   db = mongo.getDB(CommonsProperties.getMongodbDb());
   if (!mongo.getDatabaseNames().contains(CommonsProperties.getMongodbDb())) {
     BasicDBObject commandArguments = new BasicDBObject();
     commandArguments.put("user", CommonsProperties.getMongodbUser());
     commandArguments.put("pwd", CommonsProperties.getMongodbPass());
     String[] roles = {"readWrite"};
     commandArguments.put("roles", roles);
     BasicDBObject command = new BasicDBObject("createUser", commandArguments);
     db.command(command);
   }
   boolean auth =
       db.authenticate(
           CommonsProperties.getMongodbUser(), CommonsProperties.getMongodbPass().toCharArray());
 }