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.craftercms.commons.mongo.MongoScriptRunner.java

License:Open Source License

private List<String> getCommands(final Path scriptPath) throws MongoDataException {
    List<String> commandList = new ArrayList<>();
    if (SystemUtils.IS_OS_WINDOWS) {
        commandList.add("CMD");
        commandList.add("/C");
    }//from www.j a va 2 s.  c  o m
    if (StringUtils.isBlank(mongoClientBin)) {
        throw new MongoDataException("Unable to run scripts, mongo client bin path is not set ");
    }
    String pwd = null;
    String authSource = null;
    String user = null;
    MongoClientURI uri = new MongoClientURI(connectionStr);
    if (uri.getCredentials() != null) {
        authSource = uri.getCredentials().getSource();
        user = uri.getCredentials().getUserName();
        if (uri.getCredentials().getPassword() != null) {
            pwd = new String(uri.getCredentials().getPassword());
        }
    }
    String replicaSetName = "";
    if (uri.getHosts().size() > 1) {
        replicaSetName = uri.getOptions().getRequiredReplicaSetName() + "/";
    }
    final String host = StringUtils.trim(replicaSetName + StringUtils.join(uri.getHosts(), ","));
    commandList.add(mongoClientBin);
    commandList.add("--host");
    commandList.add(host);
    commandList.add(uri.getDatabase());
    if (StringUtils.isNotBlank(user) && StringUtils.isNotBlank(pwd) && StringUtils.isNotBlank(authSource)) {
        commandList.add("-u");
        commandList.add(user);
        commandList.add("-p");
        commandList.add(pwd);
        commandList.add("--authenticationDatabase");
        commandList.add(authSource);
    }
    commandList.add(scriptPath.toAbsolutePath().toString());
    return commandList;
}

From source file:org.culturegraph.mf.mongodb.common.SimpleMongoDBConnection.java

License:Apache License

/**
 * @param uri//from  w  w w  .j  a  v a 2 s  .  c o m
 *            monogdb://user:pass@host:port/database.collection?options...
 * @see MongoClientURI
 */
public SimpleMongoDBConnection(final String uri) throws UnknownHostException {
    final MongoClientURI mongoClientUri = new MongoClientURI(uri);
    mongoClient = new MongoClient(mongoClientUri);
    final DB db = mongoClient.getDB(mongoClientUri.getDatabase());
    dbCollection = db.getCollection(mongoClientUri.getCollection());
}

From source file:org.dcache.sf.AbstractSfNearlineStorage.java

License:Open Source License

@Override
public synchronized void configure(Map<String, String> properties) throws IllegalArgumentException {
    checkState(!hasTasks(), "The nearline storage is busy and cannot be reconfigured.");

    mongoUrl = properties.get("mongoUrl");
    checkArgument(mongoUrl != null, "mongoUrl attribute is required");

    client = MongoClientProvider.get(new MongoClientURI(mongoUrl));
}

From source file:org.extendedmind.logback.MongoDBAppender.java

License:Apache License

private void connect() throws UnknownHostException {
    MongoClient client;/* w  w  w  . j a  v a 2  s  .  c o  m*/
    if (this.uri == null) {
        client = new MongoClient(host, port);
    } else {
        client = new MongoClient(new MongoClientURI(this.uri));
    }
    mongoDb = client.getDB(db == null ? "log" : db);
}

From source file:org.flywaydb.core.EmbeddedMongoDb.java

License:Apache License

@Before
public void initialize() throws IOException {
    mongodExe = mongodStarter.prepare(new MongodConfigBuilder().version(Version.Main.V3_2)
            .net(new Net(mongodPort, Network.localhostIsIPv6())).build());
    mongod = mongodExe.start();//from   w  ww.  jav  a2 s  .co  m
    mongoClient = new MongoClient(new MongoClientURI(mongoUri));
}

From source file:org.flywaydb.core.MongoFlyway.java

License:Apache License

/**
 * Sets the MongoClient to use. Must have the necessary privileges to execute ddl.
 *
  * To use a custom ClassLoader, setClassLoader() must be called prior to calling this method.
  *//from  ww w  .  j  a  v  a2  s  .co m
 * @param uri   MongoDB connection URI used to connect to a MongoDB database server.
 */
private void setMongoClient(String uri) {
    MongoClientURI mongoUri = new MongoClientURI(uri);
    this.databaseName = mongoUri.getDatabase();
    this.client = new MongoClient(mongoUri);
    createdMongoClient = true;
}

From source file:org.geogit.storage.mongo.MongoConnectionManager.java

License:Open Source License

@Override
protected MongoClient connect(MongoAddress address) {
    try {/*from ww  w .  ja v a  2  s.  com*/
        MongoClientURI uri = new MongoClientURI(address.getUri());
        return new MongoClient(uri);
    } catch (UnknownHostException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.grails.datastore.gorm.mongo.bean.factory.MongoClientFactoryBean.java

License:Apache License

public void afterPropertiesSet() throws UnknownHostException {
    // apply defaults - convenient when used to configure for tests
    // in an application context
    if (mongo != null) {
        return;/*from   w ww.  j av a2s  . c  om*/
    }

    ServerAddress defaultOptions = new ServerAddress();
    List<MongoCredential> credentials = new ArrayList<MongoCredential>();
    if (mongoOptions == null) {
        MongoClientOptions.Builder builder = MongoClientOptions.builder();
        builder.codecRegistry(CodecRegistries.fromRegistries(codecRegistries));
        mongoOptions = builder.build();
    }
    // If username/pw exists and we are not authenticated, authenticate now
    if (username != null && password != null) {
        credentials.add(MongoCredential.createCredential(username, database, password.toCharArray()));
    }

    if (replicaPair != null) {
        if (replicaPair.size() < 2) {
            throw new DatastoreConfigurationException("A replica pair must have two server entries");
        }
        mongo = new MongoClient(replicaPair, credentials, mongoOptions);
    } else if (replicaSetSeeds != null) {
        mongo = new MongoClient(replicaSetSeeds, credentials, mongoOptions);
    } else if (clientURI != null) {
        mongo = new MongoClient(clientURI);
    } else if (connectionString != null) {
        mongo = new MongoClient(new MongoClientURI(connectionString));
    } else {
        String mongoHost = host != null ? host : defaultOptions.getHost();
        if (port != null) {
            mongo = new MongoClient(new ServerAddress(mongoHost, port), credentials, mongoOptions);
        } else {
            mongo = new MongoClient(new ServerAddress(host), credentials, mongoOptions);
        }
    }

}

From source file:org.graylog.plugins.metrics.mongodb.jadconfig.MongoClientURIConverter.java

License:Open Source License

@Override
public MongoClientURI convertFrom(String value) {
    if (value == null) {
        throw new ParameterException("Couldn't convert value \"null\" to a MongoDB connection string.");
    }/*  w  ww  .j  a  v a 2  s .com*/

    try {
        return new MongoClientURI(value);
    } catch (Exception e) {
        throw new ParameterException(e.getMessage(), e);
    }
}

From source file:org.helm.rest.MongoDB.java

public MongoDB(String connstring, String dbname) {
    try {/* ww w. ja  va2 s . com*/
        MongoClientURI connectionString = new MongoClientURI(connstring);
        client = new MongoClient(connectionString);
        db = client.getDatabase(dbname);
    } catch (Exception e) {
        error = e;
    }
}