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, final MongoClientOptions.Builder builder) 

Source Link

Document

Creates a MongoURI from the given URI string, and MongoClientOptions.Builder.

Usage

From source file:io.gravitee.repository.mongodb.common.MongoFactory.java

License:Apache License

@Override
public Mongo getObject() throws Exception {
    MongoClientOptions.Builder builder = builder();

    // Trying to get the MongoClientURI if uri property is defined
    String uri = readPropertyValue(propertyPrefix + "uri");

    if (uri != null && !uri.isEmpty()) {
        // The builder can be configured with default options, which may be overridden by options specified in
        // the URI string.
        return new MongoClient(new MongoClientURI(uri, builder));
    } else {/*from w  ww  .  j a v a2  s .  c  o m*/

        String databaseName = readPropertyValue(propertyPrefix + "dbname", String.class, "gravitee");

        String username = readPropertyValue(propertyPrefix + "username");
        String password = readPropertyValue(propertyPrefix + "password");

        List<MongoCredential> credentials = null;
        if (username != null || password != null) {
            credentials = Collections.singletonList(
                    MongoCredential.createMongoCRCredential(username, databaseName, password.toCharArray()));
        }

        List<ServerAddress> seeds;
        int serversCount = getServersCount();

        if (serversCount == 0) {
            String host = readPropertyValue(propertyPrefix + "host", String.class, "localhost");
            int port = readPropertyValue(propertyPrefix + "port", int.class, 27017);
            seeds = Collections.singletonList(new ServerAddress(host, port));
        } else {
            seeds = new ArrayList<>(serversCount);
            for (int i = 0; i < serversCount; i++) {
                seeds.add(buildServerAddress(i));
            }
        }

        MongoClientOptions options = builder.build();
        if (credentials == null) {
            return new MongoClient(seeds, options);
        }

        return new MongoClient(seeds, credentials, options);
    }
}

From source file:io.mandrel.config.MongoConfiguration.java

License:Apache License

@Bean
public MongoClient mongo() throws UnknownHostException {
    return new MongoClient(new MongoClientURI(properties.getUri(), options()));
}

From source file:io.sip3.tapir.salto.configuration.MongoConfiguration.java

License:Apache License

@Bean
public MongoClientURI uri(@Value("${mongo.uri}") String uri) {
    return new MongoClientURI(uri, MongoClientOptions.builder().writeConcern(WriteConcern.UNACKNOWLEDGED));
}

From source file:io.sip3.tapir.twig.configuration.MongoConfiguration.java

License:Apache License

@Bean
public MongoClientURI uri(@Value("${mongo.uri}") String uri, CodecRegistry registry) {
    return new MongoClientURI(uri, MongoClientOptions.builder().codecRegistry(registry));
}

From source file:mongoconnect.MongoConnect.java

private void connectToMongoDB(SearchParams sParam) {
    mongoClient = new MongoClient(new MongoClientURI(sParam.getMongoDBURI(), options));
    System.out.println("------Making new connection to MongoDB at " + mongoClient);

    /*  // Connect to 4 random mongos hosts out of the full list
    MongoClientURI x = new MongoClientURI(sParam.getMassDBURI(), options);
    List<String> unmodifiableHosts = x.getHosts();
    List<String> hosts = new ArrayList<String>(unmodifiableHosts);
    Collections.shuffle(hosts);//from w  w  w .  j a  v  a2s .  co  m
    List<String> newHosts = hosts.subList(0, 4);
    List<ServerAddress> newServers = new ArrayList<>();
    for (String host:newHosts){
    newServers.add(new ServerAddress(host));
    }
            
    MongoClient newclient = new MongoClient(newServers, options.build());
    System.out.println(newclient.getAllAddress());
    */
}

From source file:mongofx.service.MongoService.java

License:Open Source License

public MongoDbConnection connect(ConnectionSettings connectionSettings) {
    StringBuilder authString = new StringBuilder();

    String user = connectionSettings.getUser();
    if (user != null && !user.isEmpty()) {
        authString.append(user);/* ww  w.  j a va2  s  . c o  m*/
        String password = connectionSettings.getPassword();
        if (password != null && !password.isEmpty()) {
            authString.append(":").append(password);
        }
        authString.append("@");
    }
    String uri = String.format("mongodb://%s%s", authString, connectionSettings.getHost());
    Builder options = MongoClientOptions.builder().serverSelectionTimeout(10000);
    MongoClient client = new MongoClient(new MongoClientURI(uri, options));
    MongoConnection mongoConnection = new MongoConnection(client);
    return new MongoDbConnection(mongoConnection, connectionSettings);
}

From source file:net.ymate.platform.persistence.mongodb.impl.MongoDataSourceAdapter.java

License:Apache License

public void initialize(IMongoClientOptionsHandler optionsHandler, MongoDataSourceCfgMeta cfgMeta)
        throws Exception {
    __cfgMeta = cfgMeta;/* w  w  w . java 2s  .c  o  m*/
    MongoClientOptions.Builder _builder = null;
    if (optionsHandler != null) {
        _builder = optionsHandler.handler(cfgMeta.getName());
    }
    if (_builder == null) {
        _builder = MongoClientOptions.builder();
    }
    if (StringUtils.isNotBlank(cfgMeta.getConnectionUrl())) {
        __mongoClient = new MongoClient(new MongoClientURI(cfgMeta.getConnectionUrl(), _builder));
    } else {
        String _username = StringUtils.trimToNull(cfgMeta.getUserName());
        String _password = StringUtils.trimToNull(cfgMeta.getPassword());
        if (_username != null && _password != null) {
            if (__cfgMeta.isPasswordEncrypted() && __cfgMeta.getPasswordClass() != null) {
                _password = __cfgMeta.getPasswordClass().newInstance().decrypt(_password);
            }
            MongoCredential _credential = MongoCredential.createCredential(cfgMeta.getUserName(),
                    cfgMeta.getDatabaseName(), _password == null ? null : _password.toCharArray());
            __mongoClient = new MongoClient(cfgMeta.getServers(), Collections.singletonList(_credential),
                    _builder.build());
        } else {
            __mongoClient = new MongoClient(cfgMeta.getServers(), _builder.build());
        }
    }
}

From source file:org.apache.jackrabbit.oak.plugins.document.util.MongoConnection.java

License:Apache License

/**
 * Constructs a new connection using the specified MongoDB connection string.
 * See also http://docs.mongodb.org/manual/reference/connection-string/
 *
 * @param uri the MongoDB URI/*  www . j a va 2 s  . com*/
 * @throws UnknownHostException
 */
public MongoConnection(String uri) throws UnknownHostException {
    MongoClientOptions.Builder builder = MongoConnection.getDefaultBuilder();
    mongoURI = new MongoClientURI(uri, builder);
    mongo = new MongoClient(mongoURI);
}

From source file:org.apache.jackrabbit.oak.plugins.document.util.MongoConnection.java

License:Apache License

/**
 * Returns {@code true} if the given {@code uri} has a write concern set.
 * @param uri the URI to check./*from  w  w  w . j a v  a  2 s .c  o m*/
 * @return {@code true} if the URI has a write concern set, {@code false}
 *      otherwise.
 */
public static boolean hasWriteConcern(@Nonnull String uri) {
    MongoClientOptions.Builder builder = MongoClientOptions.builder();
    builder.writeConcern(WC_UNKNOWN);
    WriteConcern wc = new MongoClientURI(checkNotNull(uri), builder).getOptions().getWriteConcern();
    return !WC_UNKNOWN.equals(wc);
}

From source file:org.fastmongo.odm.client.MongoClientFactory.java

License:Apache License

public MongoClient createMongoClient() {
    MongoClientOptions.Builder options = MongoClientOptions.builder();
    if (useBsonDecoder) {
        options.dbDecoderFactory(BsonDbDecoderFactory.INSTANCE);
    }// w  w w  .  j a  v  a  2s. c o m

    MongoClientURI clientUri = new MongoClientURI(this.uri, options);

    try {
        return new MongoClient(clientUri);
    } catch (UnknownHostException e) {
        throw new RuntimeException("Can't create MongoClient with uri = " + clientUri, e);
    }
}