List of usage examples for com.mongodb MongoCredential getUserName
@Nullable
public String getUserName()
From source file:com.meltmedia.dropwizard.mongo.MongoBundle.java
License:Apache License
MongoClient buildClient(MongoConfiguration configuration) { try {/*from w w w . j av a 2 s . c om*/ // build the seed server list. List<ServerAddress> servers = new ArrayList<>(); for (Server seed : configuration.getSeeds()) { servers.add(new ServerAddress(seed.getHost(), seed.getPort())); } log.info("Found {} mongo seed servers", servers.size()); for (ServerAddress server : servers) { log.info("Found mongo seed server {}:{}", server.getHost(), server.getPort()); } // build the credentials Credentials credentialConfig = configuration.getCredentials(); List<MongoCredential> credentials = credentialConfig == null ? Collections.<MongoCredential>emptyList() : Collections.singletonList(MongoCredential.createCredential(credentialConfig.getUserName(), configuration.getDatabase(), credentialConfig.getPassword().toCharArray())); if (credentials.isEmpty()) { log.info("Found {} mongo credentials.", credentials.size()); } else { for (MongoCredential credential : credentials) { log.info("Found mongo credential for {} on database {}.", credential.getUserName(), credential.getSource()); } } // build the options. MongoClientOptions options = new MongoClientOptions.Builder() .writeConcern(writeConcern(configuration.getWriteConcern())).build(); log.info("Mongo database is {}", configuration.getDatabase()); return new MongoClient(servers, credentials, options); } catch (Exception e) { throw new RuntimeException("Could not configure MongoDB client.", e); } }
From source file:com.redhat.lightblue.mongo.config.MongoConfiguration.java
License:Open Source License
public static String toString(MongoCredential cr) { StringBuilder bld = new StringBuilder(); bld.append("{mechanism:").append(cr.getMechanism()); if (cr.getUserName() != null) { bld.append(" userName:").append(cr.getUserName()); }/* w ww .j a v a 2 s . c om*/ if (cr.getPassword() != null) { bld.append(" password:").append(cr.getPassword()); } if (cr.getSource() != null) { bld.append(" source:").append(cr.getSource()); } bld.append('}'); return bld.toString(); }
From source file:org.apache.drill.exec.store.mongo.MongoCnxnManager.java
License:Apache License
public synchronized static MongoClient getClient(List<ServerAddress> addresses, MongoClientOptions clientOptions, MongoCredential credential) throws UnknownHostException { // Take the first replica from the replicated servers ServerAddress serverAddress = addresses.get(0); String userName = credential == null ? null : credential.getUserName(); MongoCnxnKey key = new MongoCnxnKey(serverAddress, userName); MongoClient client = addressClientMap.getIfPresent(key); if (client == null) { if (credential != null) { List<MongoCredential> credentialList = Arrays.asList(credential); client = new MongoClient(addresses, credentialList, clientOptions); } else {/*from www .j av a 2 s.co m*/ client = new MongoClient(addresses, clientOptions); } addressClientMap.put(key, client); logger.debug("Created connection to {}.", key.toString()); logger.debug("Number of open connections {}.", addressClientMap.size()); } return client; }
From source file:org.apache.drill.exec.store.mongo.MongoStoragePlugin.java
License:Apache License
public synchronized MongoClient getClient(List<ServerAddress> addresses) { // Take the first replica from the replicated servers final ServerAddress serverAddress = addresses.get(0); final MongoCredential credential = clientURI.getCredentials(); String userName = credential == null ? null : credential.getUserName(); MongoCnxnKey key = new MongoCnxnKey(serverAddress, userName); MongoClient client = addressClientMap.getIfPresent(key); if (client == null) { if (credential != null) { List<MongoCredential> credentialList = Arrays.asList(credential); client = new MongoClient(addresses, credentialList, clientURI.getOptions()); } else {//from ww w. ja v a 2s .c om client = new MongoClient(addresses, clientURI.getOptions()); } addressClientMap.put(key, client); logger.debug("Created connection to {}.", key.toString()); logger.debug("Number of open connections {}.", addressClientMap.size()); } return client; }
From source file:org.flywaydb.core.internal.metadatatable.MongoMetaDataTable.java
License:Apache License
@Override public void addBaselineMarker(MigrationVersion baselineVersion, String baselineDescription) { MongoCredential dbCreds; String username = ""; try {//from w w w .ja v a2 s. co m dbCreds = client.getCredentialsList().get(0); username = dbCreds.getUserName(); } catch (Exception e) { LOG.info("Continuing to baseline without Mongo credentials"); } MetaDataDocument baselineMarker = new MetaDataDocument(0, baselineVersion, baselineDescription, MigrationType.BASELINE, baselineDescription, null, new Date(), username, 0, true); metadataCollection.insertOne(baselineMarker); }
From source file:org.pentaho.mongo.MongoUtils.java
License:Open Source License
/** * Return a list of custom "lastErrorModes" (if any) defined in the replica * set configuration object on the server. These can be used as the "w" * setting for the write concern in addition to the standard "w" values of * <number> or "majority"./*from ww w. j a va2 s.co m*/ * * @param hostsPorts the hosts to use * @param singlePort the default port to use if no ports are given in the * hostsPorts spec * @param username the username for authentication * @param password the password for authentication * @param vars the environment variables to use * @param log for logging * @return a list of the names of any custom "lastErrorModes" * @throws KettleException if a problem occurs */ public static List<String> getLastErrorModes(String hostsPorts, String singlePort, MongoCredential cred, VariableSpace vars, LogChannelInterface log) throws KettleException { List<String> customLastErrorModes = new ArrayList<String>(); MongoClient mongo = null; try { if (cred != null && cred.getMechanism().equals(MongoCredential.MONGODB_CR_MECHANISM)) { // need to make a new credential that specifies the local database cred = MongoCredential.createMongoCRCredential(cred.getUserName(), LOCAL_DB, cred.getPassword()); } mongo = initConnection(hostsPorts, singlePort, cred, false, null, null, null, null, null, false, null, vars, log); DB local = mongo.getDB(LOCAL_DB); if (local != null) { DBCollection replset = local.getCollection(REPL_SET_COLLECTION); if (replset != null) { DBObject config = replset.findOne(); extractLastErrorModes(config, customLastErrorModes); } } } finally { if (mongo != null) { mongo.close(); } } return customLastErrorModes; }
From source file:org.pentaho.mongo.MongoUtils.java
License:Open Source License
protected static BasicDBList getRepSetMemberRecords(String hostsPorts, String singlePort, MongoCredential cred, VariableSpace vars, LogChannelInterface log) throws KettleException { MongoClient mongo = null;/*from w w w .ja v a 2s . co m*/ BasicDBList setMembers = null; try { if (cred != null && cred.getMechanism().equals(MongoCredential.MONGODB_CR_MECHANISM)) { // need to make a new credential that specifies the local database cred = MongoCredential.createMongoCRCredential(cred.getUserName(), LOCAL_DB, cred.getPassword()); } mongo = initConnection(hostsPorts, singlePort, cred, false, null, null, null, null, null, false, null, vars, log); DB local = mongo.getDB(LOCAL_DB); if (local != null) { DBCollection replset = local.getCollection(REPL_SET_COLLECTION); if (replset != null) { DBObject config = replset.findOne(); if (config != null) { Object members = config.get(REPL_SET_MEMBERS); if (members instanceof BasicDBList) { if (((BasicDBList) members).size() == 0) { // log that there are no replica set members defined if (log != null) { log.logBasic(BaseMessages.getString(PKG, "MongoUtils.Message.Warning.NoReplicaSetMembersDefined")); //$NON-NLS-1$ } } else { setMembers = (BasicDBList) members; } } else { // log that there are no replica set members defined if (log != null) { log.logBasic(BaseMessages.getString(PKG, "MongoUtils.Message.Warning.NoReplicaSetMembersDefined")); //$NON-NLS-1$ } } } else { // log that there are no replica set members defined if (log != null) { log.logBasic(BaseMessages.getString(PKG, "MongoUtils.Message.Warning.NoReplicaSetMembersDefined")); //$NON-NLS-1$ } } } else { // log that the replica set collection is not available if (log != null) { log.logBasic(BaseMessages.getString(PKG, "MongoUtils.Message.Warning.ReplicaSetCollectionUnavailable")); //$NON-NLS-1$ } } } else { // log that the local database is not available!! if (log != null) { log.logBasic(BaseMessages.getString(PKG, "MongoUtils.Message.Warning.LocalDBNotAvailable")); //$NON-NLS-1$ } } } catch (Exception ex) { throw new KettleException(ex); } finally { if (mongo != null) { mongo.close(); } } return setMembers; }