Search in sources :

Example 36 with MongoClientURI

use of com.mongodb.MongoClientURI in project spring-data-mongodb by spring-projects.

the class MongoResultsWriter method write.

@Override
public void write(Collection<RunResult> results) {
    Date now = new Date();
    StandardEnvironment env = new StandardEnvironment();
    String projectVersion = env.getProperty("project.version", "unknown");
    String gitBranch = env.getProperty("git.branch", "unknown");
    String gitDirty = env.getProperty("git.dirty", "no");
    String gitCommitId = env.getProperty("git.commit.id", "unknown");
    MongoClientURI uri = new MongoClientURI(this.uri);
    MongoClient client = new MongoClient(uri);
    String dbName = StringUtils.hasText(uri.getDatabase()) ? uri.getDatabase() : "spring-data-mongodb-benchmarks";
    MongoDatabase db = client.getDatabase(dbName);
    for (BasicDBObject dbo : (List<BasicDBObject>) JSON.parse(ResultsWriter.jsonifyResults(results))) {
        String collectionName = extractClass(dbo.get("benchmark").toString());
        Document sink = new Document();
        sink.append("_version", projectVersion);
        sink.append("_branch", gitBranch);
        sink.append("_commit", gitCommitId);
        sink.append("_dirty", gitDirty);
        sink.append("_method", extractBenchmarkName(dbo.get("benchmark").toString()));
        sink.append("_date", now);
        sink.append("_snapshot", projectVersion.toLowerCase().contains("snapshot"));
        sink.putAll(dbo);
        db.getCollection(collectionName).insertOne(fixDocumentKeys(sink));
    }
    client.close();
}
Also used : MongoClient(com.mongodb.MongoClient) BasicDBObject(com.mongodb.BasicDBObject) MongoClientURI(com.mongodb.MongoClientURI) List(java.util.List) Document(org.bson.Document) Date(java.util.Date) StandardEnvironment(org.springframework.core.env.StandardEnvironment) MongoDatabase(com.mongodb.client.MongoDatabase)

Example 37 with MongoClientURI

use of com.mongodb.MongoClientURI in project cas by apereo.

the class MongoDbConnectionFactory method buildMongoDbClientOptionsFactoryBean.

/**
 * Create a MongoClientOptions object.
 * <p>
 * The object will be created from a collection of individual property
 * settings, or a MongoDb client connection string (uri), or some
 * combination of the two.
 * <p>
 * This is complicated by the fact that the default values provided by
 * the CAS code in BaseMongoDbProperties.java are not the same as the
 * default values for the corresponding options provided by the MongoDb
 * Java driver when it creates a MongoClientOptions object.
 * <p>
 * To ensure predictable results in all cases, we initialize the client
 * options from the individual property settings (even if just the CAS
 * default values), and then use those values as the starting point to
 * process the client uri (if one is provided). This way, any options
 * in the uri will override the earlier ones, but any options missing
 * from the uri will have the values (default or user-provided) from
 * the individual property settings.
 * <p>
 * This behavior matches the comment in BaseMongoDbProperties.java for
 * the clientUri property: "If not specified, will fallback onto other
 * individual settings. If specified, takes over all other settings
 * where applicable."
 *
 * @param mongo the property setttings (including, perhaps, a client uri)
 * @return a bean containing the MongoClientOptions object
 */
@SneakyThrows
private MongoClientOptionsFactoryBean buildMongoDbClientOptionsFactoryBean(final BaseMongoDbProperties mongo) {
    final MongoClientOptionsFactoryBean bean1 = new MongoClientOptionsFactoryBean();
    bean1.setWriteConcern(WriteConcern.valueOf(mongo.getWriteConcern()));
    bean1.setHeartbeatConnectTimeout((int) Beans.newDuration(mongo.getTimeout()).toMillis());
    bean1.setHeartbeatSocketTimeout((int) Beans.newDuration(mongo.getTimeout()).toMillis());
    bean1.setMaxConnectionLifeTime(mongo.getConns().getLifetime());
    bean1.setSocketKeepAlive(mongo.isSocketKeepAlive());
    bean1.setMaxConnectionIdleTime((int) Beans.newDuration(mongo.getIdleTimeout()).toMillis());
    bean1.setConnectionsPerHost(mongo.getConns().getPerHost());
    bean1.setSocketTimeout((int) Beans.newDuration(mongo.getTimeout()).toMillis());
    bean1.setConnectTimeout((int) Beans.newDuration(mongo.getTimeout()).toMillis());
    if (StringUtils.isNotBlank(mongo.getReplicaSet())) {
        bean1.setRequiredReplicaSetName(mongo.getReplicaSet());
    }
    bean1.setSsl(mongo.isSslEnabled());
    if (mongo.isSslEnabled()) {
        bean1.setSslSocketFactory((SSLSocketFactory) SSLSocketFactory.getDefault());
    }
    bean1.afterPropertiesSet();
    if (StringUtils.isNotBlank(mongo.getClientUri())) {
        final MongoClientOptionsFactoryBean bean2 = new MongoClientOptionsFactoryBean();
        final MongoClientURI uri = buildMongoClientURI(mongo.getClientUri(), bean1.getObject());
        final MongoClientOptions opts = uri.getOptions();
        bean2.setWriteConcern(opts.getWriteConcern());
        bean2.setHeartbeatConnectTimeout(opts.getHeartbeatConnectTimeout());
        bean2.setHeartbeatSocketTimeout(opts.getHeartbeatSocketTimeout());
        bean2.setMaxConnectionLifeTime(opts.getMaxConnectionLifeTime());
        bean2.setSocketKeepAlive(opts.isSocketKeepAlive());
        bean2.setMaxConnectionIdleTime(opts.getMaxConnectionIdleTime());
        bean2.setConnectionsPerHost(opts.getConnectionsPerHost());
        bean2.setSocketTimeout(opts.getSocketTimeout());
        bean2.setConnectTimeout(opts.getConnectTimeout());
        bean2.setRequiredReplicaSetName(opts.getRequiredReplicaSetName());
        bean2.setSsl(opts.isSslEnabled());
        if (opts.isSslEnabled()) {
            bean2.setSslSocketFactory((SSLSocketFactory) SSLSocketFactory.getDefault());
        }
        bean2.afterPropertiesSet();
        bean1.destroy();
        return bean2;
    }
    return bean1;
}
Also used : MongoClientOptionsFactoryBean(org.springframework.data.mongodb.core.MongoClientOptionsFactoryBean) MongoClientOptions(com.mongodb.MongoClientOptions) MongoClientURI(com.mongodb.MongoClientURI) SneakyThrows(lombok.SneakyThrows)

Example 38 with MongoClientURI

use of com.mongodb.MongoClientURI in project cas by apereo.

the class CasMongoAuthenticationConfiguration method mongoAuthenticatorProfileService.

@ConditionalOnMissingBean(name = "mongoAuthenticatorProfileService")
@Bean
public MongoProfileService mongoAuthenticatorProfileService() {
    final MongoAuthenticationProperties mongo = casProperties.getAuthn().getMongo();
    final MongoClientURI uri = new MongoClientURI(mongo.getMongoHostUri());
    final MongoClient client = new MongoClient(uri);
    LOGGER.info("Connected to MongoDb instance @ [{}] using database [{}]", uri.getHosts(), uri.getDatabase());
    final SpringSecurityPasswordEncoder encoder = new SpringSecurityPasswordEncoder(PasswordEncoderUtils.newPasswordEncoder(mongo.getPasswordEncoder()));
    final MongoProfileService auth = new MongoProfileService(client, mongo.getAttributes());
    auth.setUsersCollection(mongo.getCollectionName());
    auth.setUsersDatabase(uri.getDatabase());
    auth.setUsernameAttribute(mongo.getUsernameAttribute());
    auth.setPasswordAttribute(mongo.getPasswordAttribute());
    auth.setPasswordEncoder(encoder);
    return auth;
}
Also used : MongoProfileService(org.pac4j.mongo.profile.service.MongoProfileService) MongoClient(com.mongodb.MongoClient) SpringSecurityPasswordEncoder(org.pac4j.core.credentials.password.SpringSecurityPasswordEncoder) MongoClientURI(com.mongodb.MongoClientURI) MongoAuthenticationProperties(org.apereo.cas.configuration.model.support.mongo.MongoAuthenticationProperties) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 39 with MongoClientURI

use of com.mongodb.MongoClientURI in project cas by apereo.

the class MongoAuthenticationHandler method getAuthenticator.

@Override
protected Authenticator<UsernamePasswordCredentials> getAuthenticator(final Credential credential) {
    final MongoClientURI uri = new MongoClientURI(this.mongoHostUri);
    final MongoClient client = new MongoClient(uri);
    LOGGER.info("Connected to MongoDb instance @ [{}] using database [{}]", uri.getHosts(), uri.getDatabase());
    final MongoAuthenticator mongoAuthenticator = new MongoAuthenticator(client, this.attributes);
    mongoAuthenticator.setUsersCollection(this.collectionName);
    mongoAuthenticator.setUsersDatabase(uri.getDatabase());
    mongoAuthenticator.setUsernameAttribute(this.usernameAttribute);
    mongoAuthenticator.setPasswordAttribute(this.passwordAttribute);
    mongoAuthenticator.setPasswordEncoder(this.mongoPasswordEncoder);
    return mongoAuthenticator;
}
Also used : MongoClient(com.mongodb.MongoClient) MongoClientURI(com.mongodb.MongoClientURI) MongoAuthenticator(org.pac4j.mongo.credentials.authenticator.MongoAuthenticator)

Example 40 with MongoClientURI

use of com.mongodb.MongoClientURI in project Mycat-Server by MyCATApache.

the class MongoDriver method connect.

@Override
public Connection connect(String url, Properties info) throws SQLException {
    MongoClientURI mcu = null;
    if ((mcu = parseURL(url, info)) == null) {
        return null;
    }
    MongoConnection result = null;
    //System.out.print(info);
    try {
        result = new MongoConnection(mcu, url);
    } catch (Exception e) {
        throw new SQLException("Unexpected exception: " + e.getMessage(), e);
    }
    return result;
}
Also used : SQLException(java.sql.SQLException) MongoClientURI(com.mongodb.MongoClientURI) SQLException(java.sql.SQLException) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException)

Aggregations

MongoClientURI (com.mongodb.MongoClientURI)73 MongoClient (com.mongodb.MongoClient)29 DBCollection (com.mongodb.DBCollection)12 Test (org.junit.Test)11 BasicDBObject (com.mongodb.BasicDBObject)10 MongoClientURIBuilder (com.mongodb.hadoop.util.MongoClientURIBuilder)8 List (java.util.List)8 Configuration (org.apache.hadoop.conf.Configuration)8 DBObject (com.mongodb.DBObject)7 ArrayList (java.util.ArrayList)7 MongoDatabase (com.mongodb.client.MongoDatabase)6 InputSplit (org.apache.hadoop.mapreduce.InputSplit)6 Document (org.bson.Document)6 DB (com.mongodb.DB)5 MongoInputSplit (com.mongodb.hadoop.input.MongoInputSplit)5 IOException (java.io.IOException)5 MongoConnection (org.apache.jackrabbit.oak.plugins.document.util.MongoConnection)5 OptionParser (joptsimple.OptionParser)4 OptionSet (joptsimple.OptionSet)4 DocumentNodeStore (org.apache.jackrabbit.oak.plugins.document.DocumentNodeStore)4