Example usage for com.mongodb MongoURI getUsername

List of usage examples for com.mongodb MongoURI getUsername

Introduction

In this page you can find the example usage for com.mongodb MongoURI getUsername.

Prototype

@Nullable
public String getUsername() 

Source Link

Document

Gets the username.

Usage

From source file:com.appleframework.monitor.model.Project.java

License:Open Source License

public MongoTemplate fetchMongoTemplate() {

    try {// www .  j  ava  2 s  . co m
        Mongo mongo;
        if (MONGO_MAP.containsKey(mongoUri)) {
            mongo = MONGO_MAP.get(mongoUri);

        } else {
            mongo = new Mongo(new MongoURI(mongoUri));
            MONGO_MAP.put(mongoUri, mongo);

        }

        MongoURI uri = new MongoURI(mongoUri);
        return new MongoTemplate(new SimpleMongoDbFactory(mongo, uri.getDatabase(),
                new UserCredentials(uri.getUsername(), parseChars(uri.getPassword()))));

    } catch (Exception e) {
        logger.error("mongo db error ,uri={}", mongoUri, e);
        return null;
    }

}

From source file:com.cloudbees.gasp.model.MongoConnection.java

License:Apache License

public void connect() throws Exception {
    try {//  ww w  . j a v  a2s  .  c om
        // Connect to Mongo and Authenticate
        MongoURI mongoURI = new MongoURI(mongoURL);
        mongo = new Mongo(mongoURI);
        mongoDB = mongo.getDB(mongoURI.getDatabase());
        mongoDB.authenticate(mongoURI.getUsername(), mongoURI.getPassword());

        // Get Mongo collections and set WriteConcern
        String mongoLocations = "locations";
        locations = getMongoDB().getCollection(mongoLocations);
        mongoDB.setWriteConcern(WriteConcern.SAFE);
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
}

From source file:com.dianping.swallow.web.dao.SimMongoDbFactory.java

License:Apache License

/**
 * Creates a new {@link SimpleMongoDbFactory} instance from the given {@link MongoURI}.
 * //  w  ww .ja v  a 2 s . c  om
 * @param uri must not be {@literal null}.
 * @throws MongoException
 * @throws UnknownHostException
 * @see MongoURI
 * @deprecated since 1.7. Please use {@link #SimpleMongoDbFactory(MongoClientURI)} instead.
 */
@Deprecated
public SimMongoDbFactory(MongoURI uri) throws MongoException, UnknownHostException {
    this(new Mongo(uri), uri.getDatabase(),
            new UserCredentials(uri.getUsername(), parseChars(uri.getPassword())), true, uri.getDatabase());
}

From source file:fr.xebia.cocktail.CocktailRepository.java

License:Apache License

@Inject
public CocktailRepository(@Value("${mongodb_uri}") String mongoUri, @Value("${solr_url}") String solrUri)
        throws UnknownHostException, MalformedURLException {
    MongoURI databaseUri = new MongoURI(mongoUri);
    mongo = new Mongo(databaseUri);

    db = mongo.getDB(databaseUri.getDatabase());
    if (!Strings.isNullOrEmpty(databaseUri.getUsername())) {
        db.authenticate(databaseUri.getUsername(), databaseUri.getPassword());
    }//ww  w.j  av  a  2 s .  co  m
    cocktails = db.getCollection("cocktails");

    solrServer = new CommonsHttpSolrServer(solrUri);

}

From source file:org.icgc.dcc.release.job.imports.hadoop.MongoAdminInputFormat.java

License:Open Source License

private static void authenticateAdmin(MongoURI mongoUri, Mongo hadoopMongo) {
    DB admin = hadoopMongo.getDB(MONGO_ADMIN_DATABASE_NAME);
    boolean authenticated = admin.authenticate(mongoUri.getUsername(), mongoUri.getPassword());

    checkState(authenticated, "Could not authenticate user '%s' to database '%s' using MongoURI '%s'", //
            mongoUri.getUsername(), MONGO_ADMIN_DATABASE_NAME, mongoUri);
}

From source file:org.ingini.monogo.testbed.MongoManager.java

License:Apache License

/**
 * Tries to connect to a given running MongoDB instance at {@code uri}
 * <p>Example URIs:</p>/*from w w w  .  ja va2  s . c o m*/
 *      *) mongodb://127.0.0.1:27017 - connecting to 127.0.0.1 @ port 27017 without authenticating
 *      *) mongodb://john:doe@127.0.0.1:27017 - connecting to 127.0.0.1 @ port 27017 with username: 'john' and password: 'doe'
 *      *) mongodb://john:@127.0.0.1:27017 - connecting to 127.0.0.1 @ port 27017 with username: 'john' and empty password
 * @param uri
 * @throws IllegalStateException in case of difficulties while connecting
 */
private MongoManager(String uri) {
    MongoURI mongoURI = new MongoURI(uri);
    try {
        this.mongo = mongoURI.connect();
    } catch (UnknownHostException e) {
        logger.error("Could not connect to {} due to an exception!", uri, e);
        throw new IllegalStateException(e);
    }
    this.mongoDB = mongo.getDB(MONOGO_TESTBED_DB);
    if (mongoURI.getUsername() != null) {
        this.mongoDB.authenticate(mongoURI.getUsername(), mongoURI.getPassword());
    }
}

From source file:org.springframework.data.mongodb.core.SimpleMongoDbFactory.java

License:Apache License

/**
 * Creates a new {@link SimpleMongoDbFactory} instance from the given {@link MongoURI}.
 * /*  www . j a  v  a2s  . co m*/
 * @param uri must not be {@literal null}.
 * @throws MongoException
 * @throws UnknownHostException
 * @see MongoURI
 * @deprecated since 1.7. Please use {@link #SimpleMongoDbFactory(MongoClientURI)} instead.
 */
@Deprecated
public SimpleMongoDbFactory(MongoURI uri) throws MongoException, UnknownHostException {
    this(new Mongo(uri), uri.getDatabase(),
            new UserCredentials(uri.getUsername(), parseChars(uri.getPassword())), true, uri.getDatabase());
}

From source file:org.wrml.contrib.runtime.service.mongo.MongoService.java

License:Apache License

@Override
protected void initFromConfiguration(final ServiceConfiguration config) {

    if (config == null) {
        final ServiceException e = new ServiceException("The config cannot be null.", null, this);
        LOG.error(e.getMessage(), e);//from   w  w w .ja v  a 2s . c  om
        throw e;
    }

    final Map<String, String> settings = config.getSettings();
    String mongoUriString = DEFAULT_URI_STRING;

    if (settings != null) {
        if (settings.containsKey(MONGO_URI_SETTING_NAME)) {
            mongoUriString = settings.get(MONGO_URI_SETTING_NAME);
        }

        if (settings.containsKey(MONGO_COLLECTION_PREFIX_SETTING_NAME)) {
            _CollectionPrefix = settings.get(MONGO_COLLECTION_PREFIX_SETTING_NAME);
        }
    }

    // TODO: Look into MongoClientURI replacement
    final MongoURI mongoUri = new MongoURI(mongoUriString);
    try {
        _Mongo = mongoUri.connectDB();

        if (!_Mongo.isAuthenticated() && mongoUri.getPassword() != null) {
            _Mongo.authenticate(mongoUri.getUsername(), mongoUri.getPassword());
        }

    } catch (MongoException | UnknownHostException ex) {
        final String logMessage = "Error creating connection to Mongo: " + _Mongo;
        LOG.error(logMessage);
        throw new ServiceException(logMessage, ex, this);
    }
}