Example usage for com.mongodb MongoCredential createCredential

List of usage examples for com.mongodb MongoCredential createCredential

Introduction

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

Prototype

public static MongoCredential createCredential(final String userName, final String database,
        final char[] password) 

Source Link

Document

Creates a MongoCredential instance with an unspecified mechanism.

Usage

From source file:com.enitalk.configs.MongoConfig.java

@Override
@Bean/*from   w w  w  . ja  v a  2  s .c  o m*/
public Mongo mongo() throws Exception {
    return new MongoClient(singletonList(new ServerAddress(env.getProperty("mongo.host"), 27017)),
            singletonList(MongoCredential.createCredential(env.getProperty("mongo.user"), getDatabaseName(),
                    env.getProperty("mongo.pass").toCharArray())));
}

From source file:com.epam.dlab.billing.azure.MongoDbBillingClient.java

License:Apache License

public MongoDbBillingClient(String host, int port, String databaseName, String username, String password) {
    this.client = new MongoClient(new ServerAddress(host, port), Lists
            .newArrayList(MongoCredential.createCredential(username, databaseName, password.toCharArray())));

    this.database = client.getDatabase(databaseName);
}

From source file:com.epam.dlab.mongo.MongoDbConnection.java

License:Apache License

/**
 * Instantiate the helper for Mongo database adapter.
 *
 * @param host         the host name./*from   w  w w  . j ava2s .com*/
 * @param port         the port.
 * @param databaseName the name of database.
 * @param username     the name of user.
 * @param password     the password.
 * @throws AdapterException
 */
public MongoDbConnection(String host, int port, String databaseName, String username, String password)
        throws AdapterException {
    try {
        client = new MongoClient(new ServerAddress(host, port), Collections.singletonList(
                MongoCredential.createCredential(username, databaseName, password.toCharArray())));
        database = client.getDatabase(databaseName).withWriteConcern(WriteConcern.ACKNOWLEDGED);
    } catch (Exception e) {
        throw new AdapterException(
                "Cannot create connection to database " + databaseName + ". " + e.getLocalizedMessage(), e);
    }
}

From source file:com.epam.dlab.mongo.MongoServiceFactory.java

License:Apache License

public MongoService build(Environment environment) {
    MongoClient client = new MongoClient(new ServerAddress(host, port), Collections
            .singletonList(MongoCredential.createCredential(username, database, password.toCharArray())));
    environment.lifecycle().manage(new Managed() {
        @Override//w  w  w  .ja  v  a  2 s.  com
        public void start() {
        }

        @Override
        public void stop() {
            client.close();
        }
    });
    return new MongoService(client, database);
}

From source file:com.epam.ta.reportportal.config.MongodbConfiguration.java

License:Open Source License

@Bean
@Profile("!unittest")
MongoClient mongo() throws UnknownHostException {
    MongoClientOptions.Builder mongoClientBuilder = MongoClientOptions.builder();
    mongoClientBuilder.connectionsPerHost(mongoProperties.getConnectionsPerHost())
            .threadsAllowedToBlockForConnectionMultiplier(
                    mongoProperties.getThreadsAllowedToBlockForConnectionMultiplier())
            .connectTimeout(mongoProperties.getConnectTimeout())
            .socketTimeout(mongoProperties.getSocketTimeout()).maxWaitTime(mongoProperties.getMaxWaitTime())
            .socketKeepAlive(mongoProperties.getSocketKeepAlive());

    List<MongoCredential> credentials = Collections.emptyList();
    if (!Strings.isNullOrEmpty(mongoProperties.getUser())
            && !Strings.isNullOrEmpty(mongoProperties.getPassword())) {
        credentials = Collections.singletonList(MongoCredential.createCredential(mongoProperties.getUser(),
                mongoProperties.getDbName(), mongoProperties.getPassword().toCharArray()));
    }//from ww w .  j  a  va  2  s . c  o m

    return new MongoClient(new ServerAddress(mongoProperties.getHost(), mongoProperties.getPort()), credentials,
            mongoClientBuilder.build());
}

From source file:com.erudika.para.persistence.MongoDBUtils.java

License:Apache License

/**
 * Returns a client instance for MongoDB
 * @return a client that talks to MongoDB
 *//*w ww  .  j  a va  2  s .c  om*/
public static MongoDatabase getClient() {
    if (mongodb != null) {
        return mongodb;
    }

    logger.info("MongoDB host: " + DBHOST + ":" + DBPORT + ", database: " + DBNAME);
    ServerAddress s = new ServerAddress(DBHOST, DBPORT);
    if (!StringUtils.isBlank(DBUSER) && !StringUtils.isBlank(DBPASS)) {
        MongoCredential credential = MongoCredential.createCredential(DBUSER, DBNAME, DBPASS.toCharArray());
        mongodbClient = new MongoClient(s, Arrays.asList(credential));
    } else {
        mongodbClient = new MongoClient(s);
    }

    mongodb = mongodbClient.getDatabase(DBNAME);

    if (!existsTable(Config.APP_NAME_NS)) {
        createTable(Config.APP_NAME_NS);
    }

    // We don't have access to Para.addDestroyListener() here.
    // Users will be responsible for calling shutDownClient().

    return mongodb;
}

From source file:com.focusit.log4jmongo.appender.SimpleMongoDbAppender.java

License:Apache License

protected MongoClient getMongo(final List<ServerAddress> addresses) {

    List<MongoCredential> credential = null;

    if (userName != null && password != null && sourceDb != null && userName.trim().length() > 0
            && password.trim().length() > 0 && sourceDb.trim().length() > 0) {
        credential = new ArrayList<MongoCredential>();
        credential.add(MongoCredential.createCredential(userName, sourceDb, password.toCharArray()));
    }//  ww w.  j ava2 s.c o m

    if (credential != null) {
        return new MongoClient(addresses, credential);
    }
    return new MongoClient(addresses);
}

From source file:com.garyclayburg.persistence.config.LocalMongoClientConfig.java

License:Open Source License

@Override
@Bean//w  ww  .j av  a  2  s .c o m
public Mongo mongo() throws Exception {
    log.info("configuring local mongo bean: " + mongoHost + ":" + mongoPort);
    MongoClient mongoClient = null;
    try {
        log.debug("mongoHost: " + mongoHost);
        log.debug("mongoPort: " + mongoPort);
        log.debug("mongoUser: " + mongoUser);
        log.debug("mongoDatabase: " + mongoDatabase);

        if (mongoUser != null) {
            log.debug("using user/password authentication to mongodb");
            MongoCredential credential = MongoCredential.createCredential(mongoUser, getDatabaseName(),
                    mongoPassword.toCharArray());
            List<MongoCredential> credList = new ArrayList<MongoCredential>();
            credList.add(credential);
            mongoClient = new MongoClient(new ServerAddress(mongoHost, mongoPort), credList);
        } else {

            log.debug("attempting no authentication to mongodb");
            mongoClient = new MongoClient(mongoHost, mongoPort);

        }

    } catch (UnknownHostException e) {
        log.warn("kaboom", e);
    }
    return mongoClient;
}

From source file:com.github.danzx.zekke.mongo.config.MongoDbSettings.java

License:Apache License

private MongoDbSettings(Builder builder) {
    this.database = builder.database;
    address = new ServerAddress(builder.host, builder.port);
    if (!isNullOrBlank(builder.user)) {
        credential = builder.password == null
                ? MongoCredential.createCredential(builder.user, builder.database, Strings.EMPTY.toCharArray())
                : MongoCredential.createCredential(builder.user, builder.database,
                        builder.password.toCharArray());
    } else//from  w ww  . j  ava 2 s .  c  o  m
        credential = null;
}

From source file:com.globocom.grou.configurations.MongoConfiguration.java

License:Open Source License

@Override
public Mongo mongo() throws Exception {
    if ("".equals(MONGO_URI.getValue())) {
        LOGGER.info("MONGO_HOST: {}, MONGO_PORT: {}, MONGO_DB: {}", MONGO_HOST.getValue(),
                MONGO_PORT.getValue(), MONGO_DB.getValue());
        final List<MongoCredential> credentialsList = "".equals(MONGO_USER.getValue())
                || "".equals(MONGO_PASS.getValue()) ? Collections.emptyList()
                        : singletonList(MongoCredential.createCredential(MONGO_USER.getValue(),
                                MONGO_DB.getValue(), MONGO_PASS.getValue().toCharArray()));
        return new MongoClient(
                singletonList(//from ww  w .j  a v  a  2  s  .c om
                        new ServerAddress(MONGO_HOST.getValue(), Integer.parseInt(MONGO_PORT.getValue()))),
                credentialsList);
    } else {
        LOGGER.info("MONGO_URI: {}", MONGO_URI.getValue().replaceAll("([/,]).*@", "$1xxxx:xxxx@"));
        return new MongoClient(getMongoClientUri());
    }
}