/** * Creates a new {@link SimpleMongoDbFactory} instance from the given {@link MongoClientURI}. * * @param uri coordinates for a database connection. Must contain a database name and must not be {@literal null}. * @since 1.7 */ public SimpleMongoDbFactory(MongoClientURI uri) { this(new MongoClient(uri), uri.getDatabase(), true); }
@Inject public JongoFactoryImpl(final MongoClientURI uri, final MongoClient client, final Mapper mapper) { this.db = requireNonNull(uri, "MongoClientURI is required.").getDatabase(); this.client = requireNonNull(client, "MongoClient is required."); this.mapper = requireNonNull(mapper, "Mapper is required."); }
/** * The MongoDbClient constructor. * @param url The Mongo server url * @param collectionName The Mongo collection to read/write data */ public MongoDbClient(String url, String collectionName) { //Creates a MongoURI from the given string. MongoClientURI uri = new MongoClientURI(url); //Creates a MongoClient described by a URI. this.client = new MongoClient(uri); //Gets a Database. MongoDatabase db = client.getDatabase(uri.getDatabase()); //Gets a collection. this.collection = db.getCollection(collectionName); }
/** * Gets the database name. * * @return the database name */ @Nullable public String getDatabase() { return proxied.getDatabase(); }
MongoClientURI uri = new MongoClientURI( "mongodb://username:password@www.example.com:12345/db-name" ); MongoClient mongoClient = new MongoClient(uri); MongoDatabase db = mongoClient.getDatabase(uri.getDatabase());
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); } 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); }
@Override public int hashCode() { int result = getOptions().hashCode(); result = 31 * result + getHosts().hashCode(); MongoCredential credentials = getCredentials(); result = 31 * result + (credentials != null ? credentials.hashCode() : 0); String database = getDatabase(); result = 31 * result + (database != null ? database.hashCode() : 0); String collection = getCollection(); result = 31 * result + (collection != null ? collection.hashCode() : 0); return result; }
MongoClientURI uri = new MongoClientURI(url); String uriDb = uri.getDatabase(); if (!defaultedUrl && (uriDb != null) && !uriDb.isEmpty() && !"admin".equals(uriDb)) {
@Override public boolean equals(final Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } MongoClientURI that = (MongoClientURI) o; if (!getHosts().equals(that.getHosts())) { return false; } String database = getDatabase(); if (database != null ? !database.equals(that.getDatabase()) : that.getDatabase() != null) { return false; } String collection = getCollection(); if (collection != null ? !collection.equals(that.getCollection()) : that.getCollection() != null) { return false; } MongoCredential credentials = getCredentials(); if (credentials != null ? !credentials.equals(that.getCredentials()) : that.getCredentials() != null) { return false; } if (!getOptions().equals(that.getOptions())) { return false; } return true; }
public synchronized Mongo connect() { if (m == null) { final String dbName = mongoClientURI.getDatabase(); if (isNullOrEmpty(dbName)) { LOG.error("The MongoDB database name must not be null or empty (mongodb_uri was: {})", mongoClientURI);
@Override public void configure(final Env env, final Config conf, final Binder binder) { configure(env, conf, binder, (uri, client) -> { String db = uri.getDatabase(); Mapper mapper = new Mapper(); Morphia morphia = new Morphia(mapper); if (this.morphiaCbck != null) { this.morphiaCbck.accept(morphia, conf); } Datastore datastore = morphia.createDatastore(client, mapper, db); if (gen != null) { mapper.addInterceptor(new AutoIncID(datastore, gen)); } if (callback != null) { callback.accept(datastore); } ServiceKey serviceKey = env.serviceKey(); serviceKey.generate(Morphia.class, db, k -> binder.bind(k).toInstance(morphia)); serviceKey.generate(Datastore.class, db, k -> binder.bind(k).toInstance(datastore)); env.onStart(registry -> new GuiceObjectFactory(registry, morphia)); }); }
/** * Create setup using MongoDB client uri. * <ul> * <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).getDatabase(databaseName)) .executor(newExecutor()) .gson(createGson()) .build(); }
/** * Creates a new {@link SimpleMongoDbFactory} instance from the given {@link MongoClientURI}. * * @param uri coordinates for a database connection. Must contain a database name and must not be {@literal null}. * @since 1.7 */ public SimpleMongoDbFactory(MongoClientURI uri) { this(new MongoClient(uri), uri.getDatabase(), true); }
public MongoSchemaDBStore(MongoClientURI uri) throws IOException { client = new MongoClient(uri); String databaseName = uri.getDatabase(); if (databaseName == null) { databaseName = DEFAULT_databaseName; } DB database = client.getDB(databaseName); String collectionName = uri.getCollection(); if (collectionName == null) { collectionName = DEFAULT_collectionName; } collection = database.getCollection(collectionName); collection.createIndex( new BasicDBObject(FeatureTypeDBObject.KEY_typeName, 1), new BasicDBObject("unique", true)); }
public MongoDataStore( String dataStoreURI, String schemaStoreURI, boolean createDatabaseIfNeeded) { MongoClientURI dataStoreClientURI = createMongoClientURI(dataStoreURI); dataStoreClient = createMongoClient(dataStoreClientURI); dataStoreDB = createDB( dataStoreClient, dataStoreClientURI.getDatabase(), !createDatabaseIfNeeded); if (dataStoreDB == null) { dataStoreClient.close(); // This smells bad... throw new IllegalArgumentException( "Unknown mongodb database, \"" + dataStoreClientURI.getDatabase() + "\""); } schemaStore = createSchemaStore(schemaStoreURI); if (schemaStore == null) { dataStoreClient.close(); // This smells bad too... throw new IllegalArgumentException( "Unable to initialize schema store with URI \"" + schemaStoreURI + "\""); } filterCapabilities = createFilterCapabilties(); }
private SimpleMongoDbFactory createMongoDbFactory(MongoServiceInfo serviceInfo, MongoClientOptions.Builder mongoOptionsToUse) throws UnknownHostException { MongoClientURI mongoClientURI = new MongoClientURI(serviceInfo.getUri(), mongoOptionsToUse); MongoClient mongo = new MongoClient(mongoClientURI); return new SimpleMongoDbFactory(mongo, mongoClientURI.getDatabase()); }
public DB create() { try { mongoClient = new MongoClient(uri); mongoClient.setWriteConcern(WriteConcern.ACKNOWLEDGED); return mongoClient.getDB(uri.getDatabase()); } catch (Exception e) { LOGGER.error(format("Cannot connect to MongoDB on hosts %s using username: '%s'", uri.getHosts(), uri.getUsername()), e); throw new CouldNotProvideMongoException(e); } }
@Override public List<InputSplit> calculateSplits() { if (LOG.isDebugEnabled()) { MongoClientURI inputURI = MongoConfigUtil.getInputURI(getConfiguration()); LOG.debug(format("SingleMongoSplitter calculating splits for namespace: %s.%s; hosts: %s", inputURI.getDatabase(), inputURI.getCollection(), inputURI.getHosts())); } return Collections.singletonList( (InputSplit) new MongoInputSplit(getConfiguration())); }
private static MongoDbFactory mongoDbFactory(final MongoClient mongo, final BaseMongoDbProperties props) { if (StringUtils.isNotBlank(props.getClientUri())) { val uri = buildMongoClientURI(props.getClientUri(), buildMongoDbClientOptions(props)); LOGGER.trace("Using database [{}] from the connection client URI", uri.getDatabase()); return new SimpleMongoDbFactory(uri); } return new SimpleMongoDbFactory(mongo, props.getDatabaseName()); }
@Override public void clean() throws IOException { MongoClientURI uri = new MongoClientURI(mongoUri); MongoClient client = new MongoClient(uri); client.dropDatabase(uri.getDatabase()); blob.clean(); }