List of usage examples for com.mongodb MongoCredential createCredential
public static MongoCredential createCredential(final String userName, final String database, final char[] password)
From source file:uk.ac.ebi.eva.dbmigration.mongodb.MongobeeHelper.java
License:Apache License
private static List<MongoCredential> getCredentials(DatabaseParameters databaseParameters) { if (hasText(databaseParameters.getDbUsername()) && hasText(databaseParameters.getDbPassword())) { String authenticationDatabase = databaseParameters.getDbAuthenticationDatabase(); if (!hasText(authenticationDatabase)) { authenticationDatabase = databaseParameters.getDbName(); }/*from ww w. jav a 2s. co m*/ MongoCredential mongoCredential = MongoCredential.createCredential(databaseParameters.getDbUsername(), authenticationDatabase, databaseParameters.getDbPassword().toCharArray()); return Collections.singletonList(mongoCredential); } else { return Collections.emptyList(); } }
From source file:uk.ac.ebi.eva.lib.datastore.DBAdaptorConnector.java
License:Apache License
/** * Get a MongoClient using the configuration (credentials) in a given Properties. * * @param properties can have the next values: * - eva.mongo.auth.db authentication database * - eva.mongo.host comma-separated strings of colon-separated host and port strings: host_1:port_1,host_2:port_2 * - eva.mongo.user/*from ww w . ja v a 2 s .c o m*/ * - eva.mongo.passwd * - eva.mongo.read-preference string, "secondaryPreferred" if unspecified. one of: * [primary, primaryPreferred, secondary, secondaryPreferred, nearest] * @return MongoClient with given credentials * @throws UnknownHostException */ public static MongoClient getMongoClient(Properties properties) throws UnknownHostException { String[] hosts = properties.getProperty("eva.mongo.host").split(","); List<ServerAddress> servers = new ArrayList<>(); // Get the list of hosts (optionally including the port number) for (String host : hosts) { String[] params = host.split(":"); if (params.length > 1) { servers.add(new ServerAddress(params[0], Integer.parseInt(params[1]))); } else { servers.add(new ServerAddress(params[0], 27017)); } } List<MongoCredential> mongoCredentialList = null; String authenticationDb = properties.getProperty("eva.mongo.auth.db", null); if (authenticationDb != null && !authenticationDb.isEmpty()) { mongoCredentialList = Collections .singletonList(MongoCredential.createCredential(properties.getProperty("eva.mongo.user"), authenticationDb, properties.getProperty("eva.mongo.passwd").toCharArray())); } String readPreference = properties.getProperty("eva.mongo.read-preference"); readPreference = readPreference == null || readPreference.isEmpty() ? "secondaryPreferred" : readPreference; MongoClientOptions options = MongoClientOptions.builder() .readPreference(ReadPreference.valueOf(readPreference)).build(); return new MongoClient(servers, mongoCredentialList, options); }
From source file:uk.ac.ebi.eva.pipeline.configuration.MongoConfiguration.java
License:Apache License
private static MongoClient getMongoClient(MongoConnection mongoConnection) throws UnknownHostException { String authenticationDatabase = null; String user = null;/* ww w . ja va 2 s . co m*/ String password = null; MongoClient mongoClient; // The Mongo API is not happy to deal with empty strings for authentication DB, user and password if (mongoConnection.getAuthenticationDatabase() != null && !mongoConnection.getAuthenticationDatabase().trim().isEmpty()) { authenticationDatabase = mongoConnection.getAuthenticationDatabase(); } if (mongoConnection.getUser() != null && !mongoConnection.getUser().trim().isEmpty()) { user = mongoConnection.getUser(); } if (mongoConnection.getPassword() != null && !mongoConnection.getPassword().trim().isEmpty()) { password = mongoConnection.getPassword(); } if (user == null || password == null) { mongoClient = new MongoClient(MongoDBHelper.parseServerAddresses(mongoConnection.getHosts())); } else { mongoClient = new MongoClient(MongoDBHelper.parseServerAddresses(mongoConnection.getHosts()), Collections.singletonList(MongoCredential.createCredential(mongoConnection.getUser(), authenticationDatabase, mongoConnection.getPassword().toCharArray()))); } mongoClient.setReadPreference(mongoConnection.getReadPreference()); return mongoClient; }
From source file:uk.ac.ebi.eva.utils.ConnectionHelper.java
License:Apache License
public static MongoClient getMongoClient(String hosts, String authenticationDB, String user, char[] password) throws UnknownHostException { if (mongoClientWithAuthentication == null) { mongoClientWithAuthentication = new MongoClient(parseServerAddresses(hosts), Collections.singletonList(MongoCredential.createCredential(user, authenticationDB, password))); }//from w w w .j a va2 s .co m return mongoClientWithAuthentication; }
From source file:uk.ac.ebi.eva.vcfdump.server.configuration.DBAdaptorConnector.java
License:Apache License
/** * Get a MongoClient using the configuration (credentials) in a given Properties. * * @param springDataMongoDbProperties can have the next values: * - eva.mongo.auth.db authentication database * - eva.mongo.host comma-separated strings of colon-separated host and port strings: host_1:port_1,host_2:port_2 * - eva.mongo.user//from w w w . j ava 2 s .c om * - eva.mongo.passwd * - eva.mongo.read-preference string, "secondaryPreferred" if unspecified. one of: * [primary, primaryPreferred, secondary, secondaryPreferred, nearest] * @return MongoClient with given credentials * @throws UnknownHostException */ public static MongoClient getMongoClient(SpringDataMongoDbProperties springDataMongoDbProperties) throws UnknownHostException { String[] hosts = springDataMongoDbProperties.getHost().split(","); List<ServerAddress> servers = new ArrayList<>(); // Get the list of hosts (optionally including the port number) for (String host : hosts) { String[] params = host.split(":"); if (params.length > 1) { servers.add(new ServerAddress(params[0], Integer.parseInt(params[1]))); } else { servers.add(new ServerAddress(params[0], 27017)); } } List<MongoCredential> mongoCredentialList = new ArrayList<>(); String authenticationDb = springDataMongoDbProperties.getAuthenticationDatabase(); if (authenticationDb != null && !authenticationDb.isEmpty()) { mongoCredentialList = Collections .singletonList(MongoCredential.createCredential(springDataMongoDbProperties.getUsername(), authenticationDb, springDataMongoDbProperties.getPassword().toCharArray())); } String readPreference = springDataMongoDbProperties.getReadPreference(); readPreference = readPreference == null || readPreference.isEmpty() ? "secondaryPreferred" : readPreference; MongoClientOptions options = MongoClientOptions.builder() .readPreference(ReadPreference.valueOf(readPreference)).build(); return new MongoClient(servers, mongoCredentialList, options); }
From source file:xyz.wang11.log4mongo.MongoDbAppender.java
License:Apache License
protected MongoClient getMongo(List<ServerAddress> addresses) { if (userName != null && userName.trim().length() > 0) { MongoCredential credential = MongoCredential.createCredential(userName, databaseName, password.toCharArray()); if (addresses.size() < 2) { return new MongoClient(addresses.get(0), Arrays.asList(credential)); } else {/*from w w w . j ava 2s. com*/ // Replica set return new MongoClient(addresses, Arrays.asList(credential)); } } else { if (addresses.size() < 2) { return new MongoClient(addresses.get(0)); } else { // Replica set return new MongoClient(addresses); } } }