Example usage for com.mongodb MongoClient getAddress

List of usage examples for com.mongodb MongoClient getAddress

Introduction

In this page you can find the example usage for com.mongodb MongoClient getAddress.

Prototype

@Deprecated
@SuppressWarnings("deprecation")
@Nullable
public ServerAddress getAddress() 

Source Link

Document

Gets the address of the current master

Usage

From source file:org.apache.rya.indexing.export.ITBase.java

License:Apache License

/**
 * @return A new {@link MongoClient}.  Note: This does not have RYA installed.
 * @throws MongoException//from w  w  w  .  j  a  v  a 2 s .  co  m
 * @throws InferenceEngineException
 * @throws RyaDAOException
 * @throws AccumuloSecurityException
 * @throws AccumuloException
 * @throws RepositoryException
 * @throws NumberFormatException
 * @throws IOException
 * @throws SailException
 */
public static MongoClient getNewMongoResources(final String ryaInstanceName)
        throws MongoException, NumberFormatException, RepositoryException, AccumuloException,
        AccumuloSecurityException, RyaDAOException, InferenceEngineException, IOException, SailException {
    // Initialize the test mongo that will be used to host rya.
    final MongodForTestsFactory mongodTestFactory = new MongodForTestsFactory();
    final MongoClient newClient = mongodTestFactory.newMongo();
    clients.add(newClient);
    final String host = newClient.getAddress().getHost();
    final int port = newClient.getAddress().getPort();
    final RyaSailRepository newRepo = setupRya(ryaInstanceName, host, port, newClient);
    ryaRepos.add(newRepo);
    return newClient;
}

From source file:org.apache.rya.indexing.geoExamples.RyaMongoGeoDirectExample.java

License:Apache License

private static Configuration getConf() throws IOException {

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

    if (USE_MOCK) {
        mock = EmbeddedMongoFactory.newFactory();
        MongoClient c = mock.newMongoClient();
        ServerAddress address = c.getAddress();
        String url = address.getHost();
        String port = Integer.toString(address.getPort());
        c.close();/*from   w  ww.ja  va 2  s . c  o m*/
        builder.setMongoHost(url).setMongoPort(port);
    } else {
        // User name and password must be filled in:
        builder = builder.setMongoUser("fill this in").setMongoPassword("fill this in")
                .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:org.apache.rya.indexing.mongodb.AbstractMongoIndexer.java

License:Apache License

@VisibleForTesting
public void initIndexer(final Configuration conf, final MongoClient client) {
    setClient(client);/*from ww w .  ja va 2  s . c om*/
    final ServerAddress address = client.getAddress();
    conf.set(MongoDBRdfConfiguration.MONGO_INSTANCE, address.getHost());
    conf.set(MongoDBRdfConfiguration.MONGO_INSTANCE_PORT, Integer.toString(address.getPort()));
    setConf(conf);
    if (!isInit) {
        init();
        isInit = true;
    }
}

From source file:org.auraframework.test.perf.util.PerfResultsUtil.java

License:Apache License

public static void writeToDb(PerfExecutorTestCase test, String testName, String dbURI, PerfMetrics metrics,
        String traceLog) {/*from w  ww.j a  v  a 2s.  c o  m*/
    try {
        MongoClient mongo = getMongoClient(dbURI);
        if (mongo != null) {
            LOG.info("Writing perf results into mongo db at: " + mongo.getAddress());
            MongoDatabase db = mongo.getDatabase("performance");
            MongoCollection<Document> runs = db.getCollection("testRun");
            JSONObject json = metrics.toJSONObject();
            Document doc = Document.parse(json.toString());

            doc.append("timeline", traceLog);
            doc.append("testName", testName);
            doc.append("transaction", Document.parse((metrics.getMetricsServiceTransaction()).toString()));
            doc.append("commonMetrics", Document.parse((metrics.getCommonMetrics()).toString()));
            doc.append("customMetrics", Document.parse((metrics.getCustomMetrics()).toString()));
            doc.append("run", RUN_TIME);
            runs.insertOne(doc);
            exportToCsv(test, doc);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.mongeez.dao.impl.MongeezDaoImpl.java

License:Apache License

public MongeezDaoImpl(MongoClient mongo, String databaseName) {

    mongoScope = MongoRuntime.createMongoScope();

    db = mongo.getDB(databaseName);/*w  w  w .  java 2 s .  com*/
    final String script = String.format("db = connect('%s:%s/%s')", mongo.getAddress().getHost(),
            mongo.getAddress().getPort(), databaseName);
    MongoRuntime.call(new MongoScriptAction(mongoScope, "connect", script));
    configure();
}

From source file:org.s1.mongodb.MongoDBConnectionHelper.java

License:Apache License

/**
 *
 * @param instance/*w  w w  .  j av a2s . c  o  m*/
 */
private static synchronized void initialize(String instance) {
    if (!connections.containsKey(instance)) {

        Map<String, Object> mopt = Options.getStorage().getMap(OPTIONS);
        Map<String, Object> m = Objects.get(mopt, "connections." + instance);
        if (Objects.isNullOrEmpty(m)) {
            m = Objects.get(mopt, "connections." + DEFAULT_INSTANCE);
        }

        MongoClientOptions.Builder b = MongoClientOptions.builder();
        MongoClientOptions def_opt = MongoClientOptions.builder().build();
        b.connectionsPerHost(Objects.get(m, "connectionsPerHost", def_opt.getConnectionsPerHost()));
        b.autoConnectRetry(Objects.get(m, "autoConnectRetry", def_opt.isAutoConnectRetry()));
        b.connectTimeout(Objects.get(m, "connectTimeout", def_opt.getConnectTimeout()));
        b.socketKeepAlive(Objects.get(m, "socketKeepAlive", def_opt.isSocketKeepAlive()));
        b.socketTimeout(Objects.get(m, "socketTimeout", def_opt.getSocketTimeout()));
        b.maxAutoConnectRetryTime(
                Objects.get(m, "maxAutoConnectRetryTime", def_opt.getMaxAutoConnectRetryTime()));
        b.maxWaitTime(Objects.get(m, "maxWaitTime", def_opt.getMaxWaitTime()));
        b.threadsAllowedToBlockForConnectionMultiplier(
                Objects.get(m, "threadsAllowedToBlockForConnectionMultiplier",
                        def_opt.getThreadsAllowedToBlockForConnectionMultiplier()));
        b.writeConcern(WriteConcern.FSYNC_SAFE);
        MongoClientOptions opt = b.build();

        MongoClient cl = null;
        try {
            cl = new MongoClient(
                    new ServerAddress(Objects.get(m, "host", "localhost"), Objects.get(m, "port", 27017)), opt);
        } catch (UnknownHostException e) {
            throw S1SystemError.wrap(e);
        }

        String dbName = Objects.get(m, "name");
        if (Objects.isNullOrEmpty(dbName))
            throw new S1SystemError("Cannot initialize MongoDB connection, because name is not set");

        DB db = cl.getDB(dbName);

        String user = Objects.get(m, "user");
        String password = Objects.get(m, "password");
        if (!Objects.isNullOrEmpty(user)) {
            if (!db.authenticate(user, password.toCharArray())) {
                throw new S1SystemError(
                        "Cannot authenticate MongoDB connection " + dbName + " with user " + user);
            }
        }
        LOG.info("MongoDB connected " + cl.getAddress().getHost() + ":" + cl.getAddress().getPort());

        connections.put(instance, db);
    }
}

From source file:org.springframework.statemachine.buildtests.tck.mongodb.MongoDbRule.java

License:Apache License

@Override
public Statement apply(Statement base, Description description) {
    MongoClient client = null;
    try {/*  ww w  . ja va2 s.  com*/
        client = new MongoClient(new ServerAddress(), MongoClientOptions.builder().connectTimeout(50).build());
        client.getAddress();
    } catch (Exception e) {
        return super.apply(new Statement() {
            @Override
            public void evaluate() throws Throwable {
            }
        }, Description.EMPTY);
    } finally {
        if (client != null) {
            client.close();
        }
    }
    return super.apply(base, description);
}

From source file:org.wso2.carbon.datasource.reader.mongo.MongoDataSourceReader.java

License:Open Source License

@Override
public boolean testDataSourceConnection(String xmlConfig) throws DataSourceException {
    MongoClient mongoClient = (MongoClient) this.createDataSource(xmlConfig, true);
    boolean status = false;
    if (mongoClient.getAddress() != null) {
        status = true;//from w  w w  . j  av a2 s. com
    }
    mongoClient.close();
    return status;
}