List of usage examples for com.mongodb MongoClientURI getDatabase
@Nullable
public String getDatabase()
From source file:org.flywaydb.core.MongoFlyway.java
License:Apache License
/** * Sets the MongoClient to use. Must have the necessary privileges to execute ddl. * * To use a custom ClassLoader, setClassLoader() must be called prior to calling this method. */*from w ww . j ava2 s .co m*/ * @param uri MongoDB connection URI used to connect to a MongoDB database server. */ private void setMongoClient(String uri) { MongoClientURI mongoUri = new MongoClientURI(uri); this.databaseName = mongoUri.getDatabase(); this.client = new MongoClient(mongoUri); createdMongoClient = true; }
From source file:org.graylog.plugins.metrics.mongodb.providers.MongoDBReporterProvider.java
License:Open Source License
@Override public MongoDBReporter get() { final MongoClientURI mongoClientURI = configuration.getUri(); final MongoCredential credentials = mongoClientURI.getCredentials(); return MongoDBReporter.forRegistry(metricRegistry).serverAddresses(extractServerAddresses(mongoClientURI)) .mongoCredentials(/*from w w w. jav a 2 s. com*/ 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(); }
From source file:org.imirp.imirp.db._DbModule.java
License:Apache License
@Provides @Singleton/*www . j a v a2s . c om*/ Jongo provideJongo(MongoClient mongo, MongoClientURI uri) { String dbName = uri.getDatabase(); logger.debug("Using database[" + dbName + "]"); return new Jongo(mongo.getDB(dbName)); }
From source file:org.immutables.mongo.repository.RepositorySetup.java
License:Apache License
/** * Create setup using MongoDB client uri. * <ul>//from w ww .jav a2 s .co m * <li>URI should contain database path segment</li> * <li>New internal {@link MongoClient} will be created</li> * <li>New internal executor will be created (with shutdown on jvm exit)</li> * <li>New {@link Gson} instance will be created configured with type adapter factory providers</li> * </ul> * <p> * Setup created by this factory methods should be reused to configure collection repositories for * the same MongoDB database. * <p> * This constructor designed for ease of use in sample scenarious. For more flexibility consider * using {@link #builder()} with custom constructed {@link ListeningExecutorService * executor} and {@link DB database} handle. * @param uri string that will be parsed as {@link MongoClientURI}. * @see MongoClientURI * @return repository setup instance. */ public static RepositorySetup forUri(String uri) { MongoClientURI clientUri = new MongoClientURI(uri); @Nullable String databaseName = clientUri.getDatabase(); checkArgument(databaseName != null, "URI should contain database path segment"); return builder().database(newMongoClient(clientUri).getDB(databaseName)).executor(newExecutor()) .gson(createGson()).build(); }
From source file:org.jberet.repository.MongoRepository.java
License:Open Source License
public MongoRepository(final Properties configProperties) { dataSourceName = configProperties.getProperty(JdbcRepository.DATASOURCE_JNDI_KEY); dbUrl = configProperties.getProperty(JdbcRepository.DB_URL_KEY); //if dataSourceName is configured, use dataSourceName; //else if dbUrl is specified, use dbUrl; if (dataSourceName != null) { dataSourceName = dataSourceName.trim(); }/*from w w w .j a v a 2 s. com*/ if (dataSourceName != null && !dataSourceName.isEmpty()) { try { mongoClient = InitialContext.doLookup(dataSourceName); for (final DB d : mongoClient.getUsedDatabases()) { db = d; break; } } catch (final NamingException e) { throw BatchMessages.MESSAGES.failToLookupDataSource(e, dataSourceName); } } else { if (dbUrl != null) { dbUrl = dbUrl.trim(); try { final MongoClientURI uri = new MongoClientURI(dbUrl); mongoClient = (MongoClient) Mongo.Holder.singleton().connect(uri); db = mongoClient.getDB(uri.getDatabase()); } catch (final Exception e) { throw BatchMessages.MESSAGES.invalidConfigProperty(e, JdbcRepository.DB_URL_KEY, dbUrl); } } } if (!db.collectionExists(TableColumns.SEQ)) { seqCollection = db.createCollection(TableColumns.SEQ, new BasicDBObject()); final DBObject jobInstanceDbo = new BasicDBObject(TableColumns._id, TableColumns.JOBINSTANCEID); jobInstanceDbo.put(TableColumns.SEQ, 1L); final DBObject jobExecutionDbo = new BasicDBObject(TableColumns._id, TableColumns.JOBEXECUTIONID); jobExecutionDbo.put(TableColumns.SEQ, 1L); final DBObject stepExecutionDbo = new BasicDBObject(TableColumns._id, TableColumns.STEPEXECUTIONID); stepExecutionDbo.put(TableColumns.SEQ, 1L); seqCollection.insert(jobInstanceDbo, jobExecutionDbo, stepExecutionDbo); } else { seqCollection = db.getCollection(TableColumns.SEQ); } }
From source file:org.jberet.support.io.MongoItemReaderWriterBase.java
License:Open Source License
protected void init() throws Exception { if (beanType == null) { throw SupportMessages.MESSAGES.invalidReaderWriterProperty(null, null, "beanType"); }/*from w ww.j av a2 s. c o m*/ if (mongoClientLookup == null) { final MongoClientURI clientURI; if (uri != null) { clientURI = new MongoClientURI(uri); if (database == null) { database = clientURI.getDatabase(); } if (collection == null) { collection = clientURI.getCollection(); } } else { clientURI = MongoClientObjectFactory.createMongoClientURI(host, database, collection, options, user, password); } mongoClient = (MongoClient) Mongo.Holder.singleton().connect(clientURI); } else { mongoClient = InitialContext.doLookup(mongoClientLookup); } db = mongoClient.getDB(database); jacksonCollection = JacksonDBCollection.wrap(db.getCollection(collection), beanType, String.class); }
From source file:org.jooby.mongodb.Mongodb.java
License:Apache License
protected void configure(final Env env, final Config config, final Binder binder, final BiConsumer<MongoClientURI, MongoClient> callback) { MongoClientOptions.Builder options = options(mongodb(config)); if (this.options != null) { this.options.accept(options, config); }/* w ww .j a v a2s .com*/ MongoClientURI uri = new MongoClientURI(config.getString(db), options); String database = uri.getDatabase(); checkArgument(database != null, "Database not found: " + uri); MongoClient client = new MongoClient(uri); ServiceKey serviceKey = env.serviceKey(); serviceKey.generate(MongoClientURI.class, database, k -> binder.bind(k).toInstance(uri)); serviceKey.generate(MongoClient.class, database, k -> binder.bind(k).toInstance(client)); MongoDatabase mongodb = client.getDatabase(database); serviceKey.generate(MongoDatabase.class, database, k -> binder.bind(k).toInstance(mongodb)); env.onStop(client::close); callback.accept(uri, client); }
From source file:org.netbeans.modules.mongodb.native_tools.MongoDumpExecAction.java
License:Open Source License
private void parseOptionsFromURI(MongoClientURI uri, Map<String, String> options) { if (uri.getUsername() != null && uri.getUsername().isEmpty() == false) { options.put(MongoDumpOptions.USERNAME, uri.getUsername()); }/* ww w . j a v a 2 s . c o m*/ if (uri.getPassword() != null && uri.getPassword().length > 0) { options.put(MongoDumpOptions.PASSWORD, new String(uri.getPassword())); } if (uri.getHosts() != null && uri.getHosts().isEmpty() == false) { final String hostWithPort = uri.getHosts().get(0); final Pattern p = Pattern.compile("(.*)(:(\\d+))?"); final Matcher m = p.matcher(hostWithPort); if (m.matches()) { final String host = m.group(1); final String port = m.group(3); if (host.isEmpty() == false) { options.put(MongoDumpOptions.HOST, host); if (port != null) { options.put(MongoDumpOptions.PORT, port); } } } } if (uri.getDatabase() != null && uri.getDatabase().isEmpty() == false) { options.put(MongoDumpOptions.DB, uri.getDatabase()); } if (uri.getCollection() != null && uri.getCollection().isEmpty() == false) { options.put(MongoDumpOptions.COLLECTION, uri.getCollection()); } }
From source file:org.netbeans.modules.mongodb.native_tools.MongoRestoreExecAction.java
License:Open Source License
private void parseOptionsFromURI(MongoClientURI uri, Map<String, String> options) { if (uri.getUsername() != null && uri.getUsername().isEmpty() == false) { options.put(MongoRestoreOptions.USERNAME, uri.getUsername()); }//from ww w.jav a 2s . c o m if (uri.getPassword() != null && uri.getPassword().length > 0) { options.put(MongoRestoreOptions.PASSWORD, new String(uri.getPassword())); } if (uri.getHosts() != null && uri.getHosts().isEmpty() == false) { final String hostWithPort = uri.getHosts().get(0); final Pattern p = Pattern.compile("(.*)(:(\\d+))?"); final Matcher m = p.matcher(hostWithPort); if (m.matches()) { final String host = m.group(1); final String port = m.group(3); if (host.isEmpty() == false) { options.put(MongoRestoreOptions.HOST, host); if (port != null) { options.put(MongoRestoreOptions.PORT, port); } } } } if (uri.getDatabase() != null && uri.getDatabase().isEmpty() == false) { options.put(MongoRestoreOptions.DB, uri.getDatabase()); } if (uri.getCollection() != null && uri.getCollection().isEmpty() == false) { options.put(MongoRestoreOptions.COLLECTION, uri.getCollection()); } }
From source file:org.netbeans.modules.mongodb.ui.components.MongoURIEditorPanel.java
License:Open Source License
public void setMongoURI(MongoClientURI uri) { hostsListModel.clear();/*from ww w . j a v a 2 s . c o m*/ optionsListModel.clear(); for (String string : uri.getHosts()) { final String[] rawHost = string.split(":"); final String hostname = urlDecode(rawHost[0]); final Integer port = rawHost.length > 1 ? Integer.parseInt(rawHost[1]) : null; hostsListModel.addElement(new Host(hostname, port)); } usernameField.setText(uri.getUsername()); if (uri.getPassword() != null) { passwordField.setText(new String(uri.getPassword())); } databaseField.setText(uri.getDatabase()); final List<Option> options = decodeOptions(uri); optionsListModel.clear(); for (Option option : options) { optionsListModel.addElement(option); } updateURIField(); }