Skip to content

Commit f1b936e

Browse files
authored
Add support for a default environment (micronaut-projects#4027)
* Add support for a default environment * Add a whats new entry
1 parent 11eb8c1 commit f1b936e

File tree

8 files changed

+139
-7
lines changed

8 files changed

+139
-7
lines changed

inject/src/main/java/io/micronaut/context/ApplicationContextBuilder.java

+8
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ public interface ApplicationContextBuilder {
102102
*/
103103
@NonNull ApplicationContextBuilder environments(@Nullable String... environments);
104104

105+
/**
106+
* The environments to use if no other environments are specified.
107+
*
108+
* @param environments The environments
109+
* @return This builder
110+
*/
111+
@NonNull ApplicationContextBuilder defaultEnvironments(@Nullable String... environments);
112+
105113
/**
106114
* The packages to include for package scanning.
107115
*

inject/src/main/java/io/micronaut/context/ApplicationContextConfiguration.java

+9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import edu.umd.cs.findbugs.annotations.NonNull;
2222
import edu.umd.cs.findbugs.annotations.Nullable;
2323

24+
import java.util.Collections;
2425
import java.util.List;
2526
import java.util.Optional;
2627

@@ -45,6 +46,14 @@ default Optional<Boolean> getDeduceEnvironments() {
4546
return Optional.empty();
4647
}
4748

49+
/**
50+
* @return The default environments to be applied if no other environments
51+
* are explicitly specified.
52+
*/
53+
default List<String> getDefaultEnvironments() {
54+
return Collections.emptyList();
55+
}
56+
4857
/**
4958
* @return True if environment variables should contribute to configuration
5059
*/

inject/src/main/java/io/micronaut/context/DefaultApplicationContextBuilder.java

+14
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
public class DefaultApplicationContextBuilder implements ApplicationContextBuilder, ApplicationContextConfiguration {
3939
private List<Object> singletons = new ArrayList<>();
4040
private List<String> environments = new ArrayList<>();
41+
private List<String> defaultEnvironments = new ArrayList<>();
4142
private List<String> packages = new ArrayList<>();
4243
private Map<String, Object> properties = new LinkedHashMap<>();
4344
private List<PropertySource> propertySources = new ArrayList<>();
@@ -121,6 +122,14 @@ public ClassLoader getClassLoader() {
121122
return this;
122123
}
123124

125+
@Override
126+
public @NonNull ApplicationContextBuilder defaultEnvironments(@Nullable String... environments) {
127+
if (environments != null) {
128+
this.defaultEnvironments.addAll(Arrays.asList(environments));
129+
}
130+
return this;
131+
}
132+
124133
@Override
125134
public @NonNull ApplicationContextBuilder packages(@Nullable String... packages) {
126135
if (packages != null) {
@@ -177,6 +186,11 @@ public Optional<Boolean> getDeduceEnvironments() {
177186
return environments;
178187
}
179188

189+
@Override
190+
public @NonNull List<String> getDefaultEnvironments() {
191+
return defaultEnvironments;
192+
}
193+
180194
@Override
181195
public boolean isEnvironmentPropertySource() {
182196
return envPropertySource;

inject/src/main/java/io/micronaut/context/env/DefaultEnvironment.java

+12-7
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import java.util.concurrent.ConcurrentLinkedQueue;
5151
import java.util.concurrent.atomic.AtomicBoolean;
5252
import java.util.function.Function;
53+
import java.util.stream.Collectors;
5354
import java.util.stream.Stream;
5455

5556
/**
@@ -113,6 +114,17 @@ public DefaultEnvironment(@NonNull ApplicationContextConfiguration configuration
113114
Set<String> environments = new LinkedHashSet<>(3);
114115
List<String> specifiedNames = configuration.getEnvironments();
115116

117+
specifiedNames.addAll(0, Stream.of(System.getProperty(ENVIRONMENTS_PROPERTY),
118+
System.getenv(ENVIRONMENTS_ENV))
119+
.filter(StringUtils::isNotEmpty)
120+
.flatMap(s -> Arrays.stream(s.split(",")))
121+
.map(String::trim)
122+
.collect(Collectors.toList()));
123+
124+
if (specifiedNames.isEmpty()) {
125+
specifiedNames = configuration.getDefaultEnvironments();
126+
}
127+
116128
this.deduceEnvironments = configuration.getDeduceEnvironments().orElse(null);
117129
EnvironmentsAndPackage environmentsAndPackage = getEnvironmentsAndPackage(specifiedNames);
118130
environments.addAll(environmentsAndPackage.enviroments);
@@ -731,13 +743,6 @@ private static EnvironmentsAndPackage deduceEnvironmentsAndPackage(
731743
}
732744
}
733745

734-
Stream.of(System.getProperty(ENVIRONMENTS_PROPERTY),
735-
System.getenv(ENVIRONMENTS_ENV))
736-
.filter(StringUtils::isNotEmpty)
737-
.flatMap(s -> Arrays.stream(s.split(",")))
738-
.map(String::trim)
739-
.forEach(environments::add);
740-
741746
return environmentsAndPackage;
742747
}
743748

inject/src/test/groovy/io/micronaut/context/env/DefaultEnvironmentSpec.groovy

+71
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package io.micronaut.context.env
1717

1818
import com.github.stefanbirkner.systemlambda.SystemLambda
1919
import io.micronaut.context.ApplicationContext
20+
import io.micronaut.context.ApplicationContextConfiguration
2021
import io.micronaut.context.exceptions.ConfigurationException
2122
import io.micronaut.core.naming.NameUtils
2223
import spock.lang.Specification
@@ -590,6 +591,76 @@ class DefaultEnvironmentSpec extends Specification {
590591
env.activeNames == ["test", "first", "third", "second"] as Set
591592
}
592593

594+
void "test the default environment is applied"() {
595+
when:
596+
Environment env = new DefaultEnvironment(new ApplicationContextConfiguration() {
597+
@Override
598+
List<String> getEnvironments() {
599+
return []
600+
}
601+
602+
@Override
603+
List<String> getDefaultEnvironments() {
604+
return ['default']
605+
}
606+
}).start()
607+
608+
then:
609+
env.activeNames == ['test', 'default'] as Set
610+
611+
when: 'an environment is specified'
612+
env = new DefaultEnvironment(new ApplicationContextConfiguration() {
613+
@Override
614+
List<String> getEnvironments() {
615+
return ['foo']
616+
}
617+
618+
@Override
619+
List<String> getDefaultEnvironments() {
620+
return ['default']
621+
}
622+
}).start()
623+
624+
then: 'the default environment is not applied'
625+
env.activeNames == ['test', 'foo'] as Set
626+
627+
when: 'an environment is specified through env var'
628+
SystemLambda.withEnvironmentVariable("MICRONAUT_ENVIRONMENTS", "bar")
629+
.execute(() -> {
630+
env = new DefaultEnvironment(new ApplicationContextConfiguration() {
631+
@Override
632+
List<String> getEnvironments() {
633+
return []
634+
}
635+
636+
@Override
637+
List<String> getDefaultEnvironments() {
638+
return ['default']
639+
}
640+
}).start()
641+
})
642+
643+
then: 'the default environment is not applied'
644+
env.activeNames == ['test', 'bar'] as Set
645+
646+
when: 'an environment is specified through a system prop'
647+
System.setProperty('micronaut.environments', 'xyz')
648+
env = new DefaultEnvironment(new ApplicationContextConfiguration() {
649+
@Override
650+
List<String> getEnvironments() {
651+
return []
652+
}
653+
654+
@Override
655+
List<String> getDefaultEnvironments() {
656+
return ['default']
657+
}
658+
}).start()
659+
660+
then: 'the default environment is not applied'
661+
env.activeNames == ['test', 'xyz'] as Set
662+
}
663+
593664
private static Environment startEnv(String files) {
594665
new DefaultEnvironment({["test"]}) {
595666
@Override

runtime/src/main/java/io/micronaut/runtime/Micronaut.java

+5
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,11 @@ protected Micronaut() {
231231
return (Micronaut) super.environments(environments);
232232
}
233233

234+
@Override
235+
public @NonNull Micronaut defaultEnvironments(@Nullable String... environments) {
236+
return (Micronaut) super.defaultEnvironments(environments);
237+
}
238+
234239
@Override
235240
public @NonNull Micronaut packages(@Nullable String... packages) {
236241
return (Micronaut) super.packages(packages);

src/main/docs/guide/config/environments.adoc

+16
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,19 @@ $ java -Dmicronaut.env.deduction=false -jar myapp.jar
4646
Alternatively, you can disable environment deduction using the api:context.ApplicationContextBuilder[]'s `deduceEnvironment` method when setting up your application.
4747

4848
snippet::io.micronaut.docs.context.env.DefaultEnvironmentSpec[tags="disableEnvDeduction",indent=0,title="Using ApplicationContextBuilder to disable environment deduction"]
49+
50+
== Default Environment
51+
52+
Micronaut supports the concept of one or many default environments. A default environment is one that is only applied if no other environments are explicitly specified. Environments can be explicitly specified either through the application context builder `Micronaut.build().environments(...)`, through the `micronaut.environments` system property, or the `MICRONAUT_ENVIRONMENTS` environment variable. If an environment is configured through any of the above means, the default environment will *not* be applied.
53+
54+
To set the default environments, modify your application main method:
55+
56+
[source,java]
57+
----
58+
public static void main(String[] args) {
59+
Micronaut.build(args)
60+
.mainClass(Application.class)
61+
.defaultEnvironments("dev")
62+
.start();
63+
}
64+
----

src/main/docs/guide/introduction/whatsNew.adoc

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ Query parameters are now respected and bindable in the Micronaut websocket suppo
2222

2323
Cookies in HTTP responses from the client side are now retrievable on the HttpResponse. These are the cookies found in the `Set-Cookie` header.
2424

25+
==== Default Environment
26+
27+
Micronaut 2.1 introduces the concept of a default environment. One or more default environments can be set and they will apply if no other environments are explicitly specified. See the <<environments, environments documentation>> for information on how to use this new feature.
28+
2529
==== Mandatory upgrade for Netty tcnative
2630

2731
Due to a new https://github.com/netty/netty/pull/10407[SSL default value introduced in Netty 4.1.52], if you are using tcnative you need to update it to `2.0.33.Final`.

0 commit comments

Comments
 (0)