List of usage examples for com.mongodb ServerAddress ServerAddress
public ServerAddress(@Nullable final String host, final int port)
From source file:de.felix_klauke.pegasus.server.database.Database.java
License:Apache License
public Database(MongoCredential credential, String database, String collection) { final MongoClient mongoClient = new MongoClient(new ServerAddress("5.1.82.58", 27017), Collections.singletonList(credential)); this.database = mongoClient.getDatabase(database); this.collection = this.database.getCollection(collection); }
From source file:de.fhg.igd.mongomvcc.impl.MongoDBVDatabase.java
License:Open Source License
@Override public void connect(String name, String host, int port) throws VException { Mongo mongo;/*from w w w . j a v a 2 s. c om*/ try { mongo = new Mongo(new ServerAddress(host, port)); } catch (UnknownHostException e) { throw new VException("Unknown host", e); } connectInternal(name, mongo); }
From source file:de.fhg.igd.mongomvcc.impl.MongoDBVDatabase.java
License:Open Source License
@Override public void connectToReplicaSet(String name, Map<String, Integer> hostsWithPort, boolean slaveOk) throws VException { Mongo mongo = null;// www . j a v a 2 s .co m try { List<ServerAddress> addrs = new ArrayList<ServerAddress>(); for (Map.Entry<String, Integer> e : hostsWithPort.entrySet()) { addrs.add(new ServerAddress(e.getKey(), e.getValue())); } mongo = new Mongo(addrs); if (slaveOk && mongo != null) { mongo.slaveOk(); // mongo.setReadPreference(ReadPreference.SECONDARY); needed with version 2.2 } } catch (UnknownHostException e) { throw new VException("Unknown host", e); } connectInternal(name, mongo); }
From source file:de.flapdoodle.embed.mongo.tests.MongodForTestsFactory.java
License:Apache License
/** * Creates a new Mongo connection./*from w w w . j a v a2 s . c o m*/ * * @throws MongoException * @throws UnknownHostException */ public MongoClient newMongo() throws UnknownHostException, MongoException { return new MongoClient(new ServerAddress(mongodProcess.getConfig().net().getServerAddress(), mongodProcess.getConfig().net().getPort())); }
From source file:de.flapdoodle.embed.mongo.tests.MongosForTestsFactory.java
License:Apache License
/** * Creates a new Mongo connection./*from w w w .j a v a 2s .c o m*/ * * @throws MongoException * @throws UnknownHostException */ public Mongo newMongo() throws UnknownHostException, MongoException { return new Mongo(new ServerAddress(mongosProcess.getConfig().net().getServerAddress(), mongosProcess.getConfig().net().getPort())); }
From source file:de.flapdoodle.embed.mongo.tests.MongosSystemForTestFactory.java
License:Apache License
private void configureMongos() throws Exception { CommandResult cr;//from www. j a v a 2s.c o m MongoOptions mo = new MongoOptions(); mo.autoConnectRetry = true; Mongo mongo = new Mongo( new ServerAddress(this.config.net().getServerAddress().getHostName(), this.config.net().getPort()), mo); DB mongoAdminDB = mongo.getDB(ADMIN_DATABASE_NAME); // Add shard from the replica set list for (Entry<String, List<IMongodConfig>> entry : this.replicaSets.entrySet()) { String replicaName = entry.getKey(); String command = ""; for (IMongodConfig mongodConfig : entry.getValue()) { if (command.isEmpty()) { command = replicaName + "/"; } else { command += ","; } command += mongodConfig.net().getServerAddress().getHostName() + ":" + mongodConfig.net().getPort(); } logger.info("Execute add shard command: " + command); cr = mongoAdminDB.command(new BasicDBObject("addShard", command)); logger.info(cr.toString()); } logger.info("Execute list shards."); cr = mongoAdminDB.command(new BasicDBObject("listShards", 1)); logger.info(cr.toString()); // Enabled sharding at database level logger.info("Enabled sharding at database level"); cr = mongoAdminDB.command(new BasicDBObject("enableSharding", this.shardDatabase)); logger.info(cr.toString()); // Create index in sharded collection logger.info("Create index in sharded collection"); DB db = mongo.getDB(this.shardDatabase); db.getCollection(this.shardCollection).ensureIndex(this.shardKey); // Shard the collection logger.info("Shard the collection: " + this.shardDatabase + "." + this.shardCollection); DBObject cmd = new BasicDBObject(); cmd.put("shardCollection", this.shardDatabase + "." + this.shardCollection); cmd.put("key", new BasicDBObject(this.shardKey, 1)); cr = mongoAdminDB.command(cmd); logger.info(cr.toString()); logger.info("Get info from config/shards"); DBCursor cursor = mongo.getDB("config").getCollection("shards").find(); while (cursor.hasNext()) { DBObject item = cursor.next(); logger.info(item.toString()); } }
From source file:de.flapdoodle.embed.mongo.tests.MongosSystemForTestFactory.java
License:Apache License
public Mongo getMongo() throws UnknownHostException, MongoException { return new Mongo(new ServerAddress(mongosProcess.getConfig().net().getServerAddress(), mongosProcess.getConfig().net().getPort())); }
From source file:de.ifgi.fmt.mongo.MongoDB.java
License:Open Source License
/** * /*from w w w. j a v a 2s . c o m*/ */ protected MongoDB() { try { MorphiaLoggerFactory.registerLogger(SLF4JLogrImplFactory.class); InputStream is = getClass().getResourceAsStream(PROPERTIES_FILE); Properties p = new Properties(); if (is != null) { try { p.load(is); } finally { IOUtils.closeQuietly(is); } } String host = p.getProperty(HOST_PROPERTY); host = (host == null || host.trim().isEmpty()) ? "localhost" : host; String port = p.getProperty(PORT_PROPERTY); port = (port == null || port.trim().isEmpty()) ? "27017" : port; this.mongo = new Mongo(new ServerAddress(host, Integer.valueOf(port))); this.morphia = new Morphia(); DefaultConverters dc = this.morphia.getMapper().getConverters(); Set<Class<? extends TypeConverter>> classes = Implementations.getSubclasses(TypeConverter.class); if (log.isDebugEnabled()) { log.debug("{} Mongo converter classes found:\n{}", classes.size(), Utils.join(new Stringifier() { public String toString(Object t) { return " " + t.toString(); } }, "\n", classes)); } for (Class<? extends TypeConverter> c : classes) { dc.addConverter(c); } new MorphiaValidation().applyTo(this.morphia); String auth = p.getProperty(AUTH_PROPERTY); auth = (auth == null || auth.trim().isEmpty()) ? "false" : auth; String dbna = p.getProperty(DATABASE_PROPERTY); this.database = (auth == null || dbna.trim().isEmpty()) ? DEFAULT_DATABASE : dbna; if (Boolean.valueOf(auth)) { this.datastore = this.morphia.createDatastore(this.mongo, this.database, p.getProperty(USER_PROPERTY, "mongo"), p.getProperty(PASS_PROPERTY, "mongo").toCharArray()); } else { this.datastore = this.morphia.createDatastore(this.mongo, this.database); } } catch (Exception e) { throw new RuntimeException(e); } }
From source file:de.pvsnp.chat.mongodb.MongoManager.java
public void connect() { MongoCredential credential = MongoCredential.createScramSha1Credential(user, databaseAuth, password); List<MongoCredential> list = new ArrayList<MongoCredential>(); list.add(credential);// w ww .j ava 2 s .c o m this.client = new MongoClient(new ServerAddress(hostname, port), list); this.database = client.getDatabase(databaseName); massages = database.getCollection("massage"); }
From source file:de.steilerdev.myVerein.server.controller.init.InitController.java
License:Open Source License
/** * This function is validating the provided information of the MongoDB server, by establishing a test connection. * @param databaseHost The hostname of the MongoDB server. * @param databasePort The port of the MongoDB server. * @param databaseUser The user used to authenticate against the MongoDB server (may be empty if not needed). * @param databasePassword The password used to authenticate against the MongoDB server (may be empty if not needed). * @param databaseCollection The name of the database collection. * @return True if the connection was successfully established, false otherwise. *///from w w w. j a v a 2 s. c om private boolean mongoIsAvailable(String databaseHost, int databasePort, String databaseUser, String databasePassword, String databaseCollection) { logger.trace("Testing MongoDB connection"); if (!databaseUser.isEmpty() && !databasePassword.isEmpty()) { logger.debug("Credentials have been provided"); mongoCredential = Arrays.asList(MongoCredential.createMongoCRCredential(databaseUser, databaseCollection, databasePassword.toCharArray())); } try { logger.debug("Creating server address"); mongoAddress = new ServerAddress(databaseHost, databasePort); } catch (UnknownHostException e) { logger.warn("Unable to resolve server host: " + e.getMessage()); return false; } logger.debug("Creating mongo client"); MongoClient mongoClient = new MongoClient(mongoAddress, mongoCredential); try { //Checking if connection REALLY works logger.debug("Establishing connection now."); List<String> databases = mongoClient.getDatabaseNames(); if (databases == null) { logger.warn("The returned list of databases is null"); return false; } else if (databases.isEmpty()) { logger.info("The databases are empty"); return true; } else { logger.debug("The database connection seems okay"); return true; } } catch (MongoException e) { logger.warn("Unable to receive list of present databases: " + e.getMessage()); return false; } finally { logger.debug("Closing mongo client"); mongoClient.close(); } }