Ejemplo n.º 1
0
 private void parseOptionsFromURI(MongoClientURI uri, Map<String, String> options) {
   if (uri.getUsername() != null && uri.getUsername().isEmpty() == false) {
     options.put(MongoDumpOptions.USERNAME, uri.getUsername());
   }
   if (uri.getPassword() != null && uri.getPassword().length > 0) {
     options.put(MongoDumpOptions.PASSWORD, new String(uri.getPassword()));
   }
   if (uri.getHosts() != null && uri.getHosts().isEmpty() == false) {
     final String hostWithPort = uri.getHosts().get(0);
     final Pattern p = Pattern.compile("(.*)(:(\\d+))?");
     final Matcher m = p.matcher(hostWithPort);
     if (m.matches()) {
       final String host = m.group(1);
       final String port = m.group(3);
       if (host.isEmpty() == false) {
         options.put(MongoDumpOptions.HOST, host);
         if (port != null) {
           options.put(MongoDumpOptions.PORT, port);
         }
       }
     }
   }
   if (uri.getDatabase() != null && uri.getDatabase().isEmpty() == false) {
     options.put(MongoDumpOptions.DB, uri.getDatabase());
   }
   if (uri.getCollection() != null && uri.getCollection().isEmpty() == false) {
     options.put(MongoDumpOptions.COLLECTION, uri.getCollection());
   }
 }
  public MongoRolePersonRepository2(final String mongoUri) {
    super();

    // WARNING: mongoUri may contain password!
    final MongoClientURI realMongoUri = new MongoClientURI(mongoUri);
    log.info(
        "Connecting to MongoDB role-person repository {}/{} as {}, rolePerson collection={}",
        realMongoUri.getHosts(),
        realMongoUri.getDatabase(),
        realMongoUri.getUsername(),
        ROLE_PERSON_COLL_NAME);
    try {
      final DB db = MongoUtils.getDb(realMongoUri, ReadPreference.primary());
      rolePersonColl = db.getCollection(ROLE_PERSON_COLL_NAME);

      morphia = new Morphia();
      morphia.map(Role2.class);
      morphia.getMapper().getOptions().objectFactory =
          new DefaultCreator() {
            @Override
            public Object createInstance(Class clazz, DBObject dbObj) {
              // TODO: Do not hardcode
              if (clazz == Email.class) {
                return new EmailImpl();
              }
              return super.createInstance(clazz, dbObj);
            }
          };
    } catch (Exception e) {
      throw new MongoRepositoryException(
          e,
          "Cannot connect to MongoDB role-person repository {}/{} as {} for collection '{}'",
          realMongoUri.getHosts(),
          realMongoUri.getDatabase(),
          realMongoUri.getUsername(),
          ROLE_PERSON_COLL_NAME);
    }
  }
 public void setMongoURI(MongoClientURI uri) {
   hostsListModel.clear();
   optionsListModel.clear();
   for (String string : uri.getHosts()) {
     final String[] rawHost = string.split(":");
     final String hostname = urlDecode(rawHost[0]);
     final Integer port = rawHost.length > 1 ? Integer.parseInt(rawHost[1]) : null;
     hostsListModel.addElement(new Host(hostname, port));
   }
   usernameField.setText(uri.getUsername());
   if (uri.getPassword() != null) {
     passwordField.setText(new String(uri.getPassword()));
   }
   databaseField.setText(uri.getDatabase());
   final List<Option> options = decodeOptions(uri);
   optionsListModel.clear();
   for (Option option : options) {
     optionsListModel.addElement(option);
   }
   updateURIField();
 }
 @JsonIgnore
 public List<String> getHosts() {
   return clientURI.getHosts();
 }