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 1fb99948f5..ece942e89b 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 @@ -81,12 +81,15 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment { private Set activeProfiles = new LinkedHashSet(); private Set defaultProfiles = new LinkedHashSet(this.getReservedDefaultProfiles()); - private final MutablePropertySources propertySources = new MutablePropertySources(); + private final MutablePropertySources propertySources = new MutablePropertySources(logger); private final ConfigurablePropertyResolver propertyResolver = new PropertySourcesPropertyResolver(propertySources); public AbstractEnvironment() { + String name = this.getClass().getSimpleName(); + logger.debug(String.format("Initializing new %s", name)); this.customizePropertySources(propertySources); + logger.debug(String.format("Initialized %s with PropertySources %s", name, propertySources)); } /** @@ -187,7 +190,7 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment { */ protected Set doGetActiveProfiles() { if (this.activeProfiles.isEmpty()) { - String profiles = this.propertyResolver.getProperty(ACTIVE_PROFILES_PROPERTY_NAME); + String profiles = this.getProperty(ACTIVE_PROFILES_PROPERTY_NAME); if (StringUtils.hasText(profiles)) { setActiveProfiles(commaDelimitedListToStringArray(trimAllWhitespace(profiles))); } @@ -204,6 +207,7 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment { } public void addActiveProfile(String profile) { + logger.debug(String.format("Activating profile '%s'", profile)); this.validateProfile(profile); this.activeProfiles.add(profile); } @@ -226,7 +230,7 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment { */ protected Set doGetDefaultProfiles() { if (this.defaultProfiles.equals(this.getReservedDefaultProfiles())) { - String profiles = this.propertyResolver.getProperty(DEFAULT_PROFILES_PROPERTY_NAME); + String profiles = this.getProperty(DEFAULT_PROFILES_PROPERTY_NAME); if (StringUtils.hasText(profiles)) { this.setDefaultProfiles(commaDelimitedListToStringArray(trimAllWhitespace(profiles))); } @@ -413,7 +417,7 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment { @Override public String toString() { - return format("%s [activeProfiles=%s, defaultProfiles=%s, propertySources=%s]", + return format("%s {activeProfiles=%s, defaultProfiles=%s, propertySources=%s}", getClass().getSimpleName(), this.activeProfiles, this.defaultProfiles, this.propertySources); } diff --git a/org.springframework.core/src/main/java/org/springframework/core/env/MutablePropertySources.java b/org.springframework.core/src/main/java/org/springframework/core/env/MutablePropertySources.java index c24e6267f4..64614536c6 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/env/MutablePropertySources.java +++ b/org.springframework.core/src/main/java/org/springframework/core/env/MutablePropertySources.java @@ -19,7 +19,10 @@ package org.springframework.core.env; import java.util.Iterator; import java.util.LinkedList; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.springframework.util.Assert; +import org.springframework.util.StringUtils; /** * Default implementation of the {@link PropertySources} interface. @@ -39,12 +42,16 @@ public class MutablePropertySources implements PropertySources { static final String NON_EXISTENT_PROPERTY_SOURCE_MESSAGE = "PropertySource named [%s] does not exist"; static final String ILLEGAL_RELATIVE_ADDITION_MESSAGE = "PropertySource named [%s] cannot be added relative to itself"; + private final Log logger; + private final LinkedList> propertySourceList = new LinkedList>(); + /** * Create a new {@link MutablePropertySources} object. */ public MutablePropertySources() { + this.logger = LogFactory.getLog(this.getClass()); } /** @@ -52,11 +59,21 @@ public class MutablePropertySources implements PropertySources { * object, preserving the original order of contained {@code PropertySource} objects. */ public MutablePropertySources(PropertySources propertySources) { + this(); for (PropertySource propertySource : propertySources) { this.addLast(propertySource); } } + /** + * Create a new {@link MutablePropertySources} object and inheriting the given logger, + * usually from an enclosing {@link Environment}. + */ + MutablePropertySources(Log logger) { + this.logger = logger; + } + + public boolean contains(String name) { return this.propertySourceList.contains(PropertySource.named(name)); } @@ -73,6 +90,8 @@ public class MutablePropertySources implements PropertySources { * Add the given property source object with highest precedence. */ public void addFirst(PropertySource propertySource) { + logger.debug(String.format("Adding [%s] PropertySource with highest search precedence", + propertySource.getName())); removeIfPresent(propertySource); this.propertySourceList.addFirst(propertySource); } @@ -81,6 +100,8 @@ public class MutablePropertySources implements PropertySources { * Add the given property source object with lowest precedence. */ public void addLast(PropertySource propertySource) { + logger.debug(String.format("Adding [%s] PropertySource with lowest search precedence", + propertySource.getName())); removeIfPresent(propertySource); this.propertySourceList.addLast(propertySource); } @@ -90,6 +111,8 @@ public class MutablePropertySources implements PropertySources { * than the named relative property source. */ public void addBefore(String relativePropertySourceName, PropertySource propertySource) { + logger.debug(String.format("Adding [%s] PropertySource with search precedence immediately higher than [%s]", + propertySource.getName(), relativePropertySourceName)); assertLegalRelativeAddition(relativePropertySourceName, propertySource); removeIfPresent(propertySource); int index = assertPresentAndGetIndex(relativePropertySourceName); @@ -101,6 +124,8 @@ public class MutablePropertySources implements PropertySources { * than the named relative property source. */ public void addAfter(String relativePropertySourceName, PropertySource propertySource) { + logger.debug(String.format("Adding [%s] PropertySource with search precedence immediately lower than [%s]", + propertySource.getName(), relativePropertySourceName)); assertLegalRelativeAddition(relativePropertySourceName, propertySource); removeIfPresent(propertySource); int index = assertPresentAndGetIndex(relativePropertySourceName); @@ -119,6 +144,7 @@ public class MutablePropertySources implements PropertySources { * @param name the name of the property source to find and remove */ public PropertySource remove(String name) { + logger.debug(String.format("Removing [%s] PropertySource", name)); int index = this.propertySourceList.indexOf(PropertySource.named(name)); if (index >= 0) { return this.propertySourceList.remove(index); @@ -134,6 +160,8 @@ public class MutablePropertySources implements PropertySources { * @see #contains */ public void replace(String name, PropertySource propertySource) { + logger.debug(String.format("Replacing [%s] PropertySource with [%s]", + name, propertySource.getName())); int index = assertPresentAndGetIndex(name); this.propertySourceList.set(index, propertySource); } @@ -145,6 +173,15 @@ public class MutablePropertySources implements PropertySources { return this.propertySourceList.size(); } + @Override + public synchronized String toString() { + String[] names = new String[this.size()]; + for (int i=0; i < size(); i++) { + names[i] = this.propertySourceList.get(i).getName(); + } + return String.format("[%s]", StringUtils.arrayToCommaDelimitedString(names)); + } + /** * Ensure that the given property source is not being added relative to itself. */