List of usage examples for com.mongodb ReadPreference valueOf
public static ReadPreference valueOf(final String name)
From source file:org.pentaho.mongo.MongoProperties.java
License:Open Source License
/** * @return the com.mongodb.ReadPreference associated with the MongoProp.readPreference value. *///w w w . j av a 2 s .c om public ReadPreference getReadPreference() { return ReadPreference.valueOf(props.get(readPreference)); }
From source file:org.seasr.meandre.components.tools.db.mongodb.MongoDBClient.java
License:Open Source License
@Override public void initializeCallBack(ComponentContextProperties ccp) throws Exception { String location = getPropertyOrDieTrying(PROP_MONGODB_SERVER_LOCATION, ccp); String readPreference = getPropertyOrDieTrying(PROP_MONGODB_READ_PREFERENCE, ccp); console.fine(String.format("Got mongo server location %s", location)); try {/*ww w . j a v a2 s. c o m*/ mongoClientURI = new MongoClientURI(location); mongoClient = new MongoClient(mongoClientURI); } catch (IllegalArgumentException e) { console.warning(String.format( "Could not interpret %s as a mongo URI, interpreting as a server:port string.", location)); console.warning(String.format("Exception reason: %s", e.getMessage())); mongoClient = new MongoClient(location); } if (readPreference != "") mongoClient.setReadPreference(ReadPreference.valueOf(readPreference)); //mongoClient.logger. if (Boolean.parseBoolean(getPropertyOrDieTrying(PROP_MONGODB_RESTRICT_LOGGING, ccp))) { Logger.getLogger("com.mongodb.ReplicaSetStatus").setLevel(Level.SEVERE); } console.fine(String.format("Connected to mongodb with URI/location %s ", location)); }
From source file:org.springframework.data.mongodb.config.ReadPreferencePropertyEditor.java
License:Apache License
@Override public void setAsText(String readPreferenceString) throws IllegalArgumentException { if (readPreferenceString == null) { return;//from w w w. ja va 2 s. com } ReadPreference preference = null; try { preference = ReadPreference.valueOf(readPreferenceString); } catch (IllegalArgumentException ex) { // ignore this one and try to map it differently } if (preference != null) { setValue(preference); } else if ("PRIMARY".equalsIgnoreCase(readPreferenceString)) { setValue(ReadPreference.primary()); } else if ("PRIMARY_PREFERRED".equalsIgnoreCase(readPreferenceString)) { setValue(ReadPreference.primaryPreferred()); } else if ("SECONDARY".equalsIgnoreCase(readPreferenceString)) { setValue(ReadPreference.secondary()); } else if ("SECONDARY_PREFERRED".equalsIgnoreCase(readPreferenceString)) { setValue(ReadPreference.secondaryPreferred()); } else if ("NEAREST".equalsIgnoreCase(readPreferenceString)) { setValue(ReadPreference.nearest()); } else { throw new IllegalArgumentException( String.format("Cannot find matching ReadPreference for %s", readPreferenceString)); } }
From source file:org.wso2.carbon.dataservices.core.description.config.MongoConfig.java
License:Open Source License
public MongoConfig(DataService dataService, String configId, Map<String, String> properties, boolean odataEnable) throws DataServiceFault { super(dataService, configId, DBConstants.DataSourceTypes.MONGODB, properties, odataEnable); String serversParam = properties.get(DBConstants.MongoDB.SERVERS); if (DBUtils.isEmptyString(serversParam)) { throw new DataServiceFault("The data source param '" + DBConstants.MongoDB.SERVERS + "' is required"); }/*from ww w. ja va 2s . c o m*/ this.servers = serversParam.split(","); String database = properties.get(DBConstants.MongoDB.DATABASE); if (DBUtils.isEmptyString(database)) { throw new DataServiceFault("The data source param '" + DBConstants.MongoDB.DATABASE + "' is required"); } try { this.mongoClientOptions = extractMongoOptions(properties); this.mongoClient = createNewMongo(properties); String writeConcern = properties.get(DBConstants.MongoDB.WRITE_CONCERN); if (!DBUtils.isEmptyString(writeConcern)) { this.getMongoClient().setWriteConcern(WriteConcern.valueOf(writeConcern)); } String readPref = properties.get(DBConstants.MongoDB.READ_PREFERENCE); if (!DBUtils.isEmptyString(readPref)) { this.getMongoClient().setReadPreference(ReadPreference.valueOf(readPref)); } this.getMongoClient().getDatabase(database); this.jongo = new Jongo(this.getMongoClient().getDB(database)); } catch (Exception e) { throw new DataServiceFault(e, DBConstants.FaultCodes.CONNECTION_UNAVAILABLE_ERROR, e.getMessage()); } }
From source file:org.wso2.extension.siddhi.store.mongodb.util.MongoTableUtils.java
License:Open Source License
/** * Utility method which can be used to create MongoClientOptionsBuilder from values defined in the * deployment yaml file.// w w w. j a v a 2 s. co m * * @param storeAnnotation the source annotation which contains the needed parameters. * @param configReader {@link ConfigReader} Configuration Reader * @return MongoClientOptions.Builder */ public static MongoClientOptions.Builder extractMongoClientOptionsBuilder(Annotation storeAnnotation, ConfigReader configReader) { MongoClientOptions.Builder mongoClientOptionsBuilder = MongoClientOptions.builder(); try { mongoClientOptionsBuilder.connectionsPerHost( Integer.parseInt(configReader.readConfig(MongoTableConstants.CONNECTIONS_PER_HOST, "100"))); mongoClientOptionsBuilder.connectTimeout( Integer.parseInt(configReader.readConfig(MongoTableConstants.CONNECT_TIMEOUT, "10000"))); mongoClientOptionsBuilder.heartbeatConnectTimeout(Integer .parseInt(configReader.readConfig(MongoTableConstants.HEARTBEAT_CONNECT_TIMEOUT, "20000"))); mongoClientOptionsBuilder.heartbeatSocketTimeout(Integer .parseInt(configReader.readConfig(MongoTableConstants.HEARTBEAT_SOCKET_TIMEOUT, "20000"))); mongoClientOptionsBuilder.heartbeatFrequency( Integer.parseInt(configReader.readConfig(MongoTableConstants.HEARTBEAT_FREQUENCY, "10000"))); mongoClientOptionsBuilder.localThreshold( Integer.parseInt(configReader.readConfig(MongoTableConstants.LOCAL_THRESHOLD, "15"))); mongoClientOptionsBuilder.maxWaitTime( Integer.parseInt(configReader.readConfig(MongoTableConstants.MAX_WAIT_TIME, "120000"))); mongoClientOptionsBuilder.minConnectionsPerHost( Integer.parseInt(configReader.readConfig(MongoTableConstants.MIN_CONNECTIONS_PER_HOST, "0"))); mongoClientOptionsBuilder.minHeartbeatFrequency( Integer.parseInt(configReader.readConfig(MongoTableConstants.MIN_HEARTBEAT_FREQUENCY, "500"))); mongoClientOptionsBuilder.serverSelectionTimeout(Integer .parseInt(configReader.readConfig(MongoTableConstants.SERVER_SELECTION_TIMEOUT, "30000"))); mongoClientOptionsBuilder.socketTimeout( Integer.parseInt(configReader.readConfig(MongoTableConstants.SOCKET_TIMEOUT, "0"))); mongoClientOptionsBuilder.threadsAllowedToBlockForConnectionMultiplier( Integer.parseInt(configReader.readConfig(MongoTableConstants.THREADS_ALLOWED_TO_BLOCK, "5"))); mongoClientOptionsBuilder.socketKeepAlive( Boolean.parseBoolean(configReader.readConfig(MongoTableConstants.SOCKET_KEEP_ALIVE, "false"))); mongoClientOptionsBuilder.sslEnabled( Boolean.parseBoolean(configReader.readConfig(MongoTableConstants.SSL_ENABLED, "false"))); mongoClientOptionsBuilder.cursorFinalizerEnabled(Boolean .parseBoolean(configReader.readConfig(MongoTableConstants.CURSOR_FINALIZER_ENABLED, "true"))); mongoClientOptionsBuilder.readPreference(ReadPreference .valueOf(configReader.readConfig(MongoTableConstants.READ_PREFERENCE, "primary"))); mongoClientOptionsBuilder.writeConcern(WriteConcern .valueOf(configReader.readConfig(MongoTableConstants.WRITE_CONCERN, "acknowledged"))); String readConcern = configReader.readConfig(MongoTableConstants.READ_CONCERN, "DEFAULT"); if (!readConcern.matches("DEFAULT")) { mongoClientOptionsBuilder.readConcern(new ReadConcern(ReadConcernLevel.fromString(readConcern))); } int maxConnectionIdleTime = Integer .parseInt(configReader.readConfig(MongoTableConstants.MAX_CONNECTION_IDLE_TIME, "0")); if (maxConnectionIdleTime != 0) { mongoClientOptionsBuilder.maxConnectionIdleTime(maxConnectionIdleTime); } int maxConnectionLifeTime = Integer .parseInt(configReader.readConfig(MongoTableConstants.MAX_CONNECTION_LIFE_TIME, "0")); if (maxConnectionIdleTime != 0) { mongoClientOptionsBuilder.maxConnectionLifeTime(maxConnectionLifeTime); } String requiredReplicaSetName = configReader.readConfig(MongoTableConstants.REQUIRED_REPLICA_SET_NAME, ""); if (!requiredReplicaSetName.equals("")) { mongoClientOptionsBuilder.requiredReplicaSetName(requiredReplicaSetName); } String applicationName = configReader.readConfig(MongoTableConstants.APPLICATION_NAME, ""); if (!applicationName.equals("")) { mongoClientOptionsBuilder.applicationName(applicationName); } String secureConnectionEnabled = storeAnnotation .getElement(MongoTableConstants.ANNOTATION_ELEMENT_SECURE_CONNECTION); secureConnectionEnabled = secureConnectionEnabled == null ? "false" : secureConnectionEnabled; if (secureConnectionEnabled.equalsIgnoreCase("true")) { mongoClientOptionsBuilder.sslEnabled(true); String trustStore = storeAnnotation.getElement(MongoTableConstants.ANNOTATION_ELEMENT_TRUSTSTORE); trustStore = trustStore == null ? configReader.readConfig("trustStore", DEFAULT_TRUST_STORE_FILE) : trustStore; trustStore = resolveCarbonHome(trustStore); String trustStorePassword = storeAnnotation .getElement(MongoTableConstants.ANNOTATION_ELEMENT_TRUSTSTOREPASS); trustStorePassword = trustStorePassword == null ? configReader.readConfig("trustStorePassword", DEFAULT_TRUST_STORE_PASSWORD) : trustStorePassword; String keyStore = storeAnnotation.getElement(MongoTableConstants.ANNOTATION_ELEMENT_KEYSTORE); keyStore = keyStore == null ? configReader.readConfig("keyStore", DEFAULT_KEY_STORE_FILE) : keyStore; keyStore = resolveCarbonHome(keyStore); String keyStorePassword = storeAnnotation .getElement(MongoTableConstants.ANNOTATION_ELEMENT_STOREPASS); keyStorePassword = keyStorePassword == null ? configReader.readConfig("keyStorePassword", DEFAULT_KEY_STORE_PASSWORD) : keyStorePassword; mongoClientOptionsBuilder.socketFactory(MongoTableUtils.extractSocketFactory(trustStore, trustStorePassword, keyStore, keyStorePassword)); } return mongoClientOptionsBuilder; } catch (IllegalArgumentException e) { throw new MongoTableException("Values Read from config readers have illegal values : ", e); } }
From source file:thingynet.mongo.MongoConfig.java
License:Apache License
@Bean MongoClient mongoClient() throws IOException { ArrayList<ServerAddress> servers = new ArrayList<>(); for (int i = 0; i < mongoHosts.length; i++) { servers.add(new ServerAddress(mongoHosts[i], mongoPorts[i])); }/*from ww w . j a v a2s. c om*/ WriteConcern writeConcern = WriteConcern.valueOf(mongoWriteConcern); ReadPreference readPreference = ReadPreference.valueOf(mongoReadPreference); MongoClientOptions options = new MongoClientOptions.Builder().connectionsPerHost(mongoConnectionsPerHost) .writeConcern(writeConcern).readPreference(readPreference).build(); return new MongoClient(servers, options); }
From source file:uk.ac.ebi.eva.dbmigration.mongodb.MongobeeHelper.java
License:Apache License
private static MongoClientOptions getOptions(DatabaseParameters databaseParameters) { MongoClientOptions.Builder builder = new MongoClientOptions.Builder(); builder.readPreference(ReadPreference.valueOf(databaseParameters.getDbReadPreference())); return builder.build(); }
From source file:uk.ac.ebi.eva.lib.datastore.DBAdaptorConnector.java
License:Apache License
/** * Get a MongoClient using the configuration (credentials) in a given Properties. * * @param properties can have the next values: * - eva.mongo.auth.db authentication database * - eva.mongo.host comma-separated strings of colon-separated host and port strings: host_1:port_1,host_2:port_2 * - eva.mongo.user//from w w w . j a va 2 s. com * - eva.mongo.passwd * - eva.mongo.read-preference string, "secondaryPreferred" if unspecified. one of: * [primary, primaryPreferred, secondary, secondaryPreferred, nearest] * @return MongoClient with given credentials * @throws UnknownHostException */ public static MongoClient getMongoClient(Properties properties) throws UnknownHostException { String[] hosts = properties.getProperty("eva.mongo.host").split(","); List<ServerAddress> servers = new ArrayList<>(); // Get the list of hosts (optionally including the port number) for (String host : hosts) { String[] params = host.split(":"); if (params.length > 1) { servers.add(new ServerAddress(params[0], Integer.parseInt(params[1]))); } else { servers.add(new ServerAddress(params[0], 27017)); } } List<MongoCredential> mongoCredentialList = null; String authenticationDb = properties.getProperty("eva.mongo.auth.db", null); if (authenticationDb != null && !authenticationDb.isEmpty()) { mongoCredentialList = Collections .singletonList(MongoCredential.createCredential(properties.getProperty("eva.mongo.user"), authenticationDb, properties.getProperty("eva.mongo.passwd").toCharArray())); } String readPreference = properties.getProperty("eva.mongo.read-preference"); readPreference = readPreference == null || readPreference.isEmpty() ? "secondaryPreferred" : readPreference; MongoClientOptions options = MongoClientOptions.builder() .readPreference(ReadPreference.valueOf(readPreference)).build(); return new MongoClient(servers, mongoCredentialList, options); }
From source file:uk.ac.ebi.eva.pipeline.parameters.MongoConnection.java
License:Apache License
public ReadPreference getReadPreference() { return ReadPreference.valueOf(readPreference); }
From source file:uk.ac.ebi.eva.vcfdump.server.configuration.DBAdaptorConnector.java
License:Apache License
/** * Get a MongoClient using the configuration (credentials) in a given Properties. * * @param springDataMongoDbProperties can have the next values: * - eva.mongo.auth.db authentication database * - eva.mongo.host comma-separated strings of colon-separated host and port strings: host_1:port_1,host_2:port_2 * - eva.mongo.user/*from w w w.j av a2 s . c o m*/ * - eva.mongo.passwd * - eva.mongo.read-preference string, "secondaryPreferred" if unspecified. one of: * [primary, primaryPreferred, secondary, secondaryPreferred, nearest] * @return MongoClient with given credentials * @throws UnknownHostException */ public static MongoClient getMongoClient(SpringDataMongoDbProperties springDataMongoDbProperties) throws UnknownHostException { String[] hosts = springDataMongoDbProperties.getHost().split(","); List<ServerAddress> servers = new ArrayList<>(); // Get the list of hosts (optionally including the port number) for (String host : hosts) { String[] params = host.split(":"); if (params.length > 1) { servers.add(new ServerAddress(params[0], Integer.parseInt(params[1]))); } else { servers.add(new ServerAddress(params[0], 27017)); } } List<MongoCredential> mongoCredentialList = new ArrayList<>(); String authenticationDb = springDataMongoDbProperties.getAuthenticationDatabase(); if (authenticationDb != null && !authenticationDb.isEmpty()) { mongoCredentialList = Collections .singletonList(MongoCredential.createCredential(springDataMongoDbProperties.getUsername(), authenticationDb, springDataMongoDbProperties.getPassword().toCharArray())); } String readPreference = springDataMongoDbProperties.getReadPreference(); readPreference = readPreference == null || readPreference.isEmpty() ? "secondaryPreferred" : readPreference; MongoClientOptions options = MongoClientOptions.builder() .readPreference(ReadPreference.valueOf(readPreference)).build(); return new MongoClient(servers, mongoCredentialList, options); }