|
18 | 18 | import java.time.Duration;
|
19 | 19 | import java.util.ArrayList;
|
20 | 20 | import java.util.Arrays;
|
| 21 | +import java.util.HashMap; |
21 | 22 | import java.util.List;
|
| 23 | +import java.util.Map; |
22 | 24 | import java.util.Objects;
|
23 | 25 | import java.util.concurrent.Future;
|
| 26 | +import java.util.function.Function; |
| 27 | +import java.util.function.Predicate; |
| 28 | +import java.util.function.Supplier; |
24 | 29 | import java.util.stream.Collectors;
|
25 | 30 | import java.util.stream.Stream;
|
26 | 31 |
|
@@ -56,6 +61,9 @@ public abstract class ApplicationServerContainer extends GenericContainer<Applic
|
56 | 61 | // Expected path for Microprofile platforms to query for readiness
|
57 | 62 | static final String MP_HEALTH_READINESS_PATH = "/health/ready";
|
58 | 63 |
|
| 64 | + // Lazy environment variables |
| 65 | + private Map<String, Supplier<String>> lazyEnvVars = new HashMap<>(); |
| 66 | + |
59 | 67 | //Constructors
|
60 | 68 |
|
61 | 69 | public ApplicationServerContainer(@NonNull final Future<String> image) {
|
@@ -83,6 +91,11 @@ protected void configure() {
|
83 | 91 | for(MountableFile archive : archives) {
|
84 | 92 | withCopyFileToContainer(archive, getApplicationInstallDirectory() + extractApplicationName(archive));
|
85 | 93 | }
|
| 94 | + |
| 95 | + // Add lazy env variables |
| 96 | + for(Map.Entry<String, Supplier<String>> entry : lazyEnvVars.entrySet()) { |
| 97 | + withEnv(entry.getKey(), entry.getValue().get()); |
| 98 | + } |
86 | 99 | }
|
87 | 100 |
|
88 | 101 | @Override
|
@@ -236,6 +249,27 @@ public ApplicationServerContainer withHttpWaitTimeout(Duration httpWaitTimeout)
|
236 | 249 | return this;
|
237 | 250 | }
|
238 | 251 |
|
| 252 | + /** |
| 253 | + * An environment variable whose value needs to wait until the configuration stage to be evaluated. |
| 254 | + * The common use case for this would be to pass inter-container connection data. |
| 255 | + * |
| 256 | + * For example an application container often needs to have connection data to a dependent database: |
| 257 | + * <pre> |
| 258 | + * OracleContainer db = new OracleContainer(...).withExposedPorts(1521); |
| 259 | + * ApplicationContainer app = new ApplicationContainer(...) |
| 260 | + * .withLazyEnv( "oracle.url", () -> db.getJdbcUrl() ) // Need to wait until 'db' container is configured |
| 261 | + * .dependsOn(db); |
| 262 | + * </pre> |
| 263 | + * |
| 264 | + * @param key - the key |
| 265 | + * @param valueSupplier - the function that supplies the value |
| 266 | + * @return self |
| 267 | + */ |
| 268 | + public ApplicationServerContainer withLazyEnv(String key, Supplier<String> valueSupplier) { |
| 269 | + lazyEnvVars.put(key, valueSupplier); |
| 270 | + return this; |
| 271 | + } |
| 272 | + |
239 | 273 | //Getters
|
240 | 274 |
|
241 | 275 | /**
|
|
0 commit comments