Package com.mongodb

Examples of com.mongodb.ServerAddress


            MongoDBRiverDefinition definition = MongoDBRiverDefinition.parseSettings(riverName.name(),
                    RiverIndexName.Conf.DEFAULT_INDEX_NAME, riverSettings, scriptService);
            Assert.assertNotNull(definition);

            Assert.assertEquals(2, definition.getMongoServers().size());
            ServerAddress serverAddress = definition.getMongoServers().get(0);
            Assert.assertEquals(serverAddress.getHost(), "127.0.0.1");
            Assert.assertEquals(serverAddress.getPort(), MongoDBRiverDefinition.DEFAULT_DB_PORT);
            serverAddress = definition.getMongoServers().get(1);
            Assert.assertEquals(serverAddress.getHost(), "localhost");
            Assert.assertEquals(serverAddress.getPort(), MongoDBRiverDefinition.DEFAULT_DB_PORT);

        } catch (Throwable t) {
            Assert.fail("testLoadMongoDBRiverDefinitionIssue159 failed", t);
        }
    }
View Full Code Here


                    for (Map<String, Object> feed : feeds) {
                        mongoHost = XContentMapValues.nodeStringValue(feed.get(HOST_FIELD), null);
                        mongoPort = XContentMapValues.nodeIntegerValue(feed.get(PORT_FIELD), DEFAULT_DB_PORT);
                        logger.trace("Server: " + mongoHost + " - " + mongoPort);
                        try {
                            mongoServers.add(new ServerAddress(mongoHost, mongoPort));
                        } catch (UnknownHostException uhEx) {
                            logger.warn("Cannot add mongo server {}:{}", uhEx, mongoHost, mongoPort);
                        }
                    }
                }
            } else {
                mongoHost = XContentMapValues.nodeStringValue(mongoSettings.get(HOST_FIELD), DEFAULT_DB_HOST);
                mongoPort = XContentMapValues.nodeIntegerValue(mongoSettings.get(PORT_FIELD), DEFAULT_DB_PORT);
                try {
                    mongoServers.add(new ServerAddress(mongoHost, mongoPort));
                } catch (UnknownHostException uhEx) {
                    logger.warn("Cannot add mongo server {}:{}", uhEx, mongoHost, mongoPort);
                }
            }
            builder.mongoServers(mongoServers);

            MongoClientOptions.Builder mongoClientOptionsBuilder = MongoClientOptions.builder()
                    .socketKeepAlive(true);

            // MongoDB options
            if (mongoSettings.containsKey(OPTIONS_FIELD)) {
                Map<String, Object> mongoOptionsSettings = (Map<String, Object>) mongoSettings.get(OPTIONS_FIELD);
                logger.trace("mongoOptionsSettings: " + mongoOptionsSettings);
                builder.mongoSecondaryReadPreference(XContentMapValues.nodeBooleanValue(
                        mongoOptionsSettings.get(SECONDARY_READ_PREFERENCE_FIELD), false));
                builder.connectTimeout(XContentMapValues.nodeIntegerValue(mongoOptionsSettings.get(CONNECT_TIMEOUT),
                        DEFAULT_CONNECT_TIMEOUT));
                builder.socketTimeout(XContentMapValues.nodeIntegerValue(mongoOptionsSettings.get(SOCKET_TIMEOUT), DEFAULT_SOCKET_TIMEOUT));
                builder.dropCollection(XContentMapValues.nodeBooleanValue(mongoOptionsSettings.get(DROP_COLLECTION_FIELD), false));
                String isMongos = XContentMapValues.nodeStringValue(mongoOptionsSettings.get(IS_MONGOS_FIELD), null);
                if (isMongos != null) {
                    builder.isMongos(Boolean.valueOf(isMongos));
                }
                builder.mongoUseSSL(XContentMapValues.nodeBooleanValue(mongoOptionsSettings.get(SSL_CONNECTION_FIELD), false));
                builder.mongoSSLVerifyCertificate(XContentMapValues.nodeBooleanValue(mongoOptionsSettings.get(SSL_VERIFY_CERT_FIELD), true));
                builder.advancedTransformation(XContentMapValues.nodeBooleanValue(mongoOptionsSettings.get(ADVANCED_TRANSFORMATION_FIELD),
                        false));
                builder.skipInitialImport(XContentMapValues.nodeBooleanValue(mongoOptionsSettings.get(SKIP_INITIAL_IMPORT_FIELD), false));
                builder.connectionsPerHost(XContentMapValues.nodeIntegerValue(mongoOptionsSettings.get(CONNECTIONS_PER_HOST), DEFAULT_CONNECTIONS_PER_HOST));
                builder.threadsAllowedToBlockForConnectionMultiplier(XContentMapValues.nodeIntegerValue(mongoOptionsSettings.get(THREADS_ALLOWED_TO_BLOCK_FOR_CONNECTION_MULTIPLIER),
                        DEFAULT_THREADS_ALLOWED_TO_BLOCK_FOR_CONNECTION_MULTIPLIER));

                mongoClientOptionsBuilder
                    .connectTimeout(builder.connectTimeout)
                    .socketTimeout(builder.socketTimeout)
                    .connectionsPerHost(builder.connectionsPerHost)
                    .threadsAllowedToBlockForConnectionMultiplier(builder.threadsAllowedToBlockForConnectionMultiplier);

                if (builder.mongoSecondaryReadPreference) {
                    mongoClientOptionsBuilder.readPreference(ReadPreference.secondaryPreferred());
                }

                if (builder.mongoUseSSL) {
                    mongoClientOptionsBuilder.socketFactory(getSSLSocketFactory());
                }

                if (mongoOptionsSettings.containsKey(PARENT_TYPES_FIELD)) {
                    Set<String> parentTypes = new HashSet<String>();
                    Object parentTypesSettings = mongoOptionsSettings.get(PARENT_TYPES_FIELD);
                    logger.trace("parentTypesSettings: " + parentTypesSettings);
                    boolean array = XContentMapValues.isArray(parentTypesSettings);

                    if (array) {
                        ArrayList<String> fields = (ArrayList<String>) parentTypesSettings;
                        for (String field : fields) {
                            logger.trace("Field: " + field);
                            parentTypes.add(field);
                        }
                    }

                    builder.parentTypes(parentTypes);
                }

                if (mongoOptionsSettings.containsKey(STORE_STATISTICS_FIELD)) {
                    Object storeStatistics = mongoOptionsSettings.get(STORE_STATISTICS_FIELD);
                    boolean object = XContentMapValues.isObject(storeStatistics);
                    if (object) {
                        Map<String, Object> storeStatisticsSettings = (Map<String, Object>) storeStatistics;
                        builder.storeStatistics(true);
                        builder.statisticsIndexName(XContentMapValues.nodeStringValue(storeStatisticsSettings.get(INDEX_OBJECT), riverName
                                + "-stats"));
                        builder.statisticsTypeName(XContentMapValues.nodeStringValue(storeStatisticsSettings.get(TYPE_FIELD), "stats"));
                    } else {
                        builder.storeStatistics(XContentMapValues.nodeBooleanValue(storeStatistics, false));
                        if (builder.storeStatistics) {
                            builder.statisticsIndexName(riverName + "-stats");
                            builder.statisticsTypeName("stats");
                        }
                    }
                }
                // builder.storeStatistics(XContentMapValues.nodeBooleanValue(mongoOptionsSettings.get(STORE_STATISTICS_FIELD),
                // false));
                builder.importAllCollections(XContentMapValues.nodeBooleanValue(mongoOptionsSettings.get(IMPORT_ALL_COLLECTIONS_FIELD),
                        false));
                builder.disableIndexRefresh(XContentMapValues.nodeBooleanValue(mongoOptionsSettings.get(DISABLE_INDEX_REFRESH_FIELD), false));
                builder.includeCollection(XContentMapValues.nodeStringValue(mongoOptionsSettings.get(INCLUDE_COLLECTION_FIELD), ""));

                if (mongoOptionsSettings.containsKey(INCLUDE_FIELDS_FIELD)) {
                    Set<String> includeFields = new HashSet<String>();
                    Object includeFieldsSettings = mongoOptionsSettings.get(INCLUDE_FIELDS_FIELD);
                    logger.trace("includeFieldsSettings: " + includeFieldsSettings);
                    boolean array = XContentMapValues.isArray(includeFieldsSettings);

                    if (array) {
                        ArrayList<String> fields = (ArrayList<String>) includeFieldsSettings;
                        for (String field : fields) {
                            logger.trace("Field: " + field);
                            includeFields.add(field);
                        }
                    }

                    if (!includeFields.contains(MongoDBRiver.MONGODB_ID_FIELD)) {
                        includeFields.add(MongoDBRiver.MONGODB_ID_FIELD);
                    }
                    builder.includeFields(includeFields);
                } else if (mongoOptionsSettings.containsKey(EXCLUDE_FIELDS_FIELD)) {
                    Set<String> excludeFields = new HashSet<String>();
                    Object excludeFieldsSettings = mongoOptionsSettings.get(EXCLUDE_FIELDS_FIELD);
                    logger.trace("excludeFieldsSettings: " + excludeFieldsSettings);
                    boolean array = XContentMapValues.isArray(excludeFieldsSettings);

                    if (array) {
                        ArrayList<String> fields = (ArrayList<String>) excludeFieldsSettings;
                        for (String field : fields) {
                            logger.trace("Field: " + field);
                            excludeFields.add(field);
                        }
                    }

                    builder.excludeFields(excludeFields);
                }

                if (mongoOptionsSettings.containsKey(INITIAL_TIMESTAMP_FIELD)) {
                    BSONTimestamp timeStamp = null;
                    try {
                        Map<String, Object> initalTimestampSettings = (Map<String, Object>) mongoOptionsSettings
                                .get(INITIAL_TIMESTAMP_FIELD);
                        String scriptType = "js";
                        if (initalTimestampSettings.containsKey(INITIAL_TIMESTAMP_SCRIPT_TYPE_FIELD)) {
                            scriptType = initalTimestampSettings.get(INITIAL_TIMESTAMP_SCRIPT_TYPE_FIELD).toString();
                        }
                        if (initalTimestampSettings.containsKey(INITIAL_TIMESTAMP_SCRIPT_FIELD)) {

                            ExecutableScript scriptExecutable = scriptService.executable(scriptType,
                                    initalTimestampSettings.get(INITIAL_TIMESTAMP_SCRIPT_FIELD).toString(), ScriptService.ScriptType.INLINE, Maps.newHashMap());
                            Object ctx = scriptExecutable.run();
                            logger.trace("initialTimestamp script returned: {}", ctx);
                            if (ctx != null) {
                                long timestamp = Long.parseLong(ctx.toString());
                                timeStamp = new BSONTimestamp((int) (new Date(timestamp).getTime() / 1000), 1);
                            }
                        }
                    } catch (Throwable t) {
                        logger.error("Could not set initial timestamp", t);
                    } finally {
                        builder.initialTimestamp(timeStamp);
                    }
                }
            }
            builder.mongoClientOptions(mongoClientOptionsBuilder.build());

            // Credentials
            if (mongoSettings.containsKey(CREDENTIALS_FIELD)) {
                String dbCredential;
                String mau = "";
                String map = "";
                String maad = "";
                String mlu = "";
                String mlp = "";
                String mlad = "";
                // String mdu = "";
                // String mdp = "";
                Object mongoCredentialsSettings = mongoSettings.get(CREDENTIALS_FIELD);
                boolean array = XContentMapValues.isArray(mongoCredentialsSettings);

                if (array) {
                    ArrayList<Map<String, Object>> credentials = (ArrayList<Map<String, Object>>) mongoCredentialsSettings;
                    for (Map<String, Object> credential : credentials) {
                        dbCredential = XContentMapValues.nodeStringValue(credential.get(DB_FIELD), null);
                        if (ADMIN_DB_FIELD.equals(dbCredential)) {
                            mau = XContentMapValues.nodeStringValue(credential.get(USER_FIELD), null);
                            map = XContentMapValues.nodeStringValue(credential.get(PASSWORD_FIELD), null);
                            maad = XContentMapValues.nodeStringValue(credential.get(AUTH_FIELD), null);
                        } else if (LOCAL_DB_FIELD.equals(dbCredential)) {
                            mlu = XContentMapValues.nodeStringValue(credential.get(USER_FIELD), null);
                            mlp = XContentMapValues.nodeStringValue(credential.get(PASSWORD_FIELD), null);
                            mlad = XContentMapValues.nodeStringValue(credential.get(AUTH_FIELD), null);
                            // } else {
                            // mdu = XContentMapValues.nodeStringValue(
                            // credential.get(USER_FIELD), null);
                            // mdp = XContentMapValues.nodeStringValue(
                            // credential.get(PASSWORD_FIELD), null);
                        }
                    }
                }
                builder.mongoAdminUser(mau);
                builder.mongoAdminPassword(map);
                builder.mongoAdminAuthDatabase(maad);
                builder.mongoLocalUser(mlu);
                builder.mongoLocalPassword(mlp);
                builder.mongoLocalAuthDatabase(mlad);
                // mongoDbUser = mdu;
                // mongoDbPassword = mdp;
            }

            builder.mongoDb(XContentMapValues.nodeStringValue(mongoSettings.get(DB_FIELD), riverName));
            builder.mongoCollection(XContentMapValues.nodeStringValue(mongoSettings.get(COLLECTION_FIELD), riverName));
            builder.mongoGridFS(XContentMapValues.nodeBooleanValue(mongoSettings.get(GRIDFS_FIELD), false));
            if (mongoSettings.containsKey(FILTER_FIELD)) {
                String filter = XContentMapValues.nodeStringValue(mongoSettings.get(FILTER_FIELD), "");
                filter = removePrefix("o.", filter);
                builder.mongoCollectionFilter(convertToBasicDBObject(filter));
                // DBObject bsonObject = (DBObject) JSON.parse(filter);
                // builder.mongoOplogFilter(convertToBasicDBObject(addPrefix("o.",
                // filter)));
                builder.mongoOplogFilter(convertToBasicDBObject(removePrefix("o.", filter)));
                // } else {
                // builder.mongoOplogFilter("");
            }

            if (mongoSettings.containsKey(SCRIPT_FIELD)) {
                String scriptType = "js";
                builder.script(mongoSettings.get(SCRIPT_FIELD).toString());
                if (mongoSettings.containsKey("scriptType")) {
                    scriptType = mongoSettings.get("scriptType").toString();
                } else if (mongoSettings.containsKey(SCRIPT_TYPE_FIELD)) {
                    scriptType = mongoSettings.get(SCRIPT_TYPE_FIELD).toString();
                }
                builder.scriptType(scriptType);
            }
        } else {
            mongoHost = DEFAULT_DB_HOST;
            mongoPort = DEFAULT_DB_PORT;
            try {
                mongoServers.add(new ServerAddress(mongoHost, mongoPort));
                builder.mongoServers(mongoServers);
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
            builder.mongoDb(riverName);
View Full Code Here

            logger.debug("getServerAddressForReplica - definition: {}", definition);
        }
        List<ServerAddress> servers = new ArrayList<ServerAddress>();
        for (String server : definition.split(",")) {
            try {
                servers.add(new ServerAddress(server));
            } catch (UnknownHostException uhEx) {
                logger.warn("failed to execute bulk", uhEx);
            }
        }
        return servers;
View Full Code Here

            for (String serverAuthority : StringUtils.split(_serverAuthorities, ',')) {
                String[] serverAuthorityParts = StringUtils.split(StringUtils.trimToEmpty(serverAuthority), ":");

                if (serverAuthorityParts.length == 1) {
                    serverAddressList.add(new ServerAddress(serverAuthorityParts[0]));
                } else {
                    serverAddressList.add(new ServerAddress(serverAuthorityParts[0], Integer.parseInt(serverAuthorityParts[1])));
                }
            }

            if (_sLog.isInfoEnabled()) {
                _sLog.info("Initializing mongoDB '" + _databaseName + "' with servers: " + serverAddressList);
View Full Code Here

      connectionStatus = DBStatus.DISCONNECTED;
    }

    Router router = new Router(connectionStatus);

    MongoClient client = new MongoClient(new ServerAddress("localhost", 27017));

    DB db = client.getDB("moview");

    Spark.get(router.getStartPage());
View Full Code Here

TOP

Related Classes of com.mongodb.ServerAddress

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.