Example usage for com.mongodb MongoCredential getSource

List of usage examples for com.mongodb MongoCredential getSource

Introduction

In this page you can find the example usage for com.mongodb MongoCredential getSource.

Prototype

public String getSource() 

Source Link

Document

Gets the source of the user name, typically the name of the database where the user is defined.

Usage

From source file:com.meltmedia.dropwizard.mongo.MongoBundle.java

License:Apache License

MongoClient buildClient(MongoConfiguration configuration) {
    try {// www  . j a  v  a2s . com
        // 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  w w . j a v  a 2 s  . co  m
    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();
}