List of usage examples for com.mongodb MongoClientOptions builder
public static Builder builder()
From source file:org.apache.calcite.adapter.mongodb.MongoSchemaFactory.java
License:Apache License
public Schema create(SchemaPlus parentSchema, String name, Map<String, Object> operand) { final String host = (String) operand.get("host"); final String database = (String) operand.get("database"); final String authMechanismName = (String) operand.get("authMechanism"); final MongoClientOptions.Builder options = MongoClientOptions.builder(); final List<MongoCredential> credentials = new ArrayList<>(); if (authMechanismName != null) { final MongoCredential credential = createCredentials(operand); credentials.add(credential);/* w w w .j av a 2 s .co m*/ } return new MongoSchema(host, database, credentials, options.build()); }
From source file:org.apache.gora.mongodb.store.MongoStore.java
License:Apache License
/** * Retrieve a client connected to the MongoDB server to be used. * * @param servers/* w ww . ja v a 2 s.c o m*/ * This value should specify the host:port (at least one) for * connecting to remote MongoDB. Multiple values must be separated by * coma. * @return a {@link Mongo} instance connected to the server * @throws UnknownHostException */ private MongoClient getClient(MongoStoreParameters params) throws UnknownHostException { // Configure options MongoClientOptions.Builder optBuilder = new MongoClientOptions.Builder() .dbEncoderFactory(GoraDBEncoder.FACTORY); // Utf8 serialization! if (params.getReadPreference() != null) { optBuilder.readPreference(ReadPreference.valueOf(params.getReadPreference())); } if (params.getWriteConcern() != null) { optBuilder.writeConcern(WriteConcern.valueOf(params.getWriteConcern())); } // If configuration contains a login + secret, try to authenticated with DB List<MongoCredential> credentials = null; if (params.getLogin() != null && params.getSecret() != null) { credentials = new ArrayList<MongoCredential>(); credentials.add(MongoCredential.createCredential(params.getLogin(), params.getDbname(), params.getSecret().toCharArray())); } // Build server address List<ServerAddress> addrs = new ArrayList<ServerAddress>(); Iterable<String> serversArray = Splitter.on(",").split(params.getServers()); if (serversArray != null) { for (String server : serversArray) { Iterator<String> paramsIterator = Splitter.on(":").trimResults().split(server).iterator(); if (!paramsIterator.hasNext()) { // No server, use default addrs.add(new ServerAddress()); } else { String host = paramsIterator.next(); if (paramsIterator.hasNext()) { String port = paramsIterator.next(); addrs.add(new ServerAddress(host, Integer.parseInt(port))); } else { addrs.add(new ServerAddress(host)); } } } } // Connect to the Mongo server return new MongoClient(addrs, credentials, optBuilder.build()); }
From source file:org.apache.jackrabbit.oak.plugins.document.util.MongoConnection.java
License:Apache License
/** * Returns {@code true} if the given {@code uri} has a write concern set. * @param uri the URI to check.//from w w w.j av a 2 s . c o m * @return {@code true} if the URI has a write concern set, {@code false} * otherwise. */ public static boolean hasWriteConcern(@Nonnull String uri) { MongoClientOptions.Builder builder = MongoClientOptions.builder(); builder.writeConcern(WC_UNKNOWN); WriteConcern wc = new MongoClientURI(checkNotNull(uri), builder).getOptions().getWriteConcern(); return !WC_UNKNOWN.equals(wc); }
From source file:org.apache.jackrabbit.oak.plugins.document.util.MongoConnection.java
License:Apache License
/** * Constructs a new connection using the specified MongoDB connection string. * See also http://docs.mongodb.org/manual/reference/connection-string/ * * @param uri the MongoDB URI/*from w ww .j a v a 2s. co m*/ * @throws UnknownHostException */ public MongoConnection(String uri) throws UnknownHostException { MongoClientOptions.Builder builder = MongoConnection.getDefaultBuilder(); mongoURI = new MongoClientURI(uri, builder); mongo = new MongoClient(mongoURI); }
From source file:org.apache.jackrabbit.oak.plugins.document.util.MongoConnection.java
License:Apache License
/** * Constructs a builder with default options set. These can be overridden later * * @return builder with default options set *///from w w w . j a v a 2 s.c o m public static MongoClientOptions.Builder getDefaultBuilder() { return new MongoClientOptions.Builder().description("MongoConnection for Oak DocumentMK") .threadsAllowedToBlockForConnectionMultiplier(100); }
From source file:org.apache.jmeter.protocol.mongodb.config.MongoSourceElement.java
License:Apache License
@Override public void testStarted() { if (log.isDebugEnabled()) { log.debug(getTitle() + " testStarted"); }//from w ww . ja va2 s . c o m MongoClientOptions.Builder builder = MongoClientOptions.builder().autoConnectRetry(getAutoConnectRetry()) .connectTimeout(getConnectTimeout()).connectionsPerHost(getConnectionsPerHost()) .maxAutoConnectRetryTime(getMaxAutoConnectRetryTime()).maxWaitTime(getMaxWaitTime()) .socketKeepAlive(getSocketKeepAlive()).socketTimeout(getSocketTimeout()) .threadsAllowedToBlockForConnectionMultiplier(getThreadsAllowedToBlockForConnectionMultiplier()); if (getSafe()) { builder.writeConcern(WriteConcern.SAFE); } else { builder.writeConcern(new WriteConcern(getWriteOperationNumberOfServers(), getWriteOperationTimeout(), getFsync(), getWaitForJournaling(), getContinueOnInsertError())); } MongoClientOptions mongoOptions = builder.build(); if (log.isDebugEnabled()) { log.debug("options : " + mongoOptions.toString()); } if (getThreadContext().getVariables().getObject(getSource()) != null) { if (log.isWarnEnabled()) { log.warn(getSource() + " has already been defined."); } } else { if (log.isDebugEnabled()) { log.debug(getSource() + " is being defined."); } try { getThreadContext().getVariables().putObject(getSource(), new MongoDB(MongoUtils.toServerAddresses(getConnection()), mongoOptions)); } catch (UnknownHostException e) { throw new IllegalStateException(e); } } }
From source file:org.apache.tapestry5.internal.mongodb.MongoDBSourceImpl.java
License:Apache License
public MongoDBSourceImpl(Logger logger, @Symbol(MongoDBSymbols.CONNECTIONS_PER_HOSTS) int connectionPerHost, @Symbol(MongoDBSymbols.READ_PREFERENCE) ReadPreference readPreference, @Symbol(MongoDBSymbols.WRITE_CONCERN) WriteConcern writeConcern, List<ServerAddress> serverAddresses) { this.logger = logger; MongoClientOptions options = new MongoClientOptions.Builder().connectionsPerHost(connectionPerHost) .writeConcern(writeConcern).readPreference(readPreference).build(); if (serverAddresses.isEmpty()) { try {//from w ww.j a v a 2 s .co m mongoClient = new MongoClient(new ServerAddress(), options); } catch (UnknownHostException uhe) { throw new RuntimeException(uhe); } } else { mongoClient = new MongoClient(serverAddresses, options); } }
From source file:org.axonframework.mongo.eventsourcing.eventstore.MongoOptionsFactory.java
License:Apache License
/** * Default constructor for the factory that initializes the defaults. */ public MongoOptionsFactory() { defaults = MongoClientOptions.builder().build(); }
From source file:org.axonframework.mongo.eventsourcing.eventstore.MongoOptionsFactory.java
License:Apache License
/** * Uses the configured parameters to create a MongoOptions instance. * * @return MongoOptions instance based on the configured properties *//*w w w . j av a 2s .c o m*/ public MongoClientOptions createMongoOptions() { MongoClientOptions options = MongoClientOptions.builder().connectionsPerHost(getConnectionsPerHost()) .connectTimeout(getConnectionTimeout()).maxWaitTime(getMaxWaitTime()) .threadsAllowedToBlockForConnectionMultiplier(getThreadsAllowedToBlockForConnectionMultiplier()) .socketTimeout(getSocketTimeOut()).build(); if (logger.isDebugEnabled()) { logger.debug("Mongo Options"); logger.debug("Connections per host :{}", options.getConnectionsPerHost()); logger.debug("Connection timeout : {}", options.getConnectTimeout()); logger.debug("Max wait timeout : {}", options.getMaxWaitTime()); logger.debug("Threads allowed to block : {}", options.getThreadsAllowedToBlockForConnectionMultiplier()); logger.debug("Socket timeout : {}", options.getSocketTimeout()); } return options; }
From source file:org.codinjutsu.tools.mongo.logic.MongoManager.java
License:Apache License
private MongoClient createMongoClient(ServerConfiguration configuration) throws UnknownHostException { List<String> serverUrls = configuration.getServerUrls(); String username = configuration.getUsername(); String password = configuration.getPassword(); String userDatabase = configuration.getUserDatabase(); if (serverUrls.isEmpty()) { throw new ConfigurationException("server host is not set"); }/*from w ww .j a v a 2 s .c om*/ List<ServerAddress> serverAddresses = new LinkedList<ServerAddress>(); for (String serverUrl : serverUrls) { String[] host_port = serverUrl.split(":"); serverAddresses.add(new ServerAddress(host_port[0], Integer.valueOf(host_port[1]))); } MongoClientOptions.Builder optionBuilder = MongoClientOptions.builder(); if (configuration.isSslConnection()) { optionBuilder.socketFactory(SSLSocketFactory.getDefault()); } if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)) { MongoCredential credential = MongoCredential.createMongoCRCredential(username, userDatabase, password.toCharArray()); return new MongoClient(serverAddresses, Arrays.asList(credential), optionBuilder.build()); } return new MongoClient(serverAddresses, optionBuilder.build()); }