diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/DownloadConfigBuilderCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/DownloadConfigBuilderCustomizer.java new file mode 100644 index 0000000000..06c111409e --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/DownloadConfigBuilderCustomizer.java @@ -0,0 +1,39 @@ +/* + * Copyright 2012-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.mongo.embedded; + +import de.flapdoodle.embed.mongo.config.DownloadConfigBuilder; +import de.flapdoodle.embed.process.config.store.IDownloadConfig; + +/** + * Callback interface that can be implemented by beans wishing to customize the + * {@link IDownloadConfig} via a {@link DownloadConfigBuilder} whilst retaining default + * auto-configuration. + * + * @author Michael Gmeiner + * @since 2.2.0 + */ +@FunctionalInterface +public interface DownloadConfigBuilderCustomizer { + + /** + * Customize the {@link DownloadConfigBuilder}. + * @param downloadConfigBuilder the {@link DownloadConfigBuilder} to customize + */ + void customize(DownloadConfigBuilder downloadConfigBuilder); + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.java index afc8d0f4ed..dcb889f8f2 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,6 +21,7 @@ import java.net.InetAddress; import java.net.UnknownHostException; import java.util.HashMap; import java.util.Map; +import java.util.stream.Stream; import com.mongodb.MongoClient; import de.flapdoodle.embed.mongo.Command; @@ -39,6 +40,7 @@ import de.flapdoodle.embed.mongo.distribution.Version; import de.flapdoodle.embed.mongo.distribution.Versions; import de.flapdoodle.embed.process.config.IRuntimeConfig; import de.flapdoodle.embed.process.config.io.ProcessOutput; +import de.flapdoodle.embed.process.config.store.IDownloadConfig; import de.flapdoodle.embed.process.distribution.GenericVersion; import de.flapdoodle.embed.process.io.Processors; import de.flapdoodle.embed.process.io.Slf4jLevel; @@ -48,6 +50,7 @@ import de.flapdoodle.embed.process.store.ArtifactStoreBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; @@ -203,7 +206,8 @@ public class EmbeddedMongoAutoConfiguration { static class RuntimeConfigConfiguration { @Bean - public IRuntimeConfig embeddedMongoRuntimeConfig() { + public IRuntimeConfig embeddedMongoRuntimeConfig( + ObjectProvider downloadConfigBuilderCustomizers) { Logger logger = LoggerFactory .getLogger(getClass().getPackage().getName() + ".EmbeddedMongo"); ProcessOutput processOutput = new ProcessOutput( @@ -211,15 +215,21 @@ public class EmbeddedMongoAutoConfiguration { Processors.logTo(logger, Slf4jLevel.ERROR), Processors.named( "[console>]", Processors.logTo(logger, Slf4jLevel.DEBUG))); return new RuntimeConfigBuilder().defaultsWithLogger(Command.MongoD, logger) - .processOutput(processOutput).artifactStore(getArtifactStore(logger)) + .processOutput(processOutput).artifactStore(getArtifactStore(logger, + downloadConfigBuilderCustomizers.orderedStream())) .build(); } - private ArtifactStoreBuilder getArtifactStore(Logger logger) { + private ArtifactStoreBuilder getArtifactStore(Logger logger, + Stream downloadConfigBuilderCustomizers) { + DownloadConfigBuilder downloadConfigBuilder = new DownloadConfigBuilder() + .defaultsForCommand(Command.MongoD); + downloadConfigBuilder.progressListener(new Slf4jProgressListener(logger)); + downloadConfigBuilderCustomizers + .forEach((customizer) -> customizer.customize(downloadConfigBuilder)); + IDownloadConfig downloadConfig = downloadConfigBuilder.build(); return new ExtractedArtifactStoreBuilder().defaults(Command.MongoD) - .download(new DownloadConfigBuilder() - .defaultsForCommand(Command.MongoD) - .progressListener(new Slf4jProgressListener(logger)).build()); + .download(downloadConfig); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfigurationTests.java index bb73d99013..928f0d2547 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,12 +26,15 @@ import de.flapdoodle.embed.mongo.config.IMongodConfig; import de.flapdoodle.embed.mongo.config.Storage; import de.flapdoodle.embed.mongo.distribution.Feature; import de.flapdoodle.embed.mongo.distribution.Version; +import de.flapdoodle.embed.process.config.IRuntimeConfig; +import de.flapdoodle.embed.process.config.store.IDownloadConfig; import org.bson.Document; import org.junit.After; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.springframework.beans.DirectFieldAccessor; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration; @@ -180,6 +183,15 @@ public class EmbeddedMongoAutoConfigurationTests { .isEqualTo("testing"); } + @Test + public void customizeDownloadConfiguration() { + load(DownloadConfigBuilderCustomizerConfiguration.class); + IRuntimeConfig runtimeConfig = this.context.getBean(IRuntimeConfig.class); + IDownloadConfig downloadConfig = (IDownloadConfig) new DirectFieldAccessor( + runtimeConfig.getArtifactStore()).getPropertyValue("downloadConfig"); + assertThat(downloadConfig.getUserAgent()).isEqualTo("Test User Agent"); + } + private void assertVersionConfiguration(String configuredVersion, String expectedVersion) { this.context = new AnnotationConfigApplicationContext(); @@ -227,4 +239,16 @@ public class EmbeddedMongoAutoConfigurationTests { } + @Configuration + static class DownloadConfigBuilderCustomizerConfiguration { + + @Bean + public DownloadConfigBuilderCustomizer testDownloadConfigBuilderCustomizer() { + return (downloadConfigBuilder) -> { + downloadConfigBuilder.userAgent("Test User Agent"); + }; + } + + } + } diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index 5d3059b5e6..5c4335b47d 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -4292,7 +4292,8 @@ If you have SLF4J on the classpath, the output produced by Mongo is automaticall to a logger named `org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongo`. You can declare your own `IMongodConfig` and `IRuntimeConfig` beans to take control of -the Mongo instance's configuration and logging routing. +the Mongo instance's configuration and logging routing. The download configuration can be +customized by declaring a `DownloadConfigBuilderCustomizer` bean.