List of usage examples for com.mongodb ServerAddress getHost
public String getHost()
From source file:com.gopivotal.cloudfoundry.test.core.MongoDbUtils.java
License:Apache License
public String getUrl(MongoDbFactory mongoDbFactory) { DB mongoDb = mongoDbFactory.getDb(); if (mongoDb != null) { ServerAddress serverAddress = mongoDb.getMongo().getAddress(); String host = serverAddress.getHost(); int port = serverAddress.getPort(); String db = mongoDb.getName(); return String.format("mongodb://%s:%d/%s", host, port, db); }/*from ww w . ja v a 2 s. co m*/ return "mongodb://fake.connection"; }
From source file:com.meltmedia.dropwizard.mongo.MongoBundle.java
License:Apache License
MongoClient buildClient(MongoConfiguration configuration) { try {//www. j a v a 2 s .c om // build the seed server list. List<ServerAddress> servers = new ArrayList<>(); for (Server seed : configuration.getSeeds()) { servers.add(new ServerAddress(seed.getHost(), seed.getPort())); } log.info("Found {} mongo seed servers", servers.size()); for (ServerAddress server : servers) { log.info("Found mongo seed server {}:{}", server.getHost(), server.getPort()); } // build the credentials Credentials credentialConfig = configuration.getCredentials(); List<MongoCredential> credentials = credentialConfig == null ? Collections.<MongoCredential>emptyList() : Collections.singletonList(MongoCredential.createCredential(credentialConfig.getUserName(), configuration.getDatabase(), credentialConfig.getPassword().toCharArray())); if (credentials.isEmpty()) { log.info("Found {} mongo credentials.", credentials.size()); } else { for (MongoCredential credential : credentials) { log.info("Found mongo credential for {} on database {}.", credential.getUserName(), credential.getSource()); } } // build the options. MongoClientOptions options = new MongoClientOptions.Builder() .writeConcern(writeConcern(configuration.getWriteConcern())).build(); log.info("Mongo database is {}", configuration.getDatabase()); return new MongoClient(servers, credentials, options); } catch (Exception e) { throw new RuntimeException("Could not configure MongoDB client.", e); } }
From source file:com.navercorp.pinpoint.plugin.mongo.interceptor.MongoDriverConnectInterceptor3_0.java
License:Apache License
private List<String> getHostList(Object arg) { if (!(arg instanceof Cluster)) { return Collections.emptyList(); }/*ww w. j a v a 2 s .c o m*/ final Cluster cluster = (Cluster) arg; final List<String> hostList = new ArrayList<String>(); Collection<ServerDescription> serverDescriptions;// = cluster.getDescription().getAll();//.getServerDescriptions(); try { ClusterDescription.class.getDeclaredMethod("getServerDescriptions"); serverDescriptions = cluster.getDescription().getServerDescriptions(); } catch (NoSuchMethodException e) { serverDescriptions = cluster.getDescription().getAll(); } for (ServerDescription serverDescription : serverDescriptions) { ServerAddress serverAddress = serverDescription.getAddress(); final String hostAddress = HostAndPort.toHostAndPortString(serverAddress.getHost(), serverAddress.getPort()); hostList.add(hostAddress); } return hostList; }
From source file:com.navercorp.pinpoint.plugin.mongo.interceptor.MongoDriverConnectInterceptor3_7.java
License:Apache License
private List<String> getHostList(Object arg) { if (!(arg instanceof MongoClientSettings)) { return Collections.emptyList(); }/*www . j a v a2 s.c o m*/ final MongoClientSettings mongoClientSettings = (MongoClientSettings) arg; List<ServerAddress> lists = mongoClientSettings.getClusterSettings().getHosts(); final List<String> hostList = new ArrayList<String>(); for (ServerAddress sa : lists) { final String hostAddress = HostAndPort.toHostAndPortString(sa.getHost(), sa.getPort()); hostList.add(hostAddress); } return hostList; }
From source file:com.nec.strudel.tkvs.store.mongodb.MongodbStore.java
License:Apache License
@Override public MongoDbServer createDbServer(String dbName, Properties props) { ServerAddress addr = chooseOne(getServerAddressList(props)); LOGGER.info("using mongos@" + addr.getHost() + ":" + addr.getPort()); try {/* www . ja v a 2s.co m*/ return new MongoDbServer(dbName, addr, new BackoffPolicy(props)); } catch (IOException e) { throw new TkvStoreException("MongodbStore failed to create (IOException)", e); } }
From source file:com.nec.strudel.tkvs.store.mongodb.MongodbStore.java
License:Apache License
ServerAddress chooseOne(List<ServerAddress> addrs) { if (addrs.size() == 1) { return addrs.get(0); }//w ww . j a va2s. c o m InetAddress local = thisHost(); String host = local.getHostName(); for (ServerAddress a : addrs) { if (a.sameHost("localhost")) { return a; } else if (a.sameHost(host)) { return a; } else { try { InetAddress addr = InetAddress.getByName(a.getHost()); if (local.equals(addr)) { return a; } } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } Random rand = new Random(); return addrs.get(rand.nextInt(addrs.size())); }
From source file:com.sitewhere.mongodb.MongoDbClient.java
License:Open Source License
/** * Detects whether a replica set is configured and creates one if not. * //w w w . jav a 2 s. co m * @param addresses */ protected void doAutoConfigureReplication(List<ServerAddress> addresses) throws SiteWhereException { // Only connect to primary for sending commands. MongoClient primary = getPrimaryConnection(addresses); // Check for existing replica set configuration. getLogger().info("Checking for existing replica set..."); try { Document result = primary.getDatabase("admin").runCommand(new BasicDBObject("replSetGetStatus", 1)); if (result.getDouble("ok") == 1) { getLogger().warn("Replica set already configured. Skipping auto-configuration."); return; } } catch (MongoCommandException e) { getLogger().info("Replica set was not configured."); } // Create configuration for new replica set. try { getLogger().info("Configuring new replica set '" + getConfiguration().getReplicaSetName() + "'."); BasicDBObject config = new BasicDBObject("_id", getConfiguration().getReplicaSetName()); List<BasicDBObject> servers = new ArrayList<BasicDBObject>(); // Create list of members in replica set. int index = 0; for (final ServerAddress address : addresses) { BasicDBObject server = new BasicDBObject("_id", index); server.put("host", (address.getHost() + ":" + address.getPort())); // First server in list is preferred primary. if (index == 0) { server.put("priority", 10); } servers.add(server); index++; } config.put("members", servers); // Send command. Document result = primary.getDatabase("admin").runCommand(new BasicDBObject("replSetInitiate", config)); if (result.getDouble("ok") != 1) { throw new SiteWhereException("Unable to auto-configure replica set.\n" + result.toJson()); } getLogger().info( "Replica set '" + getConfiguration().getReplicaSetName() + "' creation command successful."); } finally { primary.close(); } }
From source file:com.tomtom.services.notifications.dao.DaoModule.java
License:Apache License
@Nonnull @Provides/*from w ww.j av a2 s .c om*/ @Singleton public static MongoDB provideMongoDB(@Nonnull final DatabaseProperties properties) { assert properties != null; try { LOG.info("provideMongoDB: Creating MongoDB connection: {}", properties.getServers()); final Mongo mongo = MongoConnectionCache.getMongoDB(properties.getServers(), MONGODB_TIMEOUT_SECS, properties.getUserName(), properties.getDatabase(), properties.getPassword()); // The address may not be available yet, if we connect like this. final ServerAddress address = mongo.getAddress(); LOG.info("provideMongoDB: MongoDB connection established: {}", properties.getDatabase() + " at " + properties.getServers() + " [master:" + ((address == null) ? "(unknown)" : (address.getHost() + ':' + mongo.getAddress().getPort())) + "] (MongoDB version " + ')'); final MongoDB db = getDB(mongo, properties.getDatabase(), "", properties.getUserName(), properties.getPassword()); return db; } catch (final UnknownHostException e) { throw new MongoDBConnectionException("Cannot find any of MongoDB servers: " + properties.getServers(), e); } }
From source file:com.torodb.testing.mongodb.docker.ServerAddressConverter.java
License:Apache License
public static HostAndPort convert(ServerAddress address) { return HostAndPort.fromParts(address.getHost(), address.getPort()); }
From source file:de.otto.mongodb.profiler.web.ConnectionsPageViewModel.java
License:Apache License
public String serversAsString(ProfiledConnection connection) { final StringBuilder sb = new StringBuilder(); for (ServerAddress serverAddress : connection.getConfiguration().getServerAddresses()) { if (sb.length() > 0) { sb.append(", "); }/* w w w . j ava 2 s .c om*/ sb.append(serverAddress.getHost()); if (serverAddress.getPort() != 27017) { sb.append(":").append(serverAddress.getPort()); } } return sb.toString(); }