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:MongoRyaDirectExample.java

License:Apache License

private static Configuration getConf() throws IOException {

    final MongoDBRdfConfiguration conf = new MongoDBRdfConfiguration();
    conf.set(ConfigUtils.USE_MONGO, "true");

    if (USE_MOCK) {
        mock = MockMongoFactory.newFactory();
        final MongoClient c = mock.newMongoClient();
        final ServerAddress address = c.getAddress();
        final String url = address.getHost();
        final String port = Integer.toString(address.getPort());
        c.close();/*from www . j  a  v a  2 s .  c o m*/
        conf.set(MongoDBRdfConfiguration.MONGO_INSTANCE, url);
        conf.set(MongoDBRdfConfiguration.MONGO_INSTANCE_PORT, port);
    } else {
        // User name and password must be filled in:
        conf.set(MongoDBRdfConfiguration.MONGO_USER, "fill this in");
        conf.set(MongoDBRdfConfiguration.MONGO_USER_PASSWORD, "fill this in");
        conf.set(MongoDBRdfConfiguration.MONGO_INSTANCE, MONGO_INSTANCE_URL);
        conf.set(MongoDBRdfConfiguration.MONGO_INSTANCE_PORT, MONGO_INSTANCE_PORT);
    }
    conf.set(MongoDBRdfConfiguration.MONGO_DB_NAME, MONGO_DB);
    conf.set(MongoDBRdfConfiguration.MONGO_COLLECTION_PREFIX, MONGO_COLL_PREFIX);
    conf.set(ConfigUtils.GEO_PREDICATES_LIST, "http://www.opengis.net/ont/geosparql#asWKT");
    //        conf.set(ConfigUtils.USE_GEO, "true");
    conf.set(ConfigUtils.USE_FREETEXT, "true");
    conf.setTablePrefix(MONGO_COLL_PREFIX);
    conf.set(ConfigUtils.GEO_PREDICATES_LIST, GeoConstants.GEO_AS_WKT.stringValue());
    conf.set(ConfigUtils.FREETEXT_PREDICATES_LIST, RDFS.LABEL.stringValue());
    conf.set(ConfigUtils.FREETEXT_PREDICATES_LIST, RDFS.LABEL.stringValue());
    conf.set(RdfCloudTripleStoreConfiguration.CONF_INFER, Boolean.toString(USE_INFER));
    return conf;
}

From source file:InferenceExamples.java

License:Apache License

private static Configuration getConf() throws IOException {

    // MongoDBIndexingConfigBuilder builder = MongoIndexingConfiguration.builder()
    //     .setUseMockMongo(USE_MOCK).setUseInference(USE_INFER).setAuths("U");
    MongoDBIndexingConfigBuilder builder = MongoIndexingConfiguration.builder()
            .setUseMockMongo(USE_EMBEDDED_MONGO).setUseInference(true).setAuths("U");

    if (USE_EMBEDDED_MONGO) {
        final MongoClient c = EmbeddedMongoFactory.newFactory().newMongoClient();
        final ServerAddress address = c.getAddress();
        final String url = address.getHost();
        final String port = Integer.toString(address.getPort());
        c.close();/*  w  w  w .jav a  2  s .  c  o m*/
        builder.setMongoHost(url).setMongoPort(port);
    } else {
        // User name and password must be filled in:
        builder = builder.setMongoUser(MongoUserName).setMongoPassword(MongoUserPassword)
                .setMongoHost(MONGO_INSTANCE_URL).setMongoPort(MONGO_INSTANCE_PORT);
    }

    return builder.setMongoDBName(MONGO_DB).setMongoCollectionPrefix(MONGO_COLL_PREFIX)
            .setUseMongoFreetextIndex(true).setMongoFreeTextPredicates(RDFS.LABEL.stringValue()).build();

}

From source file:brooklyn.entity.nosql.mongodb.MongoDBClientSupport.java

License:Apache License

private HostAndPort getServerHostAndPort() {
    ServerAddress address = getServerAddress();
    return HostAndPort.fromParts(address.getHost(), address.getPort());
}

From source file:com.ca.apm.mongo.Collector.java

License:Open Source License

public MetricFeedBundle makeMetrics(final CommandResult mcr) throws Exception {
    MetricFeedBundle mfb = new MetricFeedBundle();
    ServerAddress sa = mcr.getServerUsed();
    // Add a "mongo segment" to the metric path to insure that
    // mongo metrics are grouped/segregated in the metric browser.
    // Note that we can't use ":" in that segment though
    final String basePath = String.format("MongoDB@%s;%d", sa.getHost(), sa.getPort());
    makeMetrics(mfb, basePath, mcr);/*from w  w  w . j  a  v a  2  s .c  o  m*/
    return mfb;
}

From source file:com.ebay.cloud.cms.mongo.MongoDataSource.java

License:Apache License

public MongoDataSource(String servers, int connectionsPerHost, ReadPreference readPreference,
        CMSDBConfig config) {//from   w  ww.ja  v a2 s.  co m
    this.addrs = parseServerString(servers);

    Collections.sort(addrs, new Comparator<ServerAddress>() {

        @Override
        public int compare(ServerAddress s1, ServerAddress s2) {
            int result = s1.getHost().compareTo(s2.getHost());
            if (result != 0) {
                return result;
            } else {
                return s1.getPort() - s2.getPort();
            }
        }

    });

    MongoClientOptions.Builder builder = MongoClientOptions.builder();
    builder.socketKeepAlive(false);
    builder.connectionsPerHost(connectionsPerHost);
    if (readPreference != null) {
        builder.readPreference(readPreference);
    }
    // set socket timeout
    if (config != null) {
        Integer socketTimeOut = (Integer) config.get(CMSDBConfig.MONGO_CONNECTION_SOCKET_TIMEOUT);
        builder.socketTimeout(socketTimeOut);
    }
    MongoClientOptions mongoOptions = builder.build();
    this.mongo = new MongoClient(addrs, mongoOptions);
}

From source file:com.ebay.cloud.cms.mongo.MongoDataSource.java

License:Apache License

@Override
public int hashCode() {
    int hash = 0;
    for (ServerAddress a : addrs) {
        hash += a.getHost().hashCode();
        hash += a.getPort();//from   w w w .j av a  2s .c om
    }

    if (hash == 0) {
        return "".hashCode();
    }

    return hash;
}

From source file:com.ebay.cloud.cms.sysmgmt.monitor.metrics.MongoMetric.java

License:Apache License

private void snapshot() {
    MongoClient client = dataSource.getMongoInstance();

    Map<String, Object> status = new TreeMap<String, Object>();
    // update driver
    status.put(MetricConstants.MONGO_DRIVER_VERSION, client.getVersion());
    // update status
    ReplicaSetStatus rss = client.getReplicaSetStatus();

    String master = NOT_FOUND;//w ww . j  a va  2s .  c o  m
    if (rss != null) {
        status.put(MetricConstants.REPL_STATUS, rss.toString());
        ServerAddress masterServer = rss.getMaster();
        if (masterServer != null) {
            master = masterServer.getHost();
        }
    } else {
        status.put(MetricConstants.REPL_STATUS, "no repl set found!");
    }
    // update mongo cluster master
    status.put(MetricConstants.REPL_MASTER, master);
    // list mongo databases
    Map<String, Object> databaseSizeMap = listDatabases(client);
    String databases = StringUtils.join(databaseSizeMap.keySet(), ',');
    status.put(MetricConstants.REPL_DATABASES, databases);
    status.put(MetricConstants.MONGO_DB_SIZE, databaseSizeMap);

    mongoStatus = status;
}

From source file:com.edgytech.umongo.MongoPanel.java

License:Apache License

private String getServerAddressString(ServerAddress addr) {
    String ip = "unknown";
    try {//from ww w .ja  v a  2  s .co m
        ip = addr.getSocketAddress().getAddress().toString();
    } catch (UnknownHostException ex) {
        Logger.getLogger(MongoPanel.class.getName()).log(Level.WARNING, null, ex);
    }
    return addr.getHost() + ":" + addr.getPort() + " (" + ip + ")";
}

From source file:com.edgytech.umongo.RouterPanel.java

License:Apache License

@Override
protected void updateComponentCustom(JPanel comp) {
    try {//from ww  w . j av  a  2 s.c  om
        ServerAddress addr = getRouterNode().getAddress();
        setStringFieldValue(Item.host, addr.getHost() + ":" + addr.getPort());
        setStringFieldValue(Item.address, addr.getSocketAddress().toString());
        ((DocField) getBoundUnit(Item.shards)).setDoc(((RouterNode) node).shards);
    } catch (Exception e) {
        UMongo.instance.showError(this.getClass().getSimpleName() + " update", e);
    }
}

From source file:com.github.nlloyd.hornofmongo.adaptor.Mongo.java

License:Open Source License

@SuppressWarnings("unchecked")
@JSConstructor//from w w  w . j a  va 2 s. c o  m
public Mongo(final Object host) throws UnknownHostException {
    super();
    if (host instanceof Undefined)
        this.hosts = Collections.singletonList(new ServerAddress("localhost", ServerAddress.defaultPort()));
    else if (host instanceof com.mongodb.Mongo) {
        this.innerMongo = (com.mongodb.Mongo) host;
        this.hosts = this.innerMongo.getAllAddress();
        this.mongoOptions = this.innerMongo.getMongoOptions();
        // now get the query options, not same as MongoOptions
        this.options = this.innerMongo.getOptions();
    } else if (host instanceof List<?>)
        // TODO check if we get a list of ServerAddresses or something else
        this.hosts = (List<ServerAddress>) host;
    else {
        String hostsString = Context.toString(host);
        if (hostsString.startsWith(MONGO_CLIENT_URI_PREFIX))
            hostsString = hostsString.substring(MONGO_CLIENT_URI_PREFIX.length());
        String[] hostStrings = hostsString.split(",");
        this.hosts = new ArrayList<ServerAddress>(hostStrings.length);
        for (String hostString : hostStrings) {
            if (hostString.indexOf(':') > -1) {
                String[] hostBits = hostString.split(":");
                this.hosts.add(new ServerAddress(hostBits[0], Integer.valueOf(hostBits[1])));
            } else
                this.hosts.add(new ServerAddress(hostString, ServerAddress.defaultPort()));
        }
    }

    StringBuilder hostStringBuilder = new StringBuilder();
    if (!(host instanceof Undefined)) {
        for (ServerAddress serverAddress : this.hosts) {
            if (hostStringBuilder.length() > 0)
                hostStringBuilder.append(",");
            hostStringBuilder.append(serverAddress.getHost()).append(":").append(serverAddress.getPort());
        }
    } else
        hostStringBuilder.append("127.0.0.1");
    put("host", this, hostStringBuilder.toString());
}