Example usage for com.mongodb ServerAddress getHost

List of usage examples for com.mongodb ServerAddress getHost

Introduction

In this page you can find the example usage for com.mongodb ServerAddress getHost.

Prototype

public String getHost() 

Source Link

Document

Gets the hostname

Usage

From source file:org.graylog2.system.stats.mongo.MongoProbe.java

License:Open Source License

public MongoStats mongoStats() {
    final List<ServerAddress> serverAddresses = mongoClient.getServerAddressList();
    final List<HostAndPort> servers = Lists.newArrayListWithCapacity(serverAddresses.size());
    for (ServerAddress serverAddress : serverAddresses) {
        servers.add(HostAndPort.fromParts(serverAddress.getHost(), serverAddress.getPort()));
    }/*from  www .j a  v  a 2s  . c om*/

    final DatabaseStats dbStats;
    final CommandResult dbStatsResult = db.command("dbStats");
    if (dbStatsResult.ok()) {
        final BasicDBObject extentFreeListMap = (BasicDBObject) dbStatsResult.get("extentFreeList");
        final DatabaseStats.ExtentFreeList extentFreeList = DatabaseStats.ExtentFreeList
                .create(extentFreeListMap.getInt("num"), extentFreeListMap.getInt("totalSize"));

        final BasicDBObject dataFileVersionMap = (BasicDBObject) dbStatsResult.get("dataFileVersion");
        final DatabaseStats.DataFileVersion dataFileVersion = DatabaseStats.DataFileVersion
                .create(dataFileVersionMap.getInt("major"), dataFileVersionMap.getInt("minor"));

        dbStats = DatabaseStats.create(dbStatsResult.getString("db"), dbStatsResult.getLong("collections"),
                dbStatsResult.getLong("objects"), dbStatsResult.getDouble("avgObjSize"),
                dbStatsResult.getLong("dataSize"), dbStatsResult.getLong("storageSize"),
                dbStatsResult.getLong("numExtents"), dbStatsResult.getLong("indexes"),
                dbStatsResult.getLong("indexSize"), dbStatsResult.getLong("fileSize"),
                dbStatsResult.getLong("nsSizeMB"), extentFreeList, dataFileVersion);
    } else {
        dbStats = null;
    }

    final ServerStatus serverStatus;
    final CommandResult serverStatusResult = adminDb.command("serverStatus");
    if (serverStatusResult.ok()) {
        final BasicDBObject connectionsMap = (BasicDBObject) serverStatusResult.get("connections");
        final ServerStatus.Connections connections = ServerStatus.Connections.create(
                connectionsMap.getInt("current"), connectionsMap.getInt("available"),
                connectionsMap.getLong("totalCreated"));

        final BasicDBObject networkMap = (BasicDBObject) serverStatusResult.get("network");
        final ServerStatus.Network network = ServerStatus.Network.create(networkMap.getInt("bytesIn"),
                networkMap.getInt("bytesOut"), networkMap.getInt("numRequests"));

        final BasicDBObject memoryMap = (BasicDBObject) serverStatusResult.get("mem");
        final ServerStatus.Memory memory = ServerStatus.Memory.create(memoryMap.getInt("bits"),
                memoryMap.getInt("resident"), memoryMap.getInt("virtual"), memoryMap.getBoolean("supported"),
                memoryMap.getInt("mapped"), memoryMap.getInt("mappedWithJournal"));

        serverStatus = ServerStatus.create(serverStatusResult.getString("host"),
                serverStatusResult.getString("version"), serverStatusResult.getString("process"),
                serverStatusResult.getLong("pid"), serverStatusResult.getInt("uptime"),
                serverStatusResult.getLong("uptimeMillis"), serverStatusResult.getInt("uptimeEstimate"),
                new DateTime(serverStatusResult.getDate("localTime")), connections, network, memory);
    } else {
        serverStatus = null;
    }

    // TODO Collection stats? http://docs.mongodb.org/manual/reference/command/collStats/

    return MongoStats.create(servers, buildInfo, hostInfo, serverStatus, dbStats);
}

From source file:org.springframework.data.document.mongodb.MongoFactoryBean.java

License:Apache License

public void afterPropertiesSet() throws Exception {
    // apply defaults - convenient when used to configure for tests
    // in an application context
    if (mongo == null) {

        if (host == null) {
            logger.warn("Property host not specified. Using default configuration");
            mongo = new Mongo();
        } else {/*from  w ww.  j  a  v a2s  . com*/
            ServerAddress defaultOptions = new ServerAddress();
            if (mongoOptions == null)
                mongoOptions = new MongoOptions();
            if (replicaPair != null) {
                if (replicaPair.size() < 2) {
                    throw new CannotGetMongoDbConnectionException(
                            "A replica pair must have two server entries");
                }
                mongo = new Mongo(replicaPair.get(0), replicaPair.get(1), mongoOptions);
            } else if (replicaSetSeeds != null) {
                mongo = new Mongo(replicaSetSeeds, mongoOptions);
            } else {
                String mongoHost = host != null ? host : defaultOptions.getHost();
                if (port != null) {
                    mongo = new Mongo(new ServerAddress(mongoHost, port), mongoOptions);
                } else {
                    mongo = new Mongo(mongoHost, mongoOptions);
                }
            }
        }
    }
}

From source file:org.springframework.data.mongodb.core.MongoClientFactoryBean.java

License:Apache License

private ServerAddress createConfiguredOrDefaultServerAddress() throws UnknownHostException {

    ServerAddress defaultAddress = new ServerAddress();

    return new ServerAddress(StringUtils.hasText(host) ? host : defaultAddress.getHost(),
            port != null ? port.intValue() : defaultAddress.getPort());
}

From source file:org.springframework.data.mongodb.core.MongoFactoryBean.java

License:Apache License

@Override
protected Mongo createInstance() throws Exception {

    Mongo mongo;//from   w ww .j  a  v a 2s. c om
    ServerAddress defaultOptions = new ServerAddress();

    if (mongoOptions == null) {
        mongoOptions = new MongoOptions();
    }

    if (!isNullOrEmpty(replicaPair)) {
        if (replicaPair.size() < 2) {
            throw new CannotGetMongoDbConnectionException("A replica pair must have two server entries");
        }
        mongo = new Mongo(replicaPair.get(0), replicaPair.get(1), mongoOptions);
    } else if (!isNullOrEmpty(replicaSetSeeds)) {
        mongo = new Mongo(replicaSetSeeds, mongoOptions);
    } else {
        String mongoHost = StringUtils.hasText(host) ? host : defaultOptions.getHost();
        mongo = port != null ? new Mongo(new ServerAddress(mongoHost, port), mongoOptions)
                : new Mongo(mongoHost, mongoOptions);
    }

    if (writeConcern != null) {
        mongo.setWriteConcern(writeConcern);
    }

    return mongo;
}

From source file:org.springframework.datastore.mapping.mongo.MongoDatastore.java

License:Apache License

public void afterPropertiesSet() throws Exception {
    if (this.mongo == null) {
        ServerAddress defaults = new ServerAddress();
        MongoFactoryBean dbFactory = new MongoFactoryBean();
        dbFactory.setHost(read(String.class, MONGO_HOST, connectionDetails, defaults.getHost()));
        dbFactory.setPort(read(Integer.class, MONGO_PORT, connectionDetails, defaults.getPort()));
        if (mongoOptions != null) {
            dbFactory.setMongoOptions(mongoOptions);
        }/*from   w ww . ja va 2 s.  co  m*/
        dbFactory.afterPropertiesSet();

        this.mongo = dbFactory.getObject();
    }

    for (PersistentEntity entity : mappingContext.getPersistentEntities()) {
        createMongoTemplate(entity, mongo);
    }
}