Search in sources :

Example 1 with MongoOptions

use of com.mongodb.MongoOptions in project sling by apache.

the class MongoDBResourceProviderFactory method activate.

@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.");
    }
    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);
}
Also used : Mongo(com.mongodb.Mongo) MongoOptions(com.mongodb.MongoOptions) DBAddress(com.mongodb.DBAddress) LoginException(org.apache.sling.api.resource.LoginException) DB(com.mongodb.DB) Activate(org.apache.felix.scr.annotations.Activate)

Aggregations

DB (com.mongodb.DB)1 DBAddress (com.mongodb.DBAddress)1 Mongo (com.mongodb.Mongo)1 MongoOptions (com.mongodb.MongoOptions)1 Activate (org.apache.felix.scr.annotations.Activate)1 LoginException (org.apache.sling.api.resource.LoginException)1