Example usage for com.mongodb Mongo Mongo

List of usage examples for com.mongodb Mongo Mongo

Introduction

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

Prototype

Mongo(final MongoClientURI mongoURI) 

Source Link

Usage

From source file:com.ebay.jetstream.config.mongo.MongoConnection.java

License:MIT License

public MongoConnection(MongoConfiguration mongoConfiguration) {

    List<ServerAddress> hosts = new ArrayList<ServerAddress>();
    for (String host : mongoConfiguration.getHosts()) {
        try {/*from   w w  w  . j av  a 2s  . c om*/
            hosts.add(new ServerAddress(host, Integer.valueOf(mongoConfiguration.getPort())));
        } catch (UnknownHostException e) {
        }
    }

    Mongo mongo = null;
    mongo = new Mongo(hosts);
    db = mongo.getDB(mongoConfiguration.getDb());

    authenticate(mongoConfiguration);
}

From source file:com.ebay.jetstream.config.mongo.MongoConnection.java

License:MIT License

public MongoConnection(List<String> hostStrings, String port, String database) {

    List<ServerAddress> hosts = new ArrayList<ServerAddress>();
    for (String host : hostStrings) {
        try {/*from  www . ja v a 2  s . co m*/
            hosts.add(new ServerAddress(host, Integer.valueOf(port)));
        } catch (UnknownHostException e) {
        }
    }

    Mongo mongo = null;
    mongo = new Mongo(hosts);
    db = mongo.getDB(database);

}

From source file:com.flyingdonut.implementation.persistence.MongoDBConnection.java

License:Apache License

private void mongoURIInit() {
    MongoURI uri = new MongoURI(getMongoURI());
    try {/*from   ww w  .java  2  s  .  c om*/
        logger.info("Connecting to " + uri + "...");
        mongo = new Mongo(uri);
        logger.info("...success!");
        String uriDatabase = uri.getDatabase();
        if (StringUtils.hasText(uriDatabase)) {
            db = mongo.getDB(uriDatabase);
        } else {
            db = mongo.getDB(dbName);
        }
    } catch (UnknownHostException e) {
        logger.error("Could not init the database", e);
    }
}

From source file:com.flyingdonut.implementation.persistence.MongoDBConnection.java

License:Apache License

private void simpleInit() throws UnknownHostException {
    List<ServerAddress> serverAddresses = new ArrayList<ServerAddress>();
    String[] hostNames = getHosts().split(",");

    for (String host : hostNames) {
        String[] hostPort = host.split(":");
        String h = hostPort[0];//from  w w w.ja  va2s  . co  m
        Integer p = (hostPort.length == 2 ? Integer.parseInt(hostPort[1]) : null);
        ServerAddress serverAddress;
        if (p == null) {
            serverAddress = new ServerAddress(h);
        } else {
            serverAddress = new ServerAddress(h, p);
        }
        serverAddresses.add(serverAddress);
    }
    logger.info("Connecting to " + serverAddresses + "...");
    mongo = new Mongo(serverAddresses);
    logger.info("...success!");
    db = mongo.getDB(dbName);
}

From source file:com.gdn.x.ui.config.ApplicationConfiguration.java

@Bean
public Mongo mongo() {
    return new Mongo("localhost");
}

From source file:com.github.dbourdette.otto.SpringConfig.java

License:Apache License

@Bean
public Mongo mongo() throws MongoException, UnknownHostException {
    return new Mongo(getMongoServerAdresses());
}

From source file:com.github.trask.sandbox.mongodb.MongoDatastoreProvider.java

License:Apache License

public AdvancedDatastore get() {
    if (initMorphiaLogging) {
        MorphiaLoggerFactory.registerLogger(SLF4JLogrImplFactory.class);
        initMorphiaLogging = false;/*from w  w  w .j a va  2  s .c o m*/
    }
    Mongo mongo;
    try {
        mongo = new Mongo(new MongoURI(uri));
    } catch (MongoException e) {
        throw new IllegalStateException(e);
    } catch (UnknownHostException e) {
        throw new IllegalStateException(e);
    }
    Morphia morphia = new Morphia();
    AdvancedDatastore datastore = (AdvancedDatastore) morphia.createDatastore(mongo, dbName);
    datastore.ensureIndexes();
    datastore.ensureCaps();
    return datastore;
}

From source file:com.google.code.log4mongo.MongoDbAppender.java

License:Apache License

/**
 * @see org.apache.log4j.AppenderSkeleton#activateOptions()
 */// w ww  .j a v a2 s .  c  o  m
@Override
public void activateOptions() {
    try {
        // Close previous connections if reactivating
        if (mongo != null) {
            close();
        }

        List<ServerAddress> addresses = getServerAddresses(hostname, port);
        if (addresses.size() < 2) {
            mongo = new Mongo(addresses.get(0));
        } else {
            // Replication set
            mongo = new Mongo(addresses);
        }
        DB database = mongo.getDB(databaseName);

        if (userName != null && userName.trim().length() > 0) {
            if (!database.authenticate(userName, password.toCharArray())) {
                throw new RuntimeException("Unable to authenticate with MongoDB server.");
            }

            // Allow password to be GCed
            password = null;
        }

        setCollection(database.getCollection(collectionName));
        initialized = true;
    } catch (Exception e) {
        errorHandler.error("Unexpected exception while initialising MongoDbAppender.", e,
                ErrorCode.GENERIC_FAILURE);
    }
}

From source file:com.hangum.tadpole.erd.core.dnd.TableTransferDropTargetListener.java

License:Open Source License

/**
 * table?   ./* w  w  w.ja  va2  s  .com*/
 * 
 * @param strTBName
 * @return
 * @throws Exception
 */
public List<TableColumnDAO> getColumns(String strTBName) throws Exception {
    if (DBDefine.getDBDefine(userDB.getTypes()) != DBDefine.MONGODB_DEFAULT) {
        SqlMapClient sqlClient = TadpoleSQLManager.getInstance(userDB);

        Map<String, String> param = new HashMap<String, String>();
        param.put("db", userDB.getDb());
        param.put("table", strTBName);

        return sqlClient.queryForList("tableColumnList", param);
    } else if (DBDefine.getDBDefine(userDB.getTypes()) == DBDefine.MONGODB_DEFAULT) {

        Mongo mongo = new Mongo(new DBAddress(userDB.getUrl()));
        com.mongodb.DB mongoDB = mongo.getDB(userDB.getDb());
        DBCollection coll = mongoDB.getCollection(strTBName);

        return MongoDBTableColumn.tableColumnInfo(coll.getIndexInfo(), coll.findOne());
    }

    return null;
}

From source file:com.hangum.tadpole.erd.core.utils.TadpoleModelUtils.java

License:Open Source License

/**
 * table  .// w  ww  . j  a  va2s. c  om
 */
public List<TableDAO> getTables() throws Exception {
    List<TableDAO> showTables = new ArrayList<TableDAO>();

    if (DBDefine.getDBDefine(userDB.getTypes()) != DBDefine.MONGODB_DEFAULT) {
        SqlMapClient sqlClient = TadpoleSQLManager.getInstance(userDB);
        return sqlClient.queryForList("tableList", userDB.getDb()); //$NON-NLS-1$

    } else if (DBDefine.getDBDefine(userDB.getTypes()) == DBDefine.MONGODB_DEFAULT) {
        Mongo mongo = new Mongo(new DBAddress(userDB.getUrl()));
        com.mongodb.DB mongoDB = mongo.getDB(userDB.getDb());

        for (String col : mongoDB.getCollectionNames()) {
            TableDAO dao = new TableDAO();
            dao.setName(col);

            showTables.add(dao);
        }

        return showTables;
    }

    return showTables;
}