Skip to content

Commit 08757d7

Browse files
committed
Merge pull request #15496 from mgmeiner
* pr/15496: Polish "Allow easy customization of EmbeddedMongo DownloadConfig" Allow easy customization of EmbeddedMongo DownloadConfig
2 parents 4f207d2 + 6ba1f40 commit 08757d7

File tree

4 files changed

+83
-9
lines changed

4 files changed

+83
-9
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.mongo.embedded;
18+
19+
import de.flapdoodle.embed.mongo.config.DownloadConfigBuilder;
20+
import de.flapdoodle.embed.process.config.store.IDownloadConfig;
21+
22+
/**
23+
* Callback interface that can be implemented by beans wishing to customize the
24+
* {@link IDownloadConfig} via a {@link DownloadConfigBuilder} whilst retaining default
25+
* auto-configuration.
26+
*
27+
* @author Michael Gmeiner
28+
* @since 2.2.0
29+
*/
30+
@FunctionalInterface
31+
public interface DownloadConfigBuilderCustomizer {
32+
33+
/**
34+
* Customize the {@link DownloadConfigBuilder}.
35+
* @param downloadConfigBuilder the {@link DownloadConfigBuilder} to customize
36+
*/
37+
void customize(DownloadConfigBuilder downloadConfigBuilder);
38+
39+
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121
import java.net.UnknownHostException;
2222
import java.util.HashMap;
2323
import java.util.Map;
24+
import java.util.stream.Stream;
2425

2526
import com.mongodb.MongoClient;
2627
import de.flapdoodle.embed.mongo.Command;
@@ -39,6 +40,7 @@
3940
import de.flapdoodle.embed.mongo.distribution.Versions;
4041
import de.flapdoodle.embed.process.config.IRuntimeConfig;
4142
import de.flapdoodle.embed.process.config.io.ProcessOutput;
43+
import de.flapdoodle.embed.process.config.store.IDownloadConfig;
4244
import de.flapdoodle.embed.process.distribution.GenericVersion;
4345
import de.flapdoodle.embed.process.io.Processors;
4446
import de.flapdoodle.embed.process.io.Slf4jLevel;
@@ -48,6 +50,7 @@
4850
import org.slf4j.Logger;
4951
import org.slf4j.LoggerFactory;
5052

53+
import org.springframework.beans.factory.ObjectProvider;
5154
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
5255
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
5356
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@@ -203,23 +206,30 @@ private Map<String, Object> getMongoPorts(MutablePropertySources sources) {
203206
static class RuntimeConfigConfiguration {
204207

205208
@Bean
206-
public IRuntimeConfig embeddedMongoRuntimeConfig() {
209+
public IRuntimeConfig embeddedMongoRuntimeConfig(
210+
ObjectProvider<DownloadConfigBuilderCustomizer> downloadConfigBuilderCustomizers) {
207211
Logger logger = LoggerFactory
208212
.getLogger(getClass().getPackage().getName() + ".EmbeddedMongo");
209213
ProcessOutput processOutput = new ProcessOutput(
210214
Processors.logTo(logger, Slf4jLevel.INFO),
211215
Processors.logTo(logger, Slf4jLevel.ERROR), Processors.named(
212216
"[console>]", Processors.logTo(logger, Slf4jLevel.DEBUG)));
213217
return new RuntimeConfigBuilder().defaultsWithLogger(Command.MongoD, logger)
214-
.processOutput(processOutput).artifactStore(getArtifactStore(logger))
218+
.processOutput(processOutput).artifactStore(getArtifactStore(logger,
219+
downloadConfigBuilderCustomizers.orderedStream()))
215220
.build();
216221
}
217222

218-
private ArtifactStoreBuilder getArtifactStore(Logger logger) {
223+
private ArtifactStoreBuilder getArtifactStore(Logger logger,
224+
Stream<DownloadConfigBuilderCustomizer> downloadConfigBuilderCustomizers) {
225+
DownloadConfigBuilder downloadConfigBuilder = new DownloadConfigBuilder()
226+
.defaultsForCommand(Command.MongoD);
227+
downloadConfigBuilder.progressListener(new Slf4jProgressListener(logger));
228+
downloadConfigBuilderCustomizers
229+
.forEach((customizer) -> customizer.customize(downloadConfigBuilder));
230+
IDownloadConfig downloadConfig = downloadConfigBuilder.build();
219231
return new ExtractedArtifactStoreBuilder().defaults(Command.MongoD)
220-
.download(new DownloadConfigBuilder()
221-
.defaultsForCommand(Command.MongoD)
222-
.progressListener(new Slf4jProgressListener(logger)).build());
232+
.download(downloadConfig);
223233
}
224234

225235
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfigurationTests.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,12 +26,15 @@
2626
import de.flapdoodle.embed.mongo.config.Storage;
2727
import de.flapdoodle.embed.mongo.distribution.Feature;
2828
import de.flapdoodle.embed.mongo.distribution.Version;
29+
import de.flapdoodle.embed.process.config.IRuntimeConfig;
30+
import de.flapdoodle.embed.process.config.store.IDownloadConfig;
2931
import org.bson.Document;
3032
import org.junit.After;
3133
import org.junit.Rule;
3234
import org.junit.Test;
3335
import org.junit.rules.TemporaryFolder;
3436

37+
import org.springframework.beans.DirectFieldAccessor;
3538
import org.springframework.beans.factory.annotation.Value;
3639
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
3740
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
@@ -180,6 +183,15 @@ public void customReplicaSetNameIsAppliedToConfiguration() {
180183
.isEqualTo("testing");
181184
}
182185

186+
@Test
187+
public void customizeDownloadConfiguration() {
188+
load(DownloadConfigBuilderCustomizerConfiguration.class);
189+
IRuntimeConfig runtimeConfig = this.context.getBean(IRuntimeConfig.class);
190+
IDownloadConfig downloadConfig = (IDownloadConfig) new DirectFieldAccessor(
191+
runtimeConfig.getArtifactStore()).getPropertyValue("downloadConfig");
192+
assertThat(downloadConfig.getUserAgent()).isEqualTo("Test User Agent");
193+
}
194+
183195
private void assertVersionConfiguration(String configuredVersion,
184196
String expectedVersion) {
185197
this.context = new AnnotationConfigApplicationContext();
@@ -227,4 +239,16 @@ public MongoClient mongoClient(@Value("${local.mongo.port}") int port) {
227239

228240
}
229241

242+
@Configuration
243+
static class DownloadConfigBuilderCustomizerConfiguration {
244+
245+
@Bean
246+
public DownloadConfigBuilderCustomizer testDownloadConfigBuilderCustomizer() {
247+
return (downloadConfigBuilder) -> {
248+
downloadConfigBuilder.userAgent("Test User Agent");
249+
};
250+
}
251+
252+
}
253+
230254
}

spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4292,7 +4292,8 @@ If you have SLF4J on the classpath, the output produced by Mongo is automaticall
42924292
to a logger named `org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongo`.
42934293

42944294
You can declare your own `IMongodConfig` and `IRuntimeConfig` beans to take control of
4295-
the Mongo instance's configuration and logging routing.
4295+
the Mongo instance's configuration and logging routing. The download configuration can be
4296+
customized by declaring a `DownloadConfigBuilderCustomizer` bean.
42964297

42974298

42984299

0 commit comments

Comments
 (0)