Browse Source

Introduce ConfigurableEnvironment#addActiveProfile

Provides a convenient mechanism for activating an additional profile
while preserving those that are already active, as opposed to
calling #setActiveProfiles with the contents of #getActiveProfiles plus
the new profile.

Issue: SPR-8548
pull/7/head
Chris Beams 13 years ago
parent
commit
76bf72c9f8
  1. 12
      org.springframework.core/src/main/java/org/springframework/core/env/AbstractEnvironment.java
  2. 17
      org.springframework.core/src/main/java/org/springframework/core/env/ConfigurableEnvironment.java
  3. 15
      org.springframework.core/src/test/java/org/springframework/core/env/EnvironmentTests.java

12
org.springframework.core/src/main/java/org/springframework/core/env/AbstractEnvironment.java vendored

@ -188,8 +188,16 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment { @@ -188,8 +188,16 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
}
public void setActiveProfiles(String... profiles) {
Assert.notNull(profiles, "Profile array must not be null");
this.activeProfiles.clear();
this.activeProfiles.addAll(Arrays.asList(profiles));
for (String profile : profiles) {
this.addActiveProfile(profile);
}
}
public void addActiveProfile(String profile) {
this.validateProfile(profile);
this.activeProfiles.add(profile);
}
public String[] getDefaultProfiles() {
@ -226,6 +234,7 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment { @@ -226,6 +234,7 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
* @see #getReservedDefaultProfiles()
*/
public void setDefaultProfiles(String... profiles) {
Assert.notNull(profiles, "Profile array must not be null");
this.defaultProfiles.clear();
for (String profile : profiles) {
this.validateProfile(profile);
@ -255,6 +264,7 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment { @@ -255,6 +264,7 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
* <p>Subclasses may override to impose further restrictions on profile syntax.
* @throws IllegalArgumentException if the profile is null, empty or whitespace-only
* @see #acceptsProfiles
* @see #addActiveProfile
* @see #setDefaultProfiles
*/
protected void validateProfile(String profile) {

17
org.springframework.core/src/main/java/org/springframework/core/env/ConfigurableEnvironment.java vendored

@ -19,9 +19,9 @@ package org.springframework.core.env; @@ -19,9 +19,9 @@ package org.springframework.core.env;
import java.util.Map;
/**
* Configuration interface to be implemented by most if not all {@link Environment
* Environments}. Provides facilities for setting active and default profiles as well
* as accessing the {@linkplain #getPropertySources() property sources}.
* Configuration interface to be implemented by most if not all {@link Environment} types.
* Provides facilities for setting active and default profiles as well
* as accessing underlying {@linkplain #getPropertySources() property sources}.
*
* @author Chris Beams
* @since 3.1
@ -35,8 +35,10 @@ public interface ConfigurableEnvironment extends Environment, ConfigurableProper @@ -35,8 +35,10 @@ public interface ConfigurableEnvironment extends Environment, ConfigurableProper
* evaluated during container bootstrap to determine whether bean definitions
* should be registered with the container.
* <p>Any existing active profiles will be replaced with the given arguments; call
* with zero arguments to clear the current set of active profiles.
* with zero arguments to clear the current set of active profiles. Use
* {@link #addActiveProfile} to add a profile while preserving the existing set.
*
* @see #addActiveProfile
* @see #setDefaultProfiles
* @see org.springframework.context.annotation.Profile
* @see AbstractEnvironment#ACTIVE_PROFILES_PROPERTY_NAME
@ -44,6 +46,13 @@ public interface ConfigurableEnvironment extends Environment, ConfigurableProper @@ -44,6 +46,13 @@ public interface ConfigurableEnvironment extends Environment, ConfigurableProper
*/
void setActiveProfiles(String... profiles);
/**
* Add a profile to the current set of active profiles.
* @see #setActiveProfiles
* @throws IllegalArgumentException if the profile is null, empty or whitespace-only
*/
void addActiveProfile(String profile);
/**
* Specify the set of profiles to be made active by default if no other profiles
* are explicitly made active through {@link #setActiveProfiles}.

15
org.springframework.core/src/test/java/org/springframework/core/env/EnvironmentTests.java vendored

@ -120,6 +120,21 @@ public class EnvironmentTests { @@ -120,6 +120,21 @@ public class EnvironmentTests {
environment.setDefaultProfiles("");
}
@Test
public void addActiveProfile() {
assertThat(environment.getActiveProfiles().length, is(0));
environment.setActiveProfiles("local", "embedded");
assertThat(Arrays.asList(environment.getActiveProfiles()), hasItems("local", "embedded"));
assertThat(environment.getActiveProfiles().length, is(2));
environment.addActiveProfile("p1");
assertThat(Arrays.asList(environment.getActiveProfiles()), hasItems("p1"));
assertThat(environment.getActiveProfiles().length, is(3));
environment.addActiveProfile("p2");
environment.addActiveProfile("p3");
assertThat(Arrays.asList(environment.getActiveProfiles()), hasItems("p2", "p3"));
assertThat(environment.getActiveProfiles().length, is(5));
}
@Test
public void reservedDefaultProfile() {
assertThat(environment.getDefaultProfiles(), equalTo(new String[]{RESERVED_DEFAULT_PROFILE_NAME}));

Loading…
Cancel
Save