|
|
|
@ -17,12 +17,12 @@ |
|
|
|
package org.springframework.core.env; |
|
|
|
package org.springframework.core.env; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.Iterator; |
|
|
|
import java.util.Iterator; |
|
|
|
import java.util.LinkedList; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
import java.util.concurrent.CopyOnWriteArrayList; |
|
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.logging.Log; |
|
|
|
import org.apache.commons.logging.Log; |
|
|
|
import org.apache.commons.logging.LogFactory; |
|
|
|
import org.apache.commons.logging.LogFactory; |
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.util.Assert; |
|
|
|
|
|
|
|
import org.springframework.util.StringUtils; |
|
|
|
import org.springframework.util.StringUtils; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
@ -35,24 +35,22 @@ import org.springframework.util.StringUtils; |
|
|
|
* will be searched when resolving a given property with a {@link PropertyResolver}. |
|
|
|
* will be searched when resolving a given property with a {@link PropertyResolver}. |
|
|
|
* |
|
|
|
* |
|
|
|
* @author Chris Beams |
|
|
|
* @author Chris Beams |
|
|
|
|
|
|
|
* @author Juergen Hoeller |
|
|
|
* @since 3.1 |
|
|
|
* @since 3.1 |
|
|
|
* @see PropertySourcesPropertyResolver |
|
|
|
* @see PropertySourcesPropertyResolver |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class MutablePropertySources implements PropertySources { |
|
|
|
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 Log logger; |
|
|
|
|
|
|
|
|
|
|
|
private final LinkedList<PropertySource<?>> propertySourceList = new LinkedList<PropertySource<?>>(); |
|
|
|
private final List<PropertySource<?>> propertySourceList = new CopyOnWriteArrayList<PropertySource<?>>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Create a new {@link MutablePropertySources} object. |
|
|
|
* Create a new {@link MutablePropertySources} object. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public MutablePropertySources() { |
|
|
|
public MutablePropertySources() { |
|
|
|
this.logger = LogFactory.getLog(this.getClass()); |
|
|
|
this.logger = LogFactory.getLog(getClass()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
@ -62,7 +60,7 @@ public class MutablePropertySources implements PropertySources { |
|
|
|
public MutablePropertySources(PropertySources propertySources) { |
|
|
|
public MutablePropertySources(PropertySources propertySources) { |
|
|
|
this(); |
|
|
|
this(); |
|
|
|
for (PropertySource<?> propertySource : propertySources) { |
|
|
|
for (PropertySource<?> propertySource : propertySources) { |
|
|
|
this.addLast(propertySource); |
|
|
|
addLast(propertySource); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -83,7 +81,7 @@ public class MutablePropertySources implements PropertySources { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public PropertySource<?> get(String name) { |
|
|
|
public PropertySource<?> get(String name) { |
|
|
|
int index = this.propertySourceList.indexOf(PropertySource.named(name)); |
|
|
|
int index = this.propertySourceList.indexOf(PropertySource.named(name)); |
|
|
|
return index == -1 ? null : this.propertySourceList.get(index); |
|
|
|
return (index != -1 ? this.propertySourceList.get(index) : null); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
@ -100,7 +98,7 @@ public class MutablePropertySources implements PropertySources { |
|
|
|
propertySource.getName())); |
|
|
|
propertySource.getName())); |
|
|
|
} |
|
|
|
} |
|
|
|
removeIfPresent(propertySource); |
|
|
|
removeIfPresent(propertySource); |
|
|
|
this.propertySourceList.addFirst(propertySource); |
|
|
|
this.propertySourceList.add(0, propertySource); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
@ -112,7 +110,7 @@ public class MutablePropertySources implements PropertySources { |
|
|
|
propertySource.getName())); |
|
|
|
propertySource.getName())); |
|
|
|
} |
|
|
|
} |
|
|
|
removeIfPresent(propertySource); |
|
|
|
removeIfPresent(propertySource); |
|
|
|
this.propertySourceList.addLast(propertySource); |
|
|
|
this.propertySourceList.add(propertySource); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
@ -161,7 +159,7 @@ public class MutablePropertySources implements PropertySources { |
|
|
|
logger.debug(String.format("Removing [%s] PropertySource", name)); |
|
|
|
logger.debug(String.format("Removing [%s] PropertySource", name)); |
|
|
|
} |
|
|
|
} |
|
|
|
int index = this.propertySourceList.indexOf(PropertySource.named(name)); |
|
|
|
int index = this.propertySourceList.indexOf(PropertySource.named(name)); |
|
|
|
return index == -1 ? null : this.propertySourceList.remove(index); |
|
|
|
return (index != -1 ? this.propertySourceList.remove(index) : null); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
@ -190,7 +188,7 @@ public class MutablePropertySources implements PropertySources { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public String toString() { |
|
|
|
public String toString() { |
|
|
|
String[] names = new String[this.size()]; |
|
|
|
String[] names = new String[this.size()]; |
|
|
|
for (int i=0; i < size(); i++) { |
|
|
|
for (int i = 0; i < size(); i++) { |
|
|
|
names[i] = this.propertySourceList.get(i).getName(); |
|
|
|
names[i] = this.propertySourceList.get(i).getName(); |
|
|
|
} |
|
|
|
} |
|
|
|
return String.format("[%s]", StringUtils.arrayToCommaDelimitedString(names)); |
|
|
|
return String.format("[%s]", StringUtils.arrayToCommaDelimitedString(names)); |
|
|
|
@ -201,17 +199,17 @@ public class MutablePropertySources implements PropertySources { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected void assertLegalRelativeAddition(String relativePropertySourceName, PropertySource<?> propertySource) { |
|
|
|
protected void assertLegalRelativeAddition(String relativePropertySourceName, PropertySource<?> propertySource) { |
|
|
|
String newPropertySourceName = propertySource.getName(); |
|
|
|
String newPropertySourceName = propertySource.getName(); |
|
|
|
Assert.isTrue(!relativePropertySourceName.equals(newPropertySourceName), |
|
|
|
if (relativePropertySourceName.equals(newPropertySourceName)) { |
|
|
|
String.format(ILLEGAL_RELATIVE_ADDITION_MESSAGE, newPropertySourceName)); |
|
|
|
throw new IllegalArgumentException( |
|
|
|
|
|
|
|
String.format("PropertySource named [%s] cannot be added relative to itself", newPropertySourceName)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Remove the given property source if it is present. |
|
|
|
* Remove the given property source if it is present. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected void removeIfPresent(PropertySource<?> propertySource) { |
|
|
|
protected void removeIfPresent(PropertySource<?> propertySource) { |
|
|
|
if (this.propertySourceList.contains(propertySource)) { |
|
|
|
this.propertySourceList.remove(propertySource); |
|
|
|
this.propertySourceList.remove(propertySource); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
@ -230,7 +228,9 @@ public class MutablePropertySources implements PropertySources { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private int assertPresentAndGetIndex(String name) { |
|
|
|
private int assertPresentAndGetIndex(String name) { |
|
|
|
int index = this.propertySourceList.indexOf(PropertySource.named(name)); |
|
|
|
int index = this.propertySourceList.indexOf(PropertySource.named(name)); |
|
|
|
Assert.isTrue(index >= 0, String.format(NON_EXISTENT_PROPERTY_SOURCE_MESSAGE, name)); |
|
|
|
if (index == -1) { |
|
|
|
|
|
|
|
throw new IllegalArgumentException(String.format("PropertySource named [%s] does not exist", name)); |
|
|
|
|
|
|
|
} |
|
|
|
return index; |
|
|
|
return index; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|