|
16 | 16 |
|
17 | 17 | package org.springframework.boot.autoconfigure.data.mongo;
|
18 | 18 |
|
| 19 | +import java.util.Optional; |
| 20 | + |
| 21 | +import com.mongodb.ClientSessionOptions; |
| 22 | +import com.mongodb.reactivestreams.client.ClientSession; |
19 | 23 | import com.mongodb.reactivestreams.client.MongoClient;
|
| 24 | +import com.mongodb.reactivestreams.client.MongoDatabase; |
| 25 | +import org.bson.codecs.Codec; |
| 26 | +import org.bson.codecs.configuration.CodecRegistry; |
| 27 | +import reactor.core.publisher.Mono; |
20 | 28 |
|
21 | 29 | import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
22 | 30 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|
31 | 39 | import org.springframework.context.annotation.Import;
|
32 | 40 | import org.springframework.core.io.buffer.DataBufferFactory;
|
33 | 41 | import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
| 42 | +import org.springframework.dao.DataAccessException; |
| 43 | +import org.springframework.dao.support.PersistenceExceptionTranslator; |
34 | 44 | import org.springframework.data.mongodb.ReactiveMongoDatabaseFactory;
|
35 | 45 | import org.springframework.data.mongodb.core.ReactiveMongoOperations;
|
36 | 46 | import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
|
|
42 | 52 | import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
|
43 | 53 | import org.springframework.data.mongodb.gridfs.ReactiveGridFsOperations;
|
44 | 54 | import org.springframework.data.mongodb.gridfs.ReactiveGridFsTemplate;
|
| 55 | +import org.springframework.util.StringUtils; |
45 | 56 |
|
46 | 57 | /**
|
47 | 58 | * {@link EnableAutoConfiguration Auto-configuration} for Spring Data's reactive mongo
|
@@ -98,8 +109,77 @@ public DefaultDataBufferFactory dataBufferFactory() {
|
98 | 109 | @Bean
|
99 | 110 | @ConditionalOnMissingBean(ReactiveGridFsOperations.class)
|
100 | 111 | public ReactiveGridFsTemplate reactiveGridFsTemplate(ReactiveMongoDatabaseFactory reactiveMongoDatabaseFactory,
|
101 |
| - MappingMongoConverter mappingMongoConverter, DataBufferFactory dataBufferFactory) { |
102 |
| - return new ReactiveGridFsTemplate(dataBufferFactory, reactiveMongoDatabaseFactory, mappingMongoConverter, null); |
| 112 | + MappingMongoConverter mappingMongoConverter, DataBufferFactory dataBufferFactory, |
| 113 | + MongoProperties properties) { |
| 114 | + return new ReactiveGridFsTemplate(dataBufferFactory, |
| 115 | + new GridFsReactiveMongoDatabaseFactory(reactiveMongoDatabaseFactory, properties), mappingMongoConverter, |
| 116 | + null); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * {@link ReactiveMongoDatabaseFactory} decorator to use |
| 121 | + * {@link MongoProperties#getGridFsDatabase()} when set. |
| 122 | + */ |
| 123 | + static class GridFsReactiveMongoDatabaseFactory implements ReactiveMongoDatabaseFactory { |
| 124 | + |
| 125 | + private final ReactiveMongoDatabaseFactory delegate; |
| 126 | + |
| 127 | + private final MongoProperties properties; |
| 128 | + |
| 129 | + GridFsReactiveMongoDatabaseFactory(ReactiveMongoDatabaseFactory delegate, MongoProperties properties) { |
| 130 | + this.delegate = delegate; |
| 131 | + this.properties = properties; |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public boolean hasCodecFor(Class<?> type) { |
| 136 | + return this.delegate.hasCodecFor(type); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public Mono<MongoDatabase> getMongoDatabase() throws DataAccessException { |
| 141 | + String gridFsDatabase = this.properties.getGridFsDatabase(); |
| 142 | + if (StringUtils.hasText(gridFsDatabase)) { |
| 143 | + return this.delegate.getMongoDatabase(gridFsDatabase); |
| 144 | + } |
| 145 | + return this.delegate.getMongoDatabase(); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public Mono<MongoDatabase> getMongoDatabase(String dbName) throws DataAccessException { |
| 150 | + return this.delegate.getMongoDatabase(dbName); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public <T> Optional<Codec<T>> getCodecFor(Class<T> type) { |
| 155 | + return this.delegate.getCodecFor(type); |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public PersistenceExceptionTranslator getExceptionTranslator() { |
| 160 | + return this.delegate.getExceptionTranslator(); |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public CodecRegistry getCodecRegistry() { |
| 165 | + return this.delegate.getCodecRegistry(); |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public Mono<ClientSession> getSession(ClientSessionOptions options) { |
| 170 | + return this.delegate.getSession(options); |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public ReactiveMongoDatabaseFactory withSession(ClientSession session) { |
| 175 | + return this.delegate.withSession(session); |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + public boolean isTransactionActive() { |
| 180 | + return this.delegate.isTransactionActive(); |
| 181 | + } |
| 182 | + |
103 | 183 | }
|
104 | 184 |
|
105 | 185 | }
|
0 commit comments