com.mongodb.MongoClientURI Java Examples

The following examples show how to use com.mongodb.MongoClientURI. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MongoLogBenchmark.java    From tomcat-mongo-access-log with Apache License 2.0 6 votes vote down vote up
@Override
protected void setUpValve(Tomcat tomcat) throws UnknownHostException {
  // remove AccessLogValve
  for (Valve vl : tomcat.getHost().getPipeline().getValves()) {
    if (vl.getClass().equals(AccessLogValve.class)) {
      tomcat.getHost().getPipeline().removeValve(vl);
    }
  }
  
  mongoClient = new MongoClient(new MongoClientURI(url));
  db = mongoClient.getDB(dbName);
  
  MongoAccessLogValve mavl = new MongoAccessLogValve();
  mavl.setUri(url);
  mavl.setDbName(dbName);
  mavl.setCollName(collName);
  mavl.setPattern(pattern);
  
  tomcat.getHost().getPipeline().addValve(mavl);
}
 
Example #2
Source File: DefaultChannelService.java    From hvdf with Apache License 2.0 6 votes vote down vote up
public DefaultChannelService(
		final MongoClientURI dbUri, 
		final ChannelServiceConfiguration config){
	super(dbUri, config);
	
	this.config = config;
	
	if(this.config.channel_task_thread_pool_size > 0){
		this.taskExecutor = Executors.newScheduledThreadPool(
				this.config.channel_task_thread_pool_size);
	}
	else {
		this.taskExecutor = null;
	}
	
}
 
Example #3
Source File: MongoDBTest.java    From jlogstash-input-plugin with Apache License 2.0 6 votes vote down vote up
@Override
public void prepare() {
    // 获取是否需要转换Binary
    if (bin_fields != null && bin_fields.size() > 0) {
        convertBin = true;
    }

    // 准备since_time
    prepareSinceTime();

    // 将filter查询语句转换为Document对象
    filterDocument = parseFilterDocument(filter);

    // 连接client
    mongoClient = new MongoClient(new MongoClientURI(uri));
    database = mongoClient.getDatabase(db_name);
    coll = database.getCollection(collection);
}
 
Example #4
Source File: GetMongoIT.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() {
    runner = TestRunners.newTestRunner(GetMongo.class);
    runner.setVariable("uri", MONGO_URI);
    runner.setVariable("db", DB_NAME);
    runner.setVariable("collection", COLLECTION_NAME);
    runner.setProperty(AbstractMongoProcessor.URI, "${uri}");
    runner.setProperty(AbstractMongoProcessor.DATABASE_NAME, "${db}");
    runner.setProperty(AbstractMongoProcessor.COLLECTION_NAME, "${collection}");
    runner.setProperty(GetMongo.USE_PRETTY_PRINTING, GetMongo.YES_PP);
    runner.setIncomingConnection(false);

    mongoClient = new MongoClient(new MongoClientURI(MONGO_URI));

    MongoCollection<Document> collection = mongoClient.getDatabase(DB_NAME).getCollection(COLLECTION_NAME);
    collection.insertMany(DOCUMENTS);
}
 
Example #5
Source File: MongoWrapperDefaultHandler.java    From DBus with Apache License 2.0 6 votes vote down vote up
/**
 * 根据oid去数据库回查数据
 *
 * @param oid
 * @return
 */
private Document fetchData(String schemaName, String tableName, String oid) {
    Document result = null;
    DbusDatasource datasource = GlobalCache.getDatasource();
    MongoClientURI uri = new MongoClientURI(datasource.getMasterUrl());
    MongoClient client = new MongoClient(uri);
    MongoDatabase database = client.getDatabase(schemaName);
    MongoCollection<Document> collection = database.getCollection(tableName);
    MongoCursor<Document> cursor = collection.find(new BasicDBObject().append("_id", new ObjectId(oid))).iterator();
    if (cursor.hasNext()) {
        result = cursor.next();
    } else {
        logger.error("get source data error. schemaName:{}, tableName:{}, oid:{}", schemaName, tableName, oid);
    }
    client.close();
    return result;

}
 
Example #6
Source File: MongoFilterHandlerTestIT.java    From ingestion with Apache License 2.0 6 votes vote down vote up
@Test
public void getLastCheckPointWithValidCollection() throws UnknownHostException, ParseException {
    String mongoHost = getMongoHost();
    when(context.get("field"))
            .thenReturn("date");
    when(context.get("type"))
            .thenReturn("java.util.Date");
    when(context.get("format"))
            .thenReturn(DATE_FORMAT_YYYY_MM_DD_T_HH_MM_SS_XXX);
    when(context.get("mongoUri"))
            .thenReturn("mongodb://" + mongoHost + "/" + DB_TEST + ".validCollection");
    handler = new MongoFilterHandler();
    mongoClient = new MongoClient(new MongoClientURI("mongodb://" + mongoHost));
    mongoClient.getDB(DB_TEST).createCollection("validCollection", null);
    mongoClient.getDB(DB_TEST).getCollection("validCollection").save(populateDocument());
    when(context.get("filterType"))
            .thenReturn("com.stratio.ingestion.source.rest.url.filter.type.DateCheckpointType");
    handler = spy(new MongoFilterHandler());
    doReturn(context).when(handler).loadCheckpointContext(context);
    handler.configure(context);

    final Map<String, String> lastCheckpoint = handler.getLastFilter(context);
    assertThat(lastCheckpoint).isNotNull();
}
 
Example #7
Source File: MongoDriver.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Connection connect(String url, Properties info) throws SQLException {
	MongoClientURI mcu = null;
	if ((mcu = parseURL(url, info)) == null) {
		return null;
	}
	
	MongoConnection result = null;
	//System.out.print(info);
	try{
		result = new MongoConnection(mcu, url);
	}catch (Exception e){
		throw new SQLException("Unexpected exception: " + e.getMessage(), e);
	}
	
	return result;
}
 
Example #8
Source File: Validation.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
public ValidationResult validate(String subject, String value, ValidationContext context) {
    final ValidationResult.Builder builder = new ValidationResult.Builder();
    builder.subject(subject).input(value);

    if (context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(value)) {
        return builder.valid(true).explanation("Contains Expression Language").build();
    }

    String reason = null;
    try {
        new MongoClientURI(value);
    } catch (final Exception e) {
        reason = e.getLocalizedMessage();
    }

    return builder.explanation(reason).valid(reason == null).build();
}
 
Example #9
Source File: ProfileActivity.java    From medical-data-android with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected Integer doInBackground(User... params) {
    try {
        MongoClientURI mongoClientURI = new MongoClientURI(Variables.mongo_uri);
        MongoClient mongoClient = new MongoClient(mongoClientURI);
        MongoDatabase dbMongo = mongoClient.getDatabase(mongoClientURI.getDatabase());
        MongoCollection<Document> coll = dbMongo.getCollection("users");
        User local_user = params[0];
        if (!local_user.getEmail().equals(original_email)) {
            Document user = coll.find(eq("email", local_user.getEmail())).first();
            if (user != null) {
                return 1; // Repeated email
            }
        }

        Document search = new Document("_id", new ObjectId(local_user.getId()));
        Document replacement = new Document("$set", local_user.getRegisterDocument());
        // We update some fields of the documents without affecting the rest
        coll.updateOne(search, replacement);
        mongoClient.close();
        return 0; //Successfully saved
    } catch (Exception e) {
        return 2; // Error
    }
}
 
Example #10
Source File: MainActivity.java    From medical-data-android with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected Integer doInBackground(User... params) {
    try {
        MongoClientURI mongoClientURI = new MongoClientURI(Variables.mongo_uri);
        MongoClient mongoClient = new MongoClient(mongoClientURI);
        MongoDatabase dbMongo = mongoClient.getDatabase(mongoClientURI.getDatabase());
        MongoCollection<Document> coll = dbMongo.getCollection("users");
        User local_user = params[0];
        Document user = coll.find(eq("email", local_user.getEmail())).first();
        mongoClient.close();
        if (user == null || !(user.get("pin").equals(local_user.getPin()))) {
            return 1; // Wrong data
        }
        Date d = (Date) user.get("birthDate");
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        // WARNING: Calendar.MONTH starts in 0 Calendar.DAY_OF_MONTH starts in 1
        local_user.completeSignIn((String) user.get("name"), cal.get(Calendar.DAY_OF_MONTH) - 1, cal.get(Calendar.MONTH), cal.get(Calendar.YEAR), (Boolean) user.get("gender"), user.getObjectId("_id").toString());
        return 0; //Successfully saved
    } catch (Exception e) {
        return 2; // Error
    }
}
 
Example #11
Source File: ServiceManager.java    From hvdf with Apache License 2.0 6 votes vote down vote up
public ServiceManager(Map<String, Object> svcConfig, MongoClientURI defaultUri) {

        this.svcConfig = svcConfig;
        this.factory = new ServiceFactory();
        this.defaultDbUri = defaultUri;
        
        logger.info("Initializing configured services");
        // Load the configured AsyncService implementation
        Map<String, Object> asyncServiceConfig = getServiceConfig(ASYNC_SERVICE_KEY, DEFAULT_ASYNC_SERVICE);
        if(asyncServiceConfig != null){
            factory.createAndRegisterService(
                    AsyncService.class, asyncServiceConfig, this.defaultDbUri);
        }
        
        // Load the configured UserGraphService implementation
        Map<String, Object> channelServiceConfig = getServiceConfig(CHANNEL_SERVICE_KEY, DEFAULT_CHANNEL_SERVICE);
        factory.createAndRegisterService(
                ChannelService.class, channelServiceConfig, this.defaultDbUri);
                
    }
 
Example #12
Source File: MinimumViableIT.java    From kafka-connect-mongodb with Apache License 2.0 6 votes vote down vote up
@BeforeAll
public static void setup() throws IOException {
    CONTAINER_ENV.start();
    MONGODB_CLIENT_URI = new MongoClientURI(
            "mongodb://"+ MONGODB+":"+MONGODB_PORT+"/kafkaconnect"
    );
    MONGO_CLIENT = new MongoClient(MONGODB_CLIENT_URI);
    MONGO_DATABASE = MONGO_CLIENT.getDatabase(MONGODB_CLIENT_URI.getDatabase());

    Properties props = new Properties();
    props.put("bootstrap.servers",KAFKA_BROKER+":"+KAFKA_BROKER_PORT);
    props.put("key.serializer", "io.confluent.kafka.serializers.KafkaAvroSerializer");
    props.put("value.serializer", "io.confluent.kafka.serializers.KafkaAvroSerializer");
    props.put("schema.registry.url","http://"+SCHEMA_REGISTRY+":"+ SCHEMA_REGISTRY_PORT);
    PRODUCER = new KafkaProducer<>(props);

    String config = new String(Files.readAllBytes(Paths.get(SINK_CONNECTOR_CONFIG)));

    deferExecutionToWaitForDataPropagation(Duration.ofSeconds(30),
            "wait some time so that all container processes become available");

    registerMongoDBSinkConnector(config);
}
 
Example #13
Source File: FanoutOnWriteTimeBuckets.java    From socialite with Apache License 2.0 6 votes vote down vote up
public FanoutOnWriteTimeBuckets(final MongoClientURI dbUri, final UserGraphService userGraph, 
        final ContentService content, final FanoutOnWriteTimeBucketsConfiguration svcConfig) {
    
    super(dbUri, userGraph, content, svcConfig);
    this.config = svcConfig;
    bucketTimespanSeconds = this.config.bucket_timespan_days*DAY_IN_SECONDS;
    
    // setup the buckets collection for users
    this.buckets = this.database.getCollection(config.bucket_collection_name);
    
    // The id for this model is of form _id : { _t : 1234, _u : "userid" }
    // A separate index is created for {_id._t : -1 , _id._u : 1}
    this.buckets.createIndex( new BasicDBObject(
            subField(BUCKET_ID_KEY, BUCKET_TIME_KEY), -1).
            append(subField(BUCKET_ID_KEY, BUCKET_OWNER_KEY), 1));
}
 
Example #14
Source File: MongoContext.java    From immutables with Apache License 2.0 6 votes vote down vote up
public static MongoContext create() {
  final String uri = System.getProperty("mongo");
  final Closer closer = Closer.create();
  if (uri != null) {
    // remote mongo server
    return new MongoContext(new MongoClient(new MongoClientURI(uri)), closer);
  }

  final MongoServer server = new MongoServer(new MemoryBackend());
  closer.register(new Closeable() {
    @Override
    public void close() throws IOException {
      server.shutdownNow();
    }
  });

  final MongoClient client = new MongoClient(new ServerAddress(server.bind()));
  final MongoContext context = new MongoContext(client, closer);
  return context;
}
 
Example #15
Source File: SocialiteService.java    From socialite with Apache License 2.0 6 votes vote down vote up
@Override
public void run(SocialiteConfiguration config, Environment environment) throws Exception {
  	        
   	// Get the configured default MongoDB URI
    MongoClientURI default_uri = config.mongodb.default_database_uri;
    
    // Initialize the services as per configuration
    ServiceManager services = new ServiceManager(config.services, default_uri);
    environment.manage(services);
           
    // Register the custom ExceptionMapper to handle ServiceExceptions
    environment.addProvider(new ServiceExceptionMapper());
    
    environment.addResource( new UserResource( services.getContentService(),
            services.getFeedService(), services.getUserGraphService() ) );
}
 
Example #16
Source File: RegisterActivity.java    From medical-data-android with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected Integer doInBackground(User... params) {
    try {
        MongoClientURI mongoClientURI = new MongoClientURI(Variables.mongo_uri);
        MongoClient mongoClient = new MongoClient(mongoClientURI);
        MongoDatabase dbMongo = mongoClient.getDatabase(mongoClientURI.getDatabase());
        MongoCollection<Document> coll = dbMongo.getCollection("users");
        User local_user = params[0];
        if (coll.find(eq("email", local_user.getEmail())).first() != null) {
            mongoClient.close();
            return 1; // Repeated email
        }
        Document document = local_user.getRegisterDocument();
        coll.insertOne(document);
        local_user.setId(document.getObjectId("_id").toString());
        mongoClient.close();
        return 0; //Successfully saved
    } catch (Exception e) {
        return 2; // Error
    }
}
 
Example #17
Source File: PullRequestCounter.java    From repairnator with MIT License 5 votes vote down vote up
/**
 * Has the number of patches or patched builds exceeded the number
 * we intend them to run for?
 * @return yes or no (true or false)
 */
public boolean keepRunning() {
    if(numberOfPRsToRunFor == 0) {
        return true;
    }
    MongoClient client = new MongoClient(new MongoClientURI(this.mongodbHost + "/" + this.mongodbName));
    MongoDatabase mongo = client.getDatabase(this.mongodbName);
    
    boolean run = this.numberOfPRsToRunFor > numberOfPRs(mongo);
    
    client.close();
    return run;
    // return this.numberOfPatchesToRunFor > numberOfPatches(mongo);
}
 
Example #18
Source File: AsyncPostDelivery.java    From socialite with Apache License 2.0 5 votes vote down vote up
public AsyncPostDelivery(final MongoClientURI dbUri, final FeedService toWrap,
        final UserGraphService userGraph, final ContentService content,
        final AsyncService asyncService, final AsyncPostDeliveryConfiguration config){
    this.config = config;
    this.wrappedService = toWrap;
    this.userService = userGraph;
    this.contentService = content;
    this.asyncService = asyncService;
    
    asyncService.registerRecoveryService(AsyncTaskType.FEED_POST_FANOUT, this);
}
 
Example #19
Source File: ServiceManager.java    From socialite with Apache License 2.0 5 votes vote down vote up
public ServiceManager(Map<String, Object> svcConfig, MongoClientURI defaultUri) {

        this.svcConfig = svcConfig;
        this.factory = new ServiceFactory();
        this.defaultDbUri = defaultUri;
        
        logger.info("Initializing configured services");
        // Load the configured AsyncService implementation
        Map<String, Object> asyncServiceConfig = getServiceConfig(ASYNC_SERVICE_KEY, DEFAULT_ASYNC_SERVICE);
        if(asyncServiceConfig != null){
            factory.createAndRegisterService(
                    AsyncService.class, asyncServiceConfig, this.defaultDbUri);
        }
        
        // Load the configured UserGraphService implementation
        Map<String, Object> userServiceConfig = getServiceConfig(USER_SERVICE_KEY, DEFAULT_USER_SERVICE);
        factory.createAndRegisterService(
                UserGraphService.class, userServiceConfig, this.defaultDbUri);
                
        // Load the configured ContentService implementation
        Map<String, Object> contentServiceConfig = getServiceConfig(CONTENT_SERVICE_KEY, DEFAULT_CONTENT_SERVICE);
        factory.createAndRegisterService(
                ContentService.class, contentServiceConfig, this.defaultDbUri);
        
        // Load the configured FeedService implementation passing
        // the UserGraph and Content service as arguments
        Map<String, Object> feedServiceConfig = getServiceConfig(FEED_SERVICE_KEY, DEFAULT_FEED_SERVICE);
        factory.createAndRegisterService(
                FeedService.class, feedServiceConfig, this.defaultDbUri);
        
        // Load the configured feed processor
        Map<String, Object> feedProcessorConfig = getServiceConfig(FEED_PROCESSING_KEY, DEFAULT_FEED_PROCESSING);
        if(feedProcessorConfig != null){
            
            factory.createAndRegisterService(
                    FeedService.class, feedProcessorConfig, this.defaultDbUri);      
        }
    }
 
Example #20
Source File: MongoDBReporterProvider.java    From graylog-plugin-metrics-reporter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public MongoDBReporter get() {
    final MongoClientURI mongoClientURI = configuration.getUri();
    final MongoCredential credentials = mongoClientURI.getCredentials();
    return MongoDBReporter.forRegistry(metricRegistry)
            .serverAddresses(extractServerAddresses(mongoClientURI))
            .mongoCredentials(credentials == null ? new MongoCredential[0] : new MongoCredential[]{credentials})
            .mongoClientOptions(mongoClientURI.getOptions())
            .withDatabaseName(mongoClientURI.getDatabase())
            .additionalFields(configuration.getAdditionalFields())
            .convertDurationsTo(configuration.getUnitDurations())
            .convertRatesTo(configuration.getUnitRates())
            .filter(new RegexMetricFilter(configuration.getIncludeMetrics()))
            .build();
}
 
Example #21
Source File: MongoDBDriver.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
private static MongoClientURI getMongoURI( Properties connProps,
		MongoClientOptions.Builder clientOptionsBuilder ) throws Exception
{
       // check if explicitly indicated not to use URI, even if URI value exists
       Boolean ignoreURI = getBooleanPropValue( connProps, IGNORE_URI_PROP );
       if( ignoreURI != null && ignoreURI )
           return null;

       String uri = getStringPropValue( connProps, MONGO_URI_PROP );
       if( uri == null || uri.isEmpty() )
           return null;
       
       try
       {
		if ( clientOptionsBuilder != null )
		{
			return new MongoClientURI( uri, clientOptionsBuilder );
		}
		else
		{
			return new MongoClientURI( uri );
		}
       }
       catch( Exception ex )
       {
           // log and ignore
           getLogger().log( Level.INFO, Messages.bind( "Invalid Mongo Database URI: {0}", uri ), ex ); //$NON-NLS-1$
		throw ex;
       }
       //return null;
   }
 
Example #22
Source File: CachedFeedService.java    From socialite with Apache License 2.0 5 votes vote down vote up
public CachedFeedService(final MongoClientURI dbUri, final UserGraphService usergraph, 
        final ContentService content, final CachedFeedServiceConfiguration svcConfig){
    super(dbUri, svcConfig);
    this.contentService = content;
    this.usergraphService = usergraph;
    
    // setup the buckets collection for users
    this.cacheFilter = new CacheContentFilter(svcConfig.cache_author,
            svcConfig.cache_message, svcConfig.cache_data);
}
 
Example #23
Source File: MongoDBConnection.java    From nationalparks with Apache License 2.0 5 votes vote down vote up
@PostConstruct
public void initConnection() {
    String mongoHost = env.getProperty("mongodb.server.host", "127.0.0.1"); // env var MONGODB_SERVER_HOST takes precedence
    String mongoPort = env.getProperty("mongodb.server.port", "27017"); // env var MONGODB_SERVER_PORT takes precedence
    String mongoUri = env.getProperty("uri", "");
    String mongoUser = env.getProperty("mongodb.user", "mongodb"); // env var MONGODB_USER takes precedence
    String mongoPassword = env.getProperty("mongodb.password", "mongodb"); // env var MONGODB_PASSWORD takes precedence
    String mongoDBName = env.getProperty("mongodb.database", "mongodb"); // env var MONGODB_DATABASE takes precedence
    String dbServiceName = env.getProperty("database.service.name", "");

    try {
    	// If mongoUri is set, we use this, else, we use mongoHost and mongoPort
    	// This will come in this form (mongodb://127.0.0.1:27017)
    	if (mongoUri!=null && ! "".equals(mongoUri)){
    		Pattern pattern = Pattern.compile("mongodb?://([^:^/]*):?(\\d*)?");
    		Matcher matcher = pattern.matcher(mongoUri);
    		if (matcher.find()){
    			mongoHost = matcher.group(1);
    			mongoPort = matcher.group(2);
    			// We assume all information comes in the binding format
    	        mongoUser = env.getProperty("username", "mongodb");
    	        mongoPassword = env.getProperty("password", "mongodb");
    	        mongoDBName = env.getProperty("database_name", "mongodb");       			
    		}
    	} else if (dbServiceName !=null && ! "".equals(dbServiceName)) {
            mongoHost = dbServiceName;
        }
    	
        String mongoURI = "mongodb://" + mongoUser + ":" + mongoPassword + "@" + mongoHost + ":" + mongoPort + "/" + mongoDBName;
        System.out.println("[INFO] Connection string: " + mongoURI);
        MongoClient mongoClient = new MongoClient(new MongoClientURI(mongoURI));
        mongoDB = mongoClient.getDatabase(mongoDBName);
    } catch (Exception e) {
        System.out.println("[ERROR] Creating the mongoDB. " + e.getMessage());
        mongoDB = null;
    }
}
 
Example #24
Source File: MongoWrapper.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public DBCollection openMongoDb() throws UnknownHostException {

    	MongoClientURI dbUri = new MongoClientURI(dbUriStr_+"?socketTimeoutMS=180000");
	    mongoClient_ = new MongoClient(dbUri);

	    DB db = mongoClient_.getDB( dbName_ );
	    DBCollection coll = db.getCollection(collection_);
	    coll.createIndex(new BasicDBObject(index_, 1));  // create index on "i", ascending

	    return coll;

    }
 
Example #25
Source File: MongoStorage.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public void init() {
    if (!Strings.isNullOrEmpty(this.connectionUri)) {
        this.mongoClient = new MongoClient(new MongoClientURI(this.connectionUri));
    } else {
        MongoCredential credential = null;
        if (!Strings.isNullOrEmpty(this.configuration.getUsername())) {
            credential = MongoCredential.createCredential(
                    this.configuration.getUsername(),
                    this.configuration.getDatabase(),
                    Strings.isNullOrEmpty(this.configuration.getPassword()) ? null : this.configuration.getPassword().toCharArray()
            );
        }

        String[] addressSplit = this.configuration.getAddress().split(":");
        String host = addressSplit[0];
        int port = addressSplit.length > 1 ? Integer.parseInt(addressSplit[1]) : 27017;
        ServerAddress address = new ServerAddress(host, port);

        if (credential == null) {
            this.mongoClient = new MongoClient(address);
        } else {
            this.mongoClient = new MongoClient(address, credential, MongoClientOptions.builder().build());
        }
    }
    
    this.database = this.mongoClient.getDatabase(this.configuration.getDatabase());
}
 
Example #26
Source File: MongoDBDriver.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
static String getDatabaseName( Properties connProps )
{
    MongoClientURI mongoURI = getMongoURI( connProps );
    if( mongoURI != null )
        return mongoURI.getDatabase();
    
    // no mongoURI specified, get from the individual property
    return getStringPropValue( connProps, DBNAME_PROP );
}
 
Example #27
Source File: MongoImpl.java    From tephra with MIT License 5 votes vote down vote up
@Override
public void create(JSONObject config) {
    String key = config.getString("key");
    if (mongos.containsKey(key))
        return;

    String schema = config.getString("schema");
    if (validator.isEmpty(schema))
        throw new NullPointerException("未设置schema值[" + config + "]!");

    JSONArray array = config.getJSONArray("ips");
    if (array == null || array.size() == 0)
        throw new NullPointerException("未设置ips值[" + config + "]!");

    String username = config.getString("username");
    String password = config.getString("password");
    boolean ssl = json.hasTrue(config, "ssl");
    MongoClientOptions.Builder builder = MongoClientOptions.builder().connectionsPerHost(maxActive).maxWaitTime(maxWait);
    List<MongoClient> list = new ArrayList<>();
    for (int i = 0; i < array.size(); i++)
        list.add(new MongoClient(new MongoClientURI("mongodb://" + username + ":" + password + "@" + array.getString(i)
                + "/" + schema + (ssl ? "?ssl=true" : ""), builder)));
    schemas.put(key, schema);
    mongos.put(key, list);

    if (logger.isDebugEnable())
        logger.debug("Mongo数据库[{}]初始化完成。", config);
}
 
Example #28
Source File: ProfilerExtension.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
public static void setMongo(String mongourl, String database, String collection) throws Exception {
	if ( thisInst.mongourl != null && !thisInst.mongourl.equals(mongourl)) 
		return;
	
	thisInst.mongourl = mongourl;
	MongoClientURI	m	= new MongoClientURI(thisInst.mongourl);
	
	thisInst.mongo	= new MongoClient( m );
	thisInst.coll		= thisInst.mongo.getDB( database ).getCollection(collection);
	
	Thread t = new Thread(thisInst);
	t.setName("ProfilerExtension:MongoThread");
	t.setPriority( Thread.MIN_PRIORITY );
	t.start();
}
 
Example #29
Source File: FeedPaginationTest.java    From socialite with Apache License 2.0 5 votes vote down vote up
public FeedPaginationTest(String testName, Map<String, Object> svcConfig) 
            throws UnknownHostException {
    String databaseName = DATABASE_NAME + "-" + testName;
    MongoClientURI uri = new MongoClientURI(BASE_URI + databaseName);
    DatabaseTools.dropDatabaseByURI(uri, databaseName);
    // Load the configured FeedService implementation passing
    // the UserGraph and Content service as arguments
    ServiceFactory factory = new ServiceFactory();
    initDependencies(factory, uri);
    feedService = factory.createService(FeedService.class, svcConfig, uri);
}
 
Example #30
Source File: FeedServiceTest.java    From socialite with Apache License 2.0 5 votes vote down vote up
public FeedServiceTest(String testName, Map<String, Object> feedConfig) 
            throws UnknownHostException {
    String databaseName = DATABASE_NAME + "-" + testName;
    MongoClientURI uri = new MongoClientURI(BASE_URI + databaseName);
    DatabaseTools.dropDatabaseByURI(uri, databaseName);
    // Load the configured FeedService implementation passing
    // the UserGraph and Content service as arguments
    ServiceFactory factory = new ServiceFactory();
    initDependencies(factory, uri);
    feedService = factory.createService(FeedService.class, feedConfig, uri);
}