private static MongoAuthority getMongoAuthorityFromURI(final MongoURI uri) throws UnknownHostException { if (uri.getHosts().size() == 1) { return MongoAuthority.direct(new ServerAddress(uri.getHosts().get(0)), uri.getCredentials()); } else { List<ServerAddress> replicaSetSeeds = new ArrayList<ServerAddress>(uri.getHosts().size()); for (String host : uri.getHosts()) replicaSetSeeds.add(new ServerAddress(host)); return MongoAuthority.dynamicSet(replicaSetSeeds, uri.getCredentials()); } }
/** * Attempts to find an existing MongoClient instance matching that URI in the holder, and * returns it if exists. Otherwise creates a new Mongo instance based on this URI and adds it to * the holder. * * @param uri the Mongo URI * @return the client * @throws MongoException * @throws UnknownHostException * @deprecated Please use {@link #connect(MongoClientURI)} instead. */ @Deprecated public Mongo connect(final MongoURI uri) throws UnknownHostException { return connect(uri.toClientURI()); }
/** * Creates a Mongo described by a URI. If only one address is used it will only connect to that * node, otherwise it will discover all nodes. If the URI contains database credentials, the * database will be authenticated lazily on first use with those credentials. * * @param uri the URI to connect to. * <p>examples: * <ul> * <li>mongodb://localhost * <li>mongodb://fred:foobar@localhost/ * </ul> * * @throws MongoException * @throws UnknownHostException * @dochub connections * @deprecated Replaced by {@link MongoClient#MongoClient(MongoClientURI)} */ @Deprecated public Mongo(MongoURI uri) throws UnknownHostException { this(getMongoAuthorityFromURI(uri), uri.getOptions()); }