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:com.netflix.config.sources.MongoDBConfigurationSource.java

License:Apache License

public MongoDBConfigurationSource(String mongoURI, String databaseName, String collectionName) {
    this.databaseName = databaseName;
    this.collectionName = collectionName;

    mongoClient = new MongoClient(new MongoClientURI(mongoURI));
}

From source file:com.novemberain.quartz.mongodb.MongoDBJobStore.java

License:Open Source License

private Mongo connectToMongoDB(final String mongoUriAsString) throws SchedulerConfigException {
    try {/* ww w  .ja va 2 s  .  c o m*/
        return new MongoClient(new MongoClientURI(mongoUriAsString));
    } catch (final UnknownHostException e) {
        throw new SchedulerConfigException("Could not connect to MongoDB", e);
    } catch (final MongoException e) {
        throw new SchedulerConfigException("MongoDB driver thrown an exception", e);
    }
}

From source file:com.ovrhere.android.morseflash.ui.fragments.MainFragment.java

License:Apache License

private void insertIntoDB() {
    MongoClientURI URI = new MongoClientURI("mongodb://zaid:zaid@ds047802.mongolab.com:47802/aadhar");
    MongoClient client = null;//from ww  w .  ja  va2 s . com
    try {
        client = new MongoClient(URI);
        DB db = client.getDB("aadhar");
        DBCollection collection = db.getCollection("Sender");

        BasicDBObject document = new BasicDBObject();
        document.put("database", "aadhar");
        document.put("table", "Sender");

        BasicDBObject documentDetail = new BasicDBObject();
        documentDetail.put("Uid", "103");
        documentDetail.put("Amount", "1010");
        documentDetail.put("Pin", "" + UniqueCode);
        documentDetail.put("Status", "send");

        document.put("detail", documentDetail);

        collection.insert(document);

    } catch (UnknownHostException e) {
        e.printStackTrace();
    }

}

From source file:com.percona.LinkBench.MongoDBTestConfig.java

License:Apache License

static DB createConnection(String testDB) throws InstantiationException, IllegalAccessException,
        ClassNotFoundException, MongoClientException, UnknownHostException {

    StringBuilder mongoUri = new StringBuilder("mongodb://");
    if (user != null && !"".equals(user.trim()) && pass != null && !"".equals(pass.trim())) {
        mongoUri.append(user + ":" + pass + "@");
    }/*from   w w  w  .  j  a v  a  2 s .c o m*/
    if (host != null && !"".equals(host.trim()))
        mongoUri.append(host);
    if (port > 0)
        mongoUri.append(":" + port);
    mongoUri.append("/");
    if (testDB != null && !"".equals(testDB.trim()))
        mongoUri.append(testDB);

    MongoClient mongoClient = new MongoClient(new MongoClientURI(mongoUri.toString()));

    return (mongoClient.getDB(testDB));

}

From source file:com.petpet.c3po.dao.mongo.MongoPersistenceLayer.java

License:Apache License

/**
 * Establishes the connection to mongo database. This method relies on the
 * following configs being passed as arguments: <br>
 * db.name <br>/*from  w w w  .ja  v a 2s  .co m*/
 * db.host <br>
 * db.port <br>
 * <p>
 * Once the connection is open, the method will ensure that the mongo
 * collections and indexes are created.
 *
 * @throws C3POPersistenceException if something goes wrong. Make sure to check the cause of the
 *                                  exception.
 */
@Override
public void establishConnection(Map<String, String> config) throws C3POPersistenceException {
    //this.close();

    if (config == null || config.keySet().isEmpty()) {
        throw new C3POPersistenceException("Cannot establish connection. No configuration provided");
    }

    try {
        String uri = config.get(CNF_DB_URI);
        String name = config.get(CNF_DB_NAME);
        if (uri != null && uri.length() > 0) {
            MongoClientURI mongoClientURI = new MongoClientURI(uri);
            mongo = new MongoClient(mongoClientURI);
            this.db = this.mongo.getDB(name);
        }

        else {
            String host = config.get(CNF_DB_HOST);
            int port = Integer.parseInt(config.get(CNF_DB_PORT));

            this.mongo = new Mongo(host, port);
            this.db = this.mongo.getDB(name);
        }
        DBObject uid = new BasicDBObject("uid", 1);
        DBObject key = new BasicDBObject("key", 1);
        DBObject unique = new BasicDBObject("unique", true);

        this.db.getCollection(TBL_ELEMENTS).createIndex(uid);
        this.db.getCollection(TBL_PROEPRTIES).createIndex(key);

        this.collections.put(Source.class.getName(), this.db.getCollection(TBL_SOURCES));
        this.collections.put(Element.class.getName(), this.db.getCollection(TBL_ELEMENTS));
        this.collections.put(Property.class.getName(), this.db.getCollection(TBL_PROEPRTIES));
        this.collections.put(ActionLog.class.getName(), this.db.getCollection(TBL_ACTIONLOGS));

        if (this.dbCache == null) {
            DBCache cache = new DBCache();
            cache.setPersistence(this);
            this.dbCache = cache;
        }

        this.connected = true;

    } catch (NumberFormatException e) {

        LOG.error("Cannot parse port information! Error: {}", e.getMessage());
        throw new C3POPersistenceException("Could not parse port information", e);

    } catch (MongoException e) {

        LOG.error("The mongo driver threw an exception! Error: {}", e.getMessage());
        throw new C3POPersistenceException("A mongo specific error occurred", e);

    }

}

From source file:com.px100systems.data.plugin.storage.mongo.MongoDatabaseStorage.java

License:Open Source License

@Override
public void afterPropertiesSet() {
    mongoClient = new MongoClient(new MongoClientURI(connectionUrl));
}

From source file:com.ronoel.decorateste.datastore.MorphiaDataStoreFactory.java

@Produces
@MongoDecoraDatabaseUser/*from w  w w.  java2 s.com*/
public Datastore getDataStore() {

    if (this.datastore == null) {
        final Morphia morphia = new Morphia();

        morphia.map(UserEntity.class);

        this.datastore = morphia.createDatastore(
                new MongoClient(new MongoClientURI(this.config.getProperty("database.URI"))),
                this.config.getProperty("database.name"));
        datastore.ensureIndexes();
    }

    return this.datastore;
}

From source file:com.smbtec.xo.mongodb.api.MongoDbXOProvider.java

License:Apache License

@Override
public Datastore<MongoDbDatastoreSession, DocumentMetadata, String, RelationshipMetadata, String> createDatastore(
        XOUnit xoUnit) {/* w w  w.  j  a  va  2s  .  co m*/
    if (xoUnit == null) {
        throw new IllegalArgumentException("XOUnit must not be null");
    }
    final URI uri = xoUnit.getUri();
    if (uri == null) {
        throw new XOException("No URI is specified for the store.");
    }

    try {
        if ("fongodb".equals(uri.getScheme())) {
            // in memory only for testing purposes
            Fongo fongo = new Fongo("test");
            return new MongoDbDatastore(fongo.getDB("xo"));
        } else {
            MongoClient client = new MongoClient(new MongoClientURI(uri.toString()));
            return new MongoDbDatastore(client.getDB("xo"));
        }
    } catch (UnknownHostException e) {
        throw new XOException("", e);
    }
}

From source file:com.snapit.solutions.slantfree.db.DatabaseConnector.java

private static MongoClient getMongoClient() {
    // To connect to mongodb server
    MongoClientURI connectionString = new MongoClientURI(DB_CONNECTION_URI);
    MongoClient mongoClient = new MongoClient(connectionString);
    return mongoClient;
}

From source file:com.sqm.dashboard.dao.impl.UserDaoImpl.java

License:Apache License

public boolean addUser(String firstname) throws UnknownHostException {

    // String passwordHash = makePasswordHash(password,
    // Integer.toString(random.nextInt()));

    BasicDBObject user = new BasicDBObject();
    System.out.println("In USerDAO: " + firstname);
    user.append("firstname", firstname);

    try {//from w ww. j  a v  a 2 s. c  o m
        final DBCollection usersCollection;

        final MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost"));
        final DB blogDatabase = mongoClient.getDB("blog");
        usersCollection = blogDatabase.getCollection("user");

        usersCollection.insert(user);
        return true;
    } catch (MongoException.DuplicateKey e) {
        System.out.println("Username already in use: " + firstname);
        return false;
    }
}