Skip to content

Commit 4b78ef6

Browse files
christophstroblmp911de
authored andcommitted
Extend GridFsTemplate and its reactive variant to accept a provided GridFSBucket instance.
Allow to pass in a GridFSBucket from outside to avoid recreating instances on every method call. Closes #1627 Original pull request:# 4389
1 parent 5163e54 commit 4b78ef6

File tree

3 files changed

+67
-24
lines changed

3 files changed

+67
-24
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsTemplate.java

+22-9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.ArrayList;
2323
import java.util.List;
2424
import java.util.Optional;
25+
import java.util.function.Supplier;
2526

2627
import org.bson.Document;
2728
import org.bson.types.ObjectId;
@@ -30,6 +31,7 @@
3031
import org.springframework.data.mongodb.core.convert.MongoConverter;
3132
import org.springframework.data.mongodb.core.query.Query;
3233
import org.springframework.data.mongodb.util.BsonUtils;
34+
import org.springframework.data.util.Lazy;
3335
import org.springframework.lang.Nullable;
3436
import org.springframework.util.Assert;
3537
import org.springframework.util.StringUtils;
@@ -56,9 +58,7 @@
5658
*/
5759
public class GridFsTemplate extends GridFsOperationsSupport implements GridFsOperations, ResourcePatternResolver {
5860

59-
private final MongoDatabaseFactory dbFactory;
60-
61-
private final @Nullable String bucket;
61+
private Supplier<GridFSBucket> bucketSupplier;
6262

6363
/**
6464
* Creates a new {@link GridFsTemplate} using the given {@link MongoDatabaseFactory} and {@link MongoConverter}.
@@ -78,13 +78,22 @@ public GridFsTemplate(MongoDatabaseFactory dbFactory, MongoConverter converter)
7878
* @param bucket can be {@literal null}.
7979
*/
8080
public GridFsTemplate(MongoDatabaseFactory dbFactory, MongoConverter converter, @Nullable String bucket) {
81+
this(converter, Lazy.of(() -> getGridFs(dbFactory, bucket)));
82+
}
8183

82-
super(converter);
84+
/**
85+
* Creates a new {@link GridFsTemplate} using the given {@link MongoConverter} and {@link Supplier} providing the
86+
* required {@link GridFSBucket}.
87+
*
88+
* @param converter must not be {@literal null}.
89+
* @param gridFSBucket must not be {@literal null}.
90+
* @since 4.2
91+
*/
92+
public GridFsTemplate(MongoConverter converter, Supplier<GridFSBucket> gridFSBucket) {
8393

84-
Assert.notNull(dbFactory, "MongoDbFactory must not be null");
94+
super(converter);
8595

86-
this.dbFactory = dbFactory;
87-
this.bucket = bucket;
96+
this.bucketSupplier = gridFSBucket;
8897
}
8998

9099
public ObjectId store(InputStream content, @Nullable String filename, @Nullable String contentType,
@@ -142,7 +151,7 @@ public void delete(Query query) {
142151
}
143152

144153
public ClassLoader getClassLoader() {
145-
return dbFactory.getClass().getClassLoader();
154+
return this.getClassLoader();
146155
}
147156

148157
public GridFsResource getResource(String location) {
@@ -182,7 +191,11 @@ public GridFsResource[] getResources(String locationPattern) {
182191
return new GridFsResource[] { getResource(locationPattern) };
183192
}
184193

185-
private GridFSBucket getGridFs() {
194+
GridFSBucket getGridFs() {
195+
return this.bucketSupplier.get();
196+
}
197+
198+
private static GridFSBucket getGridFs(MongoDatabaseFactory dbFactory, @Nullable String bucket) {
186199

187200
MongoDatabase db = dbFactory.getMongoDatabase();
188201
return bucket == null ? GridFSBuckets.create(db) : GridFSBuckets.create(db, bucket);

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/ReactiveGridFsTemplate.java

+22-7
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.bson.Document;
2828
import org.bson.types.ObjectId;
2929
import org.reactivestreams.Publisher;
30-
3130
import org.springframework.core.io.buffer.DataBuffer;
3231
import org.springframework.core.io.buffer.DataBufferFactory;
3332
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
@@ -37,6 +36,7 @@
3736
import org.springframework.data.mongodb.core.query.Query;
3837
import org.springframework.data.mongodb.core.query.SerializationUtils;
3938
import org.springframework.data.mongodb.util.BsonUtils;
39+
import org.springframework.data.util.Lazy;
4040
import org.springframework.lang.Nullable;
4141
import org.springframework.util.Assert;
4242
import org.springframework.util.StringUtils;
@@ -61,9 +61,8 @@
6161
*/
6262
public class ReactiveGridFsTemplate extends GridFsOperationsSupport implements ReactiveGridFsOperations {
6363

64-
private final ReactiveMongoDatabaseFactory dbFactory;
6564
private final DataBufferFactory dataBufferFactory;
66-
private final @Nullable String bucket;
65+
private Mono<GridFSBucket> bucketSupplier;
6766

6867
/**
6968
* Creates a new {@link ReactiveGridFsTemplate} using the given {@link ReactiveMongoDatabaseFactory} and
@@ -100,15 +99,27 @@ public ReactiveGridFsTemplate(ReactiveMongoDatabaseFactory dbFactory, MongoConve
10099
*/
101100
public ReactiveGridFsTemplate(DataBufferFactory dataBufferFactory, ReactiveMongoDatabaseFactory dbFactory,
102101
MongoConverter converter, @Nullable String bucket) {
102+
this(converter, Mono.defer(Lazy.of(() -> doGetBucket(dbFactory, bucket))), dataBufferFactory);
103+
}
104+
105+
/**
106+
* Creates a new {@link ReactiveGridFsTemplate} using the given {@link MongoConverter}, {@link Mono} emitting a
107+
* {@link ReactiveMongoDatabaseFactory} and {@link DataBufferFactory}.
108+
*
109+
* @param converter must not be {@literal null}.
110+
* @param gridFSBucket must not be {@literal null}.
111+
* @param dataBufferFactory must not be {@literal null}.
112+
* @since 4.2
113+
*/
114+
public ReactiveGridFsTemplate(MongoConverter converter, Mono<GridFSBucket> gridFSBucket,
115+
DataBufferFactory dataBufferFactory) {
103116

104117
super(converter);
105118

106119
Assert.notNull(dataBufferFactory, "DataBufferFactory must not be null");
107-
Assert.notNull(dbFactory, "ReactiveMongoDatabaseFactory must not be null");
108120

121+
this.bucketSupplier = gridFSBucket;
109122
this.dataBufferFactory = dataBufferFactory;
110-
this.dbFactory = dbFactory;
111-
this.bucket = bucket;
112123
}
113124

114125
@Override
@@ -247,11 +258,15 @@ public <T> Flux<T> createFlux(ReactiveBucketCallback<T> callback) {
247258
return doGetBucket().flatMapMany(callback::doInBucket);
248259
}
249260

250-
protected Mono<GridFSBucket> doGetBucket() {
261+
static Mono<GridFSBucket> doGetBucket(ReactiveMongoDatabaseFactory dbFactory, @Nullable String bucket) {
251262
return dbFactory.getMongoDatabase()
252263
.map(db -> bucket == null ? GridFSBuckets.create(db) : GridFSBuckets.create(db, bucket));
253264
}
254265

266+
protected Mono<GridFSBucket> doGetBucket() {
267+
return bucketSupplier;
268+
}
269+
255270
/**
256271
* @param <T>
257272
* @author Mathieu Ouellet

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/MongoNamespaceTests.java

+23-8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020

2121
import javax.net.ssl.SSLSocketFactory;
2222

23+
import java.util.function.Supplier;
24+
25+
import com.mongodb.client.MongoCollection;
26+
import com.mongodb.client.gridfs.GridFSBucket;
27+
import com.mongodb.client.gridfs.model.GridFSFile;
2328
import org.junit.Test;
2429
import org.junit.runner.RunWith;
2530
import org.springframework.beans.factory.annotation.Autowired;
@@ -168,8 +173,12 @@ public void testGridFsTemplateFactory() {
168173
assertThat(ctx.containsBean("gridFsTemplate")).isTrue();
169174
GridFsOperations operations = (GridFsOperations) ctx.getBean("gridFsTemplate");
170175

171-
MongoDatabaseFactory dbf = (MongoDatabaseFactory) getField(operations, "dbFactory");
172-
assertThat(getField(dbf, "databaseName")).isEqualTo("database");
176+
Supplier<GridFSBucket> gridFSBucketSupplier = (Supplier<GridFSBucket>) getField(operations, "bucketSupplier");
177+
GridFSBucket gfsBucket = gridFSBucketSupplier.get();
178+
assertThat(gfsBucket.getBucketName()).isEqualTo("fs"); // fs is the default
179+
180+
MongoCollection<GridFSFile> filesCollection = (MongoCollection<GridFSFile>) getField(gfsBucket, "filesCollection");
181+
assertThat(filesCollection.getNamespace().getDatabaseName()).isEqualTo("database");
173182

174183
MongoConverter converter = (MongoConverter) getField(operations, "converter");
175184
assertThat(converter).isNotNull();
@@ -181,9 +190,12 @@ public void testSecondGridFsTemplateFactory() {
181190
assertThat(ctx.containsBean("secondGridFsTemplate")).isTrue();
182191
GridFsOperations operations = (GridFsOperations) ctx.getBean("secondGridFsTemplate");
183192

184-
MongoDatabaseFactory dbf = (MongoDatabaseFactory) getField(operations, "dbFactory");
185-
assertThat(getField(dbf, "databaseName")).isEqualTo("database");
186-
assertThat(getField(operations, "bucket")).isEqualTo(null);
193+
Supplier<GridFSBucket> gridFSBucketSupplier = (Supplier<GridFSBucket>) getField(operations, "bucketSupplier");
194+
GridFSBucket gfsBucket = gridFSBucketSupplier.get();
195+
assertThat(gfsBucket.getBucketName()).isEqualTo("fs"); // fs is the default
196+
197+
MongoCollection<GridFSFile> filesCollection = (MongoCollection<GridFSFile>) getField(gfsBucket, "filesCollection");
198+
assertThat(filesCollection.getNamespace().getDatabaseName()).isEqualTo("database");
187199

188200
MongoConverter converter = (MongoConverter) getField(operations, "converter");
189201
assertThat(converter).isNotNull();
@@ -195,9 +207,12 @@ public void testThirdGridFsTemplateFactory() {
195207
assertThat(ctx.containsBean("thirdGridFsTemplate")).isTrue();
196208
GridFsOperations operations = (GridFsOperations) ctx.getBean("thirdGridFsTemplate");
197209

198-
MongoDatabaseFactory dbf = (MongoDatabaseFactory) getField(operations, "dbFactory");
199-
assertThat(getField(dbf, "databaseName")).isEqualTo("database");
200-
assertThat(getField(operations, "bucket")).isEqualTo("bucketString");
210+
Supplier<GridFSBucket> gridFSBucketSupplier = (Supplier<GridFSBucket>) getField(operations, "bucketSupplier");
211+
GridFSBucket gfsBucket = gridFSBucketSupplier.get();
212+
assertThat(gfsBucket.getBucketName()).isEqualTo("bucketString"); // fs is the default
213+
214+
MongoCollection<GridFSFile> filesCollection = (MongoCollection<GridFSFile>) getField(gfsBucket, "filesCollection");
215+
assertThat(filesCollection.getNamespace().getDatabaseName()).isEqualTo("database");
201216

202217
MongoConverter converter = (MongoConverter) getField(operations, "converter");
203218
assertThat(converter).isNotNull();

0 commit comments

Comments
 (0)