Example usage for com.mongodb DBAddress DBAddress

List of usage examples for com.mongodb DBAddress DBAddress

Introduction

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

Prototype

public DBAddress(final InetAddress inetAddress, final int port, final String databaseName) 

Source Link

Usage

From source file:cascading.mongodb.MongoWrapper.java

License:Apache License

static private Mongo makeInstance(String hostname, int port, String database) throws UnknownHostException {
    return new Mongo(new DBAddress(hostname, port, database));
}

From source file:org.apache.sling.mongodb.impl.MongoDBResourceProviderFactory.java

License:Apache License

@Activate
protected void activate(final Map<String, Object> props) throws Exception {
    final String[] roots = PropertiesUtil.toStringArray(props.get(ResourceProvider.ROOTS));
    if (roots == null || roots.length == 0) {
        throw new Exception("Roots configuration is missing.");
    }//ww  w . jav  a2  s  .co  m
    if (roots.length > 1) {
        throw new Exception("Only a single root should be configured.");
    }
    if (roots[0] == null || roots[0].trim().length() == 0) {
        throw new Exception("Roots configuration is missing.");
    }
    final String host = PropertiesUtil.toString(props.get(PROP_HOST), DEFAULT_HOST);
    final int port = PropertiesUtil.toInteger(props.get(PROP_PORT), DEFAULT_PORT);
    final String db = PropertiesUtil.toString(props.get(PROP_DB), DEFAULT_DB);
    logger.info("Starting MongoDB resource provider with host={}, port={}, db={}",
            new Object[] { host, port, db });
    final DBAddress address = new DBAddress(host, port, db);
    final MongoOptions options = new MongoOptions();

    options.connectionsPerHost = PropertiesUtil.toInteger(props.get(PROP_NUM_CONNECTIONS),
            DEFAULT_NUMCONNECTIONS);
    options.threadsAllowedToBlockForConnectionMultiplier = PropertiesUtil
            .toInteger(props.get(PROP_THREAD_MULTIPLIER), DEFAULT_THREAD_MULTIPLIER);
    final Mongo m = new Mongo(address, options);

    final DB database = m.getDB(db);
    logger.info("Connected to database {}", database);

    this.context = new MongoDBContext(database, roots[0],
            PropertiesUtil.toStringArray(props.get(PROP_FILTER_COLLECTIONS)), this.eventAdmin);
}

From source file:org.apache.streams.mongo.MongoPersistReader.java

License:Apache License

private synchronized void connectToMongo() {

    try {/*from ww w .  ja v a 2s .c  om*/
        dbaddress = new DBAddress(config.getHost(), config.getPort().intValue(), config.getDb());
    } catch (UnknownHostException e) {
        e.printStackTrace();
        return;
    }

    client = MongoClient.connect(dbaddress);

    if (!Strings.isNullOrEmpty(config.getUser()) && !Strings.isNullOrEmpty(config.getPassword()))
        client.authenticate(config.getUser(), config.getPassword().toCharArray());

    if (!client.collectionExists(config.getCollection())) {
        client.createCollection(config.getCollection(), null);
    }
    ;

    collection = client.getCollection(config.getCollection());
}

From source file:org.apache.streams.mongo.MongoPersistWriter.java

License:Apache License

private synchronized void connectToMongo() {
    try {/*  ww w  .  ja v a2 s .c  o  m*/
        dbaddress = new DBAddress(config.getHost(), config.getPort().intValue(), config.getDb());
    } catch (UnknownHostException e) {
        e.printStackTrace();
        return;
    }

    client = MongoClient.connect(dbaddress);

    if (!Strings.isNullOrEmpty(config.getUser()) && !Strings.isNullOrEmpty(config.getPassword()))
        client.authenticate(config.getUser(), config.getPassword().toCharArray());

    if (!client.collectionExists(config.getCollection())) {
        client.createCollection(config.getCollection(), null);
    }
    ;

    collection = client.getCollection(config.getCollection());
}