Example usage for com.mongodb MongoClientURI MongoClientURI

List of usage examples for com.mongodb MongoClientURI MongoClientURI

Introduction

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

Prototype

public MongoClientURI(final String uri) 

Source Link

Document

Creates a MongoURI from the given string.

Usage

From source file:org.apache.storm.mongodb.common.MongoDBClient.java

License:Apache License

public MongoDBClient(String url, String collectionName) {
    //Creates a MongoURI from the given string.
    MongoClientURI uri = new MongoClientURI(url);
    //Creates a MongoClient described by a URI.
    this.client = new MongoClient(uri);
    //Gets a Database.
    MongoDatabase db = client.getDatabase(uri.getDatabase());
    //Gets a collection.
    this.collection = db.getCollection(collectionName);
}

From source file:org.apereo.lap.services.storage.mongo.MongoMultiTenantConfiguration.java

License:Educational Community License

@Override
@Bean/* ww w .  j  a  v a  2s.  c om*/
public Mongo mongo() throws Exception {
    logger.warn("Mongo Db URI is set to: {}", dbUri);
    return new MongoClient(new MongoClientURI(dbUri));
}

From source file:org.auraframework.test.perf.PerfResultsUtil.java

License:Apache License

private static MongoClient getMongoClient() {
    if (MONGO_CLIENT == null) {
        try {//from   ww w  . j  av  a2 s. c  om
            LOG.info("Trying to connect to MongoDB: " + MONGO_URI);
            MongoClientURI uri = new MongoClientURI(MONGO_URI);
            MONGO_CLIENT = new MongoClient(uri);
        } catch (Exception e) {
            LOG.info("Not able to connect to MongoDB");
            return null;
        }
    }
    return MONGO_CLIENT;
}

From source file:org.auraframework.test.perf.util.PerfResultsUtil.java

License:Apache License

private static MongoClient getMongoClient(String dbURI) {
    if (MONGO_CLIENT == null) {
        try {/*  ww w.  j  av a  2  s. c o m*/
            LOG.info("Trying to connect to DB: " + dbURI);
            MongoClientURI uri = new MongoClientURI(dbURI);
            MONGO_CLIENT = new MongoClient(uri);
        } catch (Exception e) {
            LOG.info("Not able to connect to DB");
            return null;
        }
    }
    return MONGO_CLIENT;
}

From source file:org.aw20.mongoworkbench.eclipse.Activator.java

License:Open Source License

public MongoClient getMongoClient(String sName) throws UnknownHostException {
    Map<String, Object> props = getServerMap(sName);
    if (props == null)
        return null;

    if (props.containsKey("username") && props.get("username").toString().length() > 0
            && props.containsKey("password") && props.get("password").toString().length() > 0) {
        StringBuilder sb = new StringBuilder(32);
        sb.append("mongodb://").append(props.get("username")).append(":").append(props.get("password"))
                .append("@").append(props.get("host")).append(":")
                .append(StringUtil.toInteger(props.get("port"), 27017));

        if (props.containsKey("database") && props.get("database").toString().length() > 0)
            sb.append("/").append(props.get("database"));

        return new MongoClient(new MongoClientURI(sb.toString()));
    } else/*from w w w. j  av a 2  s .  c  om*/
        return new MongoClient((String) props.get("host"), StringUtil.toInteger(props.get("port"), 27017));
}

From source file:org.basex.modules.MongoDB.java

License:BSD License

/**
 * Mongodb connection with options.//from   w ww  .  j  a  va2 s.  com
 * @param url
 * @param options
 * @return
 * @throws QueryException
 */
public Str connection(final Str url, final Map options) throws QueryException {
    MongoClientURI uri = new MongoClientURI(url.toJava());
    String handler = "mongoClient" + mongoClients.size();
    try {
        MongoClient mongoClient = new MongoClient(uri);
        mongoClients.put(handler, mongoClient);
        return mongoConnect(handler, uri.getDatabase(), uri.getUsername(), uri.getPassword(), options);
    } catch (final MongoException ex) {
        throw MongoDBErrors.mongoExceptionError(ex);
    } catch (UnknownHostException ex) {
        throw MongoDBErrors.generalExceptionError(ex);
    }
}

From source file:org.basex.modules.nosql.MongoDB.java

License:BSD License

/**
 * Mongodb connection with options./*from   w ww.ja  va  2 s .  co m*/
 * @param url mongodb url like: "mongodb://127.0.0.1:27017/basex"
 * @param options nosql options
 * @return Str
 * @throws QueryException query exception
 */
public Str connect(final Str url, final Map options) throws QueryException {
    MongoClientURI uri = new MongoClientURI(url.toJava());
    String handler = "mongoClient" + mongoClients.size();
    try {
        MongoClient mongoClient = new MongoClient(uri);
        mongoClients.put(handler, mongoClient);
        return mongoConnect(handler, uri.getDatabase(), uri.getUsername(), uri.getPassword(), options);
    } catch (final MongoException ex) {
        throw MongoDBErrors.mongoExceptionError(ex);
    } catch (UnknownHostException ex) {
        throw MongoDBErrors.generalExceptionError(ex);
    }
}

From source file:org.codinjutsu.tools.nosql.mongo.logic.MongoClient.java

License:Apache License

private com.mongodb.MongoClient createMongoClient(ServerConfiguration configuration)
        throws UnknownHostException {
    String serverUrl = configuration.getServerUrl();
    if (StringUtils.isEmpty(serverUrl)) {
        throw new ConfigurationException("server host is not set");
    }/*from w  ww  .j  a  va2s .com*/

    MongoClientURIBuilder uriBuilder = MongoClientURIBuilder.builder();
    uriBuilder.setServerAddresses(serverUrl);
    AuthenticationSettings authenticationSettings = configuration.getAuthenticationSettings();
    MongoExtraSettings mongoExtraSettings = new MongoExtraSettings(authenticationSettings.getExtras());
    if (StringUtils.isNotEmpty(authenticationSettings.getUsername())) {
        uriBuilder.setCredential(authenticationSettings.getUsername(), authenticationSettings.getPassword(),
                mongoExtraSettings.getAuthenticationDatabase());
    }

    if (mongoExtraSettings.getAuthenticationMechanism() != null) {
        uriBuilder.setAuthenticationMecanism(mongoExtraSettings.getAuthenticationMechanism());
    }

    if (mongoExtraSettings.isSsl()) {
        uriBuilder.sslEnabled();
    }

    return new com.mongodb.MongoClient(new MongoClientURI(uriBuilder.build()));
}

From source file:org.codinjutsu.tools.nosql.mongo.logic.SingleMongoClient.java

License:Apache License

private MongoClient createMongoClient(ServerConfiguration configuration) throws UnknownHostException {
    String serverUrl = configuration.getServerUrl();
    if (StringUtils.isEmpty(serverUrl)) {
        throw new ConfigurationException("server host is not set");
    }/* w w w.j  a va  2s.com*/

    MongoClientURIBuilder uriBuilder = MongoClientURIBuilder.builder();
    uriBuilder.setServerAddresses(serverUrl);
    AuthenticationSettings authenticationSettings = configuration.getAuthenticationSettings();
    MongoExtraSettings mongoExtraSettings = new MongoExtraSettings(authenticationSettings.getExtras());
    if (StringUtils.isNotEmpty(authenticationSettings.getUsername())) {
        uriBuilder.setCredential(authenticationSettings.getUsername(), authenticationSettings.getPassword(),
                mongoExtraSettings.getAuthenticationDatabase());
    }

    if (mongoExtraSettings.getAuthenticationMechanism() != null) {
        uriBuilder.setAuthenticationMecanism(mongoExtraSettings.getAuthenticationMechanism());
    }

    if (mongoExtraSettings.isSsl()) {
        uriBuilder.sslEnabled();
    }

    return new MongoClient(new MongoClientURI(uriBuilder.build()));
}

From source file:org.craftercms.commons.mongo.MongoClientFromUriFactoryBean.java

License:Open Source License

@Override
protected MongoClient createInstance() throws Exception {
    final MongoClientURI client = new MongoClientURI(uri);
    logger.debug("Connecting to :" + client.getDatabase());
    return new MongoClient(client);
}