Polish logging for core.env package

This commit is contained in:
Chris Beams
2011-11-26 05:20:32 +00:00
parent 91f05c8b9d
commit c6a0f1ef25
2 changed files with 45 additions and 4 deletions

View File

@@ -81,12 +81,15 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
private Set<String> activeProfiles = new LinkedHashSet<String>();
private Set<String> defaultProfiles = new LinkedHashSet<String>(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<String> 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<String> 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);
}

View File

@@ -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<PropertySource<?>> propertySourceList = new LinkedList<PropertySource<?>>();
/**
* 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.
*/