diff --git a/core/src/main/java/org/testcontainers/utility/ImageNameSubstitutor.java b/core/src/main/java/org/testcontainers/utility/ImageNameSubstitutor.java index 54372cd726f..e46d091d657 100644 --- a/core/src/main/java/org/testcontainers/utility/ImageNameSubstitutor.java +++ b/core/src/main/java/org/testcontainers/utility/ImageNameSubstitutor.java @@ -4,7 +4,9 @@ import lombok.extern.slf4j.Slf4j; import org.testcontainers.UnstableAPI; +import java.util.ServiceLoader; import java.util.function.Function; +import java.util.stream.StreamSupport; /** * An image name substitutor converts a Docker image name, as may be specified in code, to an alternative name. @@ -25,26 +27,19 @@ public abstract class ImageNameSubstitutor implements Function clazz) throws IOException { + Path classFile = Paths.get(tempDir.toString(), classFilePath); + Files.createDirectories(classFile.getParent()); + Files.copy(clazz.getResourceAsStream("/" + classFilePath), classFile); + } + + private void createServiceProviderFile(Path metaInfDir, String serviceInterface, String... implementations) + throws IOException { + Path serviceFile = Paths.get(metaInfDir.toString(), serviceInterface); + try (FileWriter writer = new FileWriter(serviceFile.toFile())) { + for (String impl : implementations) { + writer.write(impl + "\n"); + } + } + } } diff --git a/docs/features/image_name_substitution.md b/docs/features/image_name_substitution.md index cf2b09d3d8e..0948e9fa2c7 100644 --- a/docs/features/image_name_substitution.md +++ b/docs/features/image_name_substitution.md @@ -125,6 +125,13 @@ Note that it is also possible to provide this same configuration property: Please see [the documentation on configuration mechanisms](./configuration.md) for more information. +Also, you can use the `ServiceLoader` mechanism to provide the fully qualified class name of the `ImageNameSubstitutor` implementation: + +=== "`src/test/resources/META-INF/services/org.testcontainers.utility.ImageNameSubstitutor`" + ```text + com.mycompany.testcontainers.ExampleImageNameSubstitutor + ``` + ## Overriding image names individually in configuration