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();
}
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;
}
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;
}
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;
}
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;
}
Aggregations