Example usage for com.mongodb ServerAddress ServerAddress

List of usage examples for com.mongodb ServerAddress ServerAddress

Introduction

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

Prototype

public ServerAddress(@Nullable final String host, final int port) 

Source Link

Document

Creates a ServerAddress

Usage

From source file:org.hillview.storage.MongoDBLoader.java

License:Open Source License

public MongoDBLoader(JdbcConnectionInformation info) {
    super(Converters.checkNull(info.table), null);
    this.info = info;
    assert info.database != null;
    assert info.password != null;

    MongoCredential credential = MongoCredential.createCredential(info.user, info.database,
            info.password.toCharArray());
    ServerAddress address = new ServerAddress(info.host, info.port);
    MongoClientOptions options = MongoClientOptions.builder().build();
    MongoClient client = new MongoClient(address); //, credential, options);

    this.database = client.getDatabase(info.database);
    //this.oldDatabase = client.getDB(info.database);
}

From source file:org.icgc.dcc.storage.test.mongo.Mongo.java

License:Open Source License

private static MongoClient createClient(MongodProcess mongodProcess) throws UnknownHostException {
    val net = mongodProcess.getConfig().net();
    val address = new ServerAddress(net.getServerAddress(), net.getPort());

    return new MongoClient(address);
}

From source file:org.ikasan.component.endpoint.mongo.MongoClientConfiguration.java

License:BSD License

/**
 * Utility method for translating the list of connectionUrls (host:port) to host and port
 * into ServerAddress(es)/*from  w ww .j a v  a 2  s  .c om*/
 * @return
 */
protected List<ServerAddress> getServerAddresses() {
    List<ServerAddress> serverAddresses = new ArrayList<ServerAddress>();

    for (String connectionUrl : connectionUrls) {
        String[] properties = connectionUrl.split(":");
        try {
            serverAddresses.add(new ServerAddress(properties[0], Integer.valueOf(properties[1])));
        } catch (NumberFormatException | NullPointerException e) {
            logger.warn("connectionUrl is not valid [" + connectionUrl + "]");
        }
    }

    return serverAddresses;
}

From source file:org.ikasan.component.endpoint.mongo.test.EmbeddedMongo.java

License:BSD License

/**
 * This will check for the singleton instance and wont instantiate a new mongo database each time start is called by
 * a test./*from w w w. j a  v  a  2  s. com*/
 * 
 * @return
 */
public MongoClient start() {
    synchronized (EmbeddedMongo.class) {
        if (staticReference == null) {
            final Command command = Command.MongoD;
            MongodStarter runtime = null;
            final DownloadConfigBuilder downloadConfigBuilder = setupDownloadConfigBuilder(command);
            final IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder().defaults(command).artifactStore(
                    new ArtifactStoreBuilder().defaults(command).download(downloadConfigBuilder.build()))
                    .build();
            runtime = MongodStarter.getInstance(runtimeConfig);
            ;
            try {
                final MongodConfigBuilder mongodConfigBuilder = setupMongodConfigBuilder();
                mongodExecutable = runtime.prepare(mongodConfigBuilder.build());
                final MongodProcess mongodProcess = mongodExecutable.start();
                staticReference = this;
                mongoClient = new MongoClient(
                        new ServerAddress(mongodProcess.getConfig().net().getServerAddress(),
                                mongodProcess.getConfig().net().getPort()));
            } catch (final IOException e) {
                throw new RuntimeException("Unable to start embeddedMongo", e);
            }
        }
        return staticReference.mongoClient;
    }
}

From source file:org.infinispan.loaders.mongodb.MongoDBCacheStore.java

License:Open Source License

@Override
public void start() throws CacheLoaderException {
    super.start();
    try {/*from   w  ww  .  j a v  a  2s  .  c o m*/
        MongoClientOptions.Builder optionBuilder = new MongoClientOptions.Builder();
        optionBuilder.connectTimeout(this.cfg.getTimeout());

        WriteConcern writeConcern = new WriteConcern(this.cfg.getAcknowledgment());
        optionBuilder.writeConcern(writeConcern);

        log.connectingToMongo(this.cfg.getHost(), this.cfg.getPort(), this.cfg.getTimeout(),
                this.cfg.getAcknowledgment());

        ServerAddress serverAddress = new ServerAddress(this.cfg.getHost(), this.cfg.getPort());

        this.mongo = new MongoClient(serverAddress, optionBuilder.build());
    } catch (UnknownHostException e) {
        throw log.mongoOnUnknownHost(this.cfg.getHost());
    } catch (RuntimeException e) {
        throw log.unableToInitializeMongoDB(e);
    }
    mongoDb = extractDatabase();
    this.collection = mongoDb.getCollection(this.cfg.getCollectionName());

}

From source file:org.jboss.as.quickstarts.kitchensink.util.Resources.java

License:Apache License

@Produces
private MongoClient produceMongoClient() {
    List<DbServicePrefixMappingParser.DbServicePrefixMapping> mappings = dbServicePrefixMappingParser
            .parseDbServicePrefixMappingEnvVar(System.getenv(DB_SERVICE_PREFIX_MAPPING));
    for (DbServicePrefixMappingParser.DbServicePrefixMapping mapping : mappings) {
        if ("MONGODB".equals(mapping.getDatabaseType().toUpperCase())) {
            String hostname = System.getenv(mapping.getServiceName() + "_SERVICE_HOST");
            String port = System.getenv(mapping.getServiceName() + "_SERVICE_PORT");
            String database = System.getenv(mapping.getEnvPrefix() + "_DATABASE");
            String username = System.getenv(mapping.getEnvPrefix() + "_USERNAME");
            String password = System.getenv(mapping.getEnvPrefix() + "_PASSWORD");
            MongoCredential credential = MongoCredential.createCredential(username, database,
                    password.toCharArray());
            return new MongoClient(new ServerAddress(hostname, Integer.parseInt(port)),
                    Collections.singletonList(credential));
        }//w ww.  j a  v a 2  s .  c o  m
    }
    throw new IllegalStateException("No MongoDB mapping in " + DB_SERVICE_PREFIX_MAPPING);
}

From source file:org.jenkinsci.plugins.compatibilityaction.MongoDBHolderService.java

public MongoClient createClient() throws UnknownHostException {
    if (!StringUtils.isBlank(Secret.toString(password))) {
        return new MongoClient(new ServerAddress(host, port), Arrays.asList(
                MongoCredential.createCredential(user, database, Secret.toString(password).toCharArray())));
    } else {//from ww w .  j  a  v a2  s  .co m
        ServerAddress addr = new ServerAddress(host, port);
        return new MongoClient(addr);
    }
}

From source file:org.jeo.mongo.MongoOpts.java

License:Open Source License

public DB connect() throws IOException {
    ServerAddress server = new ServerAddress(host, port);
    Mongo mongo = new Mongo(server);
    DB database = mongo.getDB(db);/*from   w w w.  ja  v  a2  s .c  o m*/
    database.authenticate(user, passwd != null ? passwd.get() : new char[] {});

    return database;
}

From source file:org.jmingo.mongo.MongoDBFactory.java

License:Apache License

private Mongo create(MongoConfig config) {
    Mongo mongo;/* w ww  . ja  va2  s.com*/
    try {
        MongoClientOptions options = createOptions(config);
        mongo = new MongoClient(new ServerAddress(config.getDatabaseHost(), config.getDatabasePort()), options);
        WriteConcern writeConcern = WriteConcern.valueOf(config.getWriteConcern());
        mongo.setWriteConcern(writeConcern);
        LOGGER.debug("set '{}' write concern", writeConcern);
    } catch (UnknownHostException e) {
        throw new MongoConfigurationException(e);
    }
    return mongo;
}

From source file:org.kaaproject.kaa.server.appenders.mongo.appender.LogEventMongoDao.java

License:Apache License

/**
 * Create new instance of <code>LogEventMongoDao</code> using configuration instance of
 * <code>MongoDbConfig</code>.
 *
 * @param configuration the configuration of log event mongo dao, it contain server size,
 *                      credentials, max wait time, etc.
 *//*from www.j a  v  a2s .  c  o  m*/
@SuppressWarnings("deprecation")
public LogEventMongoDao(MongoDbConfig configuration) throws Exception {

    List<ServerAddress> seeds = new ArrayList<>(configuration.getMongoServers().size());
    for (MongoDbServer server : configuration.getMongoServers()) {
        seeds.add(new ServerAddress(server.getHost(), server.getPort()));
    }

    List<MongoCredential> credentials = new ArrayList<>();
    if (configuration.getMongoCredentials() != null) {
        for (MongoDBCredential credential : configuration.getMongoCredentials()) {
            credentials.add(MongoCredential.createMongoCRCredential(credential.getUser(),
                    configuration.getDbName(), credential.getPassword().toCharArray()));
        }
    }

    MongoClientOptions.Builder optionsBuilder = new MongoClientOptions.Builder();
    if (configuration.getConnectionsPerHost() != null) {
        optionsBuilder.connectionsPerHost(configuration.getConnectionsPerHost());
    }
    if (configuration.getMaxWaitTime() != null) {
        optionsBuilder.maxWaitTime(configuration.getMaxWaitTime());
    }
    if (configuration.getConnectionTimeout() != null) {
        optionsBuilder.connectTimeout(configuration.getConnectionTimeout());
    }
    if (configuration.getSocketTimeout() != null) {
        optionsBuilder.socketTimeout(configuration.getSocketTimeout());
    }
    if (configuration.getSocketKeepalive() != null) {
        optionsBuilder.socketKeepAlive(configuration.getSocketKeepalive());
    }

    MongoClientOptions options = optionsBuilder.build();
    mongoClient = new MongoClient(seeds, credentials, options);

    MongoDbFactory dbFactory = new SimpleMongoDbFactory(mongoClient, configuration.getDbName());

    MappingMongoConverter converter = new MappingMongoConverter(dbFactory, new MongoMappingContext());
    converter.setTypeMapper(new DefaultMongoTypeMapper(null));

    mongoTemplate = new MongoTemplate(dbFactory, converter);
    mongoTemplate.setWriteResultChecking(WriteResultChecking.EXCEPTION);
}