List of usage examples for com.mongodb MongoClientOptions.Builder codecRegistry
CodecRegistry codecRegistry
To view the source code for com.mongodb MongoClientOptions.Builder codecRegistry.
Click Source Link
From source file:io.lumeer.storage.mongodb.MongoDbStorage.java
License:Open Source License
@Override public void connect(final List<StorageConnection> connections, final String database, final Boolean useSsl) { final List<ServerAddress> addresses = new ArrayList<>(); connections.forEach(c -> {/* w w w . j a v a2 s .c om*/ addresses.add(new ServerAddress(c.getHost(), c.getPort())); }); MongoCredential credential = null; if (connections.size() > 0 && connections.get(0).getUserName() != null && !connections.get(0).getUserName().isEmpty()) { credential = MongoCredential.createScramSha1Credential(connections.get(0).getUserName(), database, connections.get(0).getPassword()); } final MongoClientOptions.Builder optionsBuilder = (new MongoClientOptions.Builder()).connectTimeout(30000); if (useSsl) { optionsBuilder.sslEnabled(true).socketFactory(NaiveTrustManager.getSocketFactory()) .sslInvalidHostNameAllowed(true); } final CodecRegistry defaultRegistry = MongoClient.getDefaultCodecRegistry(); final CodecRegistry codecRegistry = CodecRegistries.fromCodecs(new BigDecimalCodec(), new RoleCodec()); final CodecRegistry providersRegistry = CodecRegistries.fromProviders(new PermissionsCodecProvider(), new PermissionCodecProvider(), new QueryCodecProvider(), new ViewCodecProvider(), new AttributeCodecProvider(), new LinkInstanceCodecProvider(), new LinkTypeCodecProvider(), new UserCodecProvider(), new GroupCodecProvider(), new PaymentCodecProvider(), new CompanyContactCodedProvider(), new UserLoginEventCodecProvider(), new FeedbackCodecProvider()); final CodecRegistry registry = CodecRegistries.fromRegistries(defaultRegistry, codecRegistry, providersRegistry); if (credential != null) { this.mongoClient = new MongoClient(addresses, credential, optionsBuilder.codecRegistry(registry).build()); } else { this.mongoClient = new MongoClient(addresses, optionsBuilder.codecRegistry(registry).build()); } this.database = mongoClient.getDatabase(database); this.datastore = (AdvancedDatastore) morphia.createDatastore(this.mongoClient, database); }
From source file:org.grails.datastore.gorm.mongo.bean.factory.MongoClientFactoryBean.java
License:Apache License
public void afterPropertiesSet() throws UnknownHostException { // apply defaults - convenient when used to configure for tests // in an application context if (mongo != null) { return;// ww w . j a v a 2s .c om } ServerAddress defaultOptions = new ServerAddress(); List<MongoCredential> credentials = new ArrayList<MongoCredential>(); if (mongoOptions == null) { MongoClientOptions.Builder builder = MongoClientOptions.builder(); builder.codecRegistry(CodecRegistries.fromRegistries(codecRegistries)); mongoOptions = builder.build(); } // If username/pw exists and we are not authenticated, authenticate now if (username != null && password != null) { credentials.add(MongoCredential.createCredential(username, database, password.toCharArray())); } if (replicaPair != null) { if (replicaPair.size() < 2) { throw new DatastoreConfigurationException("A replica pair must have two server entries"); } mongo = new MongoClient(replicaPair, credentials, mongoOptions); } else if (replicaSetSeeds != null) { mongo = new MongoClient(replicaSetSeeds, credentials, mongoOptions); } else if (clientURI != null) { mongo = new MongoClient(clientURI); } else if (connectionString != null) { mongo = new MongoClient(new MongoClientURI(connectionString)); } else { String mongoHost = host != null ? host : defaultOptions.getHost(); if (port != null) { mongo = new MongoClient(new ServerAddress(mongoHost, port), credentials, mongoOptions); } else { mongo = new MongoClient(new ServerAddress(host), credentials, mongoOptions); } } }