use of com.mongodb.ServerAddress in project jackrabbit-oak by apache.
the class NodeCollectionProvider method prepareClientForHostname.
private MongoClient prepareClientForHostname(String hostname) throws UnknownHostException {
ServerAddress address;
if (hostname.contains(":")) {
String[] hostSplit = hostname.split(":");
if (hostSplit.length != 2) {
throw new IllegalArgumentException("Not a valid hostname: " + hostname);
}
address = new ServerAddress(hostSplit[0], Integer.parseInt(hostSplit[1]));
} else {
address = new ServerAddress(hostname);
}
MongoClientURI originalUri = new MongoClientURI(originalMongoUri);
List<MongoCredential> credentialList = new ArrayList<MongoCredential>(1);
if (originalUri.getCredentials() != null) {
credentialList.add(originalUri.getCredentials());
}
return new MongoClient(address, credentialList, originalUri.getOptions());
}
use of com.mongodb.ServerAddress in project qi4j-sdk by Qi4j.
the class MongoMapEntityStoreMixin method loadConfiguration.
private void loadConfiguration() throws UnknownHostException {
configuration.refresh();
MongoEntityStoreConfiguration config = configuration.get();
// Combine hostname, port and nodes configuration properties
// If no configuration, use 127.0.0.1:27017
serverAddresses = new ArrayList<>();
int port = config.port().get() == null ? 27017 : config.port().get();
if (config.nodes().get().isEmpty()) {
String hostname = config.hostname().get() == null ? "127.0.0.1" : config.hostname().get();
serverAddresses.add(new ServerAddress(hostname, port));
} else {
if (config.hostname().get() != null && !config.hostname().get().isEmpty()) {
serverAddresses.add(new ServerAddress(config.hostname().get(), port));
}
serverAddresses.addAll(config.nodes().get());
}
// If database name not configured, set it to qi4j:entitystore
databaseName = config.database().get();
if (databaseName == null) {
databaseName = DEFAULT_DATABASE_NAME;
}
// If collection name not configured, set it to qi4j:entitystore:entities
collectionName = config.collection().get();
if (collectionName == null) {
collectionName = DEFAULT_COLLECTION_NAME;
}
// If write concern not configured, set it to normal
switch(config.writeConcern().get()) {
case FSYNC_SAFE:
writeConcern = WriteConcern.FSYNC_SAFE;
break;
case JOURNAL_SAFE:
writeConcern = WriteConcern.JOURNAL_SAFE;
break;
case MAJORITY:
writeConcern = WriteConcern.MAJORITY;
break;
case REPLICAS_SAFE:
writeConcern = WriteConcern.REPLICAS_SAFE;
break;
case SAFE:
writeConcern = WriteConcern.SAFE;
break;
case NORMAL:
default:
writeConcern = WriteConcern.NORMAL;
}
// Username and password are defaulted to empty strings
username = config.username().get();
password = config.password().get().toCharArray();
}
use of com.mongodb.ServerAddress in project leopard by tanhaichao.
the class DfsGridImpl method loadGridFS.
@SuppressWarnings("deprecation")
private synchronized GridFS loadGridFS() {
if (fs != null) {
return fs;
}
String[] list = server.split(":");
String host = list[0];
int port = Integer.parseInt(list[1]);
String username = null;
String password = null;
if (list.length >= 4) {
username = list[2];
password = list[3];
}
int connectTimeout = 1000 * 60;
MongoClientOptions options = new MongoClientOptions.Builder().connectTimeout(connectTimeout).build();
client = new MongoClient(new ServerAddress(host, port), options);
// @SuppressWarnings("deprecation")
DB db = client.getDB("dfs");
if (username != null && username.length() > 0) {
if (password != null && password.length() > 0) {
db.addUser(username, password.toCharArray());
}
}
return new GridFS(db, "fs");
}
use of com.mongodb.ServerAddress in project leopard by tanhaichao.
the class MongoImpl method init.
@SuppressWarnings("deprecation")
public void init() {
String[] list = server.split(":");
String host = list[0];
int port = Integer.parseInt(list[1]);
int connectTimeout = 1000 * 60;
MongoClientOptions options = new MongoClientOptions.Builder().connectTimeout(connectTimeout).build();
client = new MongoClient(new ServerAddress(host, port), options);
this.db = client.getDB(this.database);
if (username != null && username.length() > 0) {
if (password != null && password.length() > 0) {
db.addUser(username, password.toCharArray());
}
}
this.collection = db.getCollection(collectionName);
}
use of com.mongodb.ServerAddress in project spring-data-mongodb by spring-projects.
the class MongoNamespaceReplicaSetTests method testParsingMongoWithReplicaSets.
@Test
@SuppressWarnings("unchecked")
public void testParsingMongoWithReplicaSets() throws Exception {
assertTrue(ctx.containsBean("replicaSetMongo"));
MongoClientFactoryBean mfb = (MongoClientFactoryBean) ctx.getBean("&replicaSetMongo");
List<ServerAddress> replicaSetSeeds = (List<ServerAddress>) ReflectionTestUtils.getField(mfb, "replicaSetSeeds");
assertThat(replicaSetSeeds, is(notNullValue()));
assertThat(replicaSetSeeds, hasItems(new ServerAddress(InetAddress.getByName("127.0.0.1"), 10001), new ServerAddress(InetAddress.getByName("localhost"), 10002)));
}
Aggregations