List of usage examples for com.mongodb ServerAddress ServerAddress
public ServerAddress(@Nullable final String host, final int port)
From source file:com.AlertMailerWebPage.servlet.MCRMData.java
public Document getMCRMObject(String database, String collection, String serverAddress, String email) { MongoClient mongoClient = new MongoClient(new ServerAddress(serverAddress, 27017)); MongoDatabase db = mongoClient.getDatabase(database); FindIterable<Document> iterable = db.getCollection(collection).find(new Document("email", email)) .projection(new Document("email", 0).append("_id", 0)); iterable.forEach(new Block<Document>() { @Override/*from ww w . ja v a 2 s .c o m*/ public void apply(final Document document) { MCRMData.document = document; } }); return document; }
From source file:com.AlertMailerWebPage.servlet.MCRMData.java
public void getSL_VM(String database, String collection, String serverAddress, String email) { MongoClient mongoClient = new MongoClient(new ServerAddress(serverAddress, 27017)); MongoDatabase db = mongoClient.getDatabase(database); FindIterable<Document> iterable = db.getCollection(collection).find(new Document("email", email)) .sort(new Document("_id", -1)).limit(1); iterable.forEach(new Block<Document>() { @Override/*from w ww. j ava2 s . c om*/ public void apply(final Document document) { SL = (String) document.get("subject"); VM = (String) document.get("vmfilename"); } }); }
From source file:com.almende.eve.state.mongo.MongoStateFactory.java
License:Apache License
private static MongoClient createClient(String databaseUri, int port) throws UnknownHostException { MongoClientOptions options = MongoClientOptions.builder().connectionsPerHost(100) .threadsAllowedToBlockForConnectionMultiplier(1500).build(); MongoClient client = new MongoClient(new ServerAddress(databaseUri, port), options); return client; }
From source file:com.analogmountains.flume.MongoSink.java
License:Open Source License
private List<ServerAddress> getSeeds(String seedsString) { List<ServerAddress> seeds = new LinkedList<ServerAddress>(); String[] seedStrings = StringUtils.deleteWhitespace(seedsString).split(","); for (String seed : seedStrings) { String[] hostAndPort = seed.split(":"); String host = hostAndPort[0]; int port; if (hostAndPort.length == 2) { port = Integer.parseInt(hostAndPort[1]); } else {/*from w ww . j av a2 s .c om*/ port = 27017; } seeds.add(new ServerAddress(host, port)); } return seeds; }
From source file:com.andreig.jetty.MongoDB.java
License:GNU General Public License
private static List<ServerAddress> str2addresses(String servers) throws UnknownHostException { List<ServerAddress> list = new ArrayList<ServerAddress>(); servers = servers.trim();/*from w ww.j ava 2 s.c o m*/ String ar[] = servers.split("[,]"); for (String s : ar) { s = s.trim(); String ar2[] = s.split("[:]"); if (ar2 == null || ar2.length != 2) throw new IllegalArgumentException("mongo server must be <server:port>"); int port = 27017; try { port = Integer.parseInt(ar2[1]); } catch (NumberFormatException e) { log.warning("Can not parse port for " + ar[0]); } ServerAddress adr = new ServerAddress(ar2[0], port); list.add(adr); } return list; }
From source file:com.appdynamics.monitors.mongo.MongoDBMonitor.java
License:Apache License
private MongoClient buildMongoClient(Configuration config, List<MongoCredential> credentials, MongoClientOptions options) {/*from w ww. ja v a2 s. c o m*/ List<ServerAddress> seeds = Lists.newArrayList(); for (Server server : config.getServers()) { seeds.add(new ServerAddress(server.getHost(), server.getPort())); } if (options == null && credentials.size() == 0) { mongoClient = new MongoClient(seeds); } else if (options == null && credentials.size() > 0) { mongoClient = new MongoClient(seeds, credentials); } else if (options != null && credentials.size() == 0) { mongoClient = new MongoClient(seeds, options); } else { mongoClient = new MongoClient(seeds, credentials, options); } return mongoClient; }
From source file:com.bc.fiduceo.db.MongoDbDriver.java
License:Open Source License
@Override public void open(BasicDataSource dataSource) throws SQLException { final String address = parseAddress(dataSource.getUrl()); final String port = parsePort(dataSource.getUrl()); final ServerAddress serverAddress = new ServerAddress(address, Integer.parseInt(port)); final String username = dataSource.getUsername(); final String password = dataSource.getPassword(); if (StringUtils.isNotNullAndNotEmpty(password) && StringUtils.isNotNullAndNotEmpty(username)) { final MongoCredential credential = MongoCredential.createCredential(username, DATABASE_NAME, password.toCharArray()); final List<MongoCredential> credentialsList = new ArrayList<>(); credentialsList.add(credential); mongoClient = new MongoClient(serverAddress, credentialsList); } else {// ww w . j a va 2 s . c om mongoClient = new MongoClient(serverAddress); } database = mongoClient.getDatabase(DATABASE_NAME); }
From source file:com.bluedragon.mongo.MongoDSN.java
License:Open Source License
public static MongoClient newClient(String server, String user, String pass, String db) throws UnknownHostException { MongoClientOptions options = MongoClientOptions.builder() .readPreference(ReadPreference.secondaryPreferred()).build(); List<InetSocketAddress> serverList = AddrUtil.getAddresses(server); List<ServerAddress> addrs = new ArrayList<ServerAddress>(); Iterator<InetSocketAddress> it = serverList.iterator(); while (it.hasNext()) { InetSocketAddress isa = it.next(); addrs.add(new ServerAddress(isa.getAddress(), isa.getPort())); }//from w w w . ja va 2s .c om if (user != null) { MongoCredential cred = MongoCredential.createCredential(user, db, pass.toCharArray()); List<MongoCredential> creds = new ArrayList<MongoCredential>(); creds.add(cred); return new MongoClient(addrs, creds, options); } else { return new MongoClient(addrs, options); } }
From source file:com.bnrc.sdn.properties.MongoProperties.java
License:Apache License
/** * Creates a {@link MongoClient} using the given {@code options} and * {@code environment}. If the configured port is zero, the value of the * {@code local.mongo.port} property retrieved from the {@code environment} is used * to configure the client. /*w w w . ja v a 2s .c o m*/ * * @param options the options * @param environment the environment * @return the Mongo client * @throws UnknownHostException if the configured host is unknown */ public MongoClient createMongoClient(MongoClientOptions options, Environment environment) throws UnknownHostException { try { if (hasCustomAddress() || hasCustomCredentials()) { if (options == null) { options = MongoClientOptions.builder().build(); } List<MongoCredential> credentials = null; if (hasCustomCredentials()) { String database = this.authenticationDatabase == null ? getMongoClientDatabase() : this.authenticationDatabase; credentials = Arrays.asList( MongoCredential.createMongoCRCredential(this.username, database, this.password)); } String host = this.host == null ? "localhost" : this.host; int port = determinePort(environment); return new MongoClient(Arrays.asList(new ServerAddress(host, port)), credentials, options); } // The options and credentials are in the URI System.out.println("uri=" + uri); return new MongoClient(new MongoClientURI(this.uri, builder(options))); } finally { clearPassword(); } }
From source file:com.bugull.mongo.BuguConnection.java
License:Apache License
private void doConnect() throws UnknownHostException, DBConnectionException { if (host != null && port != 0) { ServerAddress sa = new ServerAddress(host, port); if (options != null) { mc = new MongoClient(sa, options); } else {/*from www .j av a 2s .c o m*/ mc = new MongoClient(sa); } } else if (replicaSet != null) { if (options != null) { mc = new MongoClient(replicaSet, options); } else { mc = new MongoClient(replicaSet); } if (readPreference != null) { mc.setReadPreference(readPreference); } } if (mc != null) { db = mc.getDB(database); } else { throw new DBConnectionException( "Can not get database instance! Please ensure connected to mongoDB correctly."); } }