From 76bf72c9f85bb7cb9d50cedbd096e639314350b3 Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Sat, 20 Aug 2011 03:02:20 +0000 Subject: [PATCH] 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 --- .../core/env/AbstractEnvironment.java | 12 +++++++++++- .../core/env/ConfigurableEnvironment.java | 17 +++++++++++++---- .../core/env/EnvironmentTests.java | 15 +++++++++++++++ 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/org.springframework.core/src/main/java/org/springframework/core/env/AbstractEnvironment.java b/org.springframework.core/src/main/java/org/springframework/core/env/AbstractEnvironment.java index 4c6e5e3bfd..5192c47e29 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/env/AbstractEnvironment.java +++ b/org.springframework.core/src/main/java/org/springframework/core/env/AbstractEnvironment.java @@ -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 { * @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 { *

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) { diff --git a/org.springframework.core/src/main/java/org/springframework/core/env/ConfigurableEnvironment.java b/org.springframework.core/src/main/java/org/springframework/core/env/ConfigurableEnvironment.java index 264645b36a..a49a5487a4 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/env/ConfigurableEnvironment.java +++ b/org.springframework.core/src/main/java/org/springframework/core/env/ConfigurableEnvironment.java @@ -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 * evaluated during container bootstrap to determine whether bean definitions * should be registered with the container. *

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 */ 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}. diff --git a/org.springframework.core/src/test/java/org/springframework/core/env/EnvironmentTests.java b/org.springframework.core/src/test/java/org/springframework/core/env/EnvironmentTests.java index f076735da1..78375120ff 100644 --- a/org.springframework.core/src/test/java/org/springframework/core/env/EnvironmentTests.java +++ b/org.springframework.core/src/test/java/org/springframework/core/env/EnvironmentTests.java @@ -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}));