Consistently use PropertySource.getName() for comparison
Includes synchronization for mutators on MutablePropertySources. Closes gh-25369
This commit is contained in:
parent
5846d9c2ea
commit
01bab89dba
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -79,14 +79,23 @@ public class MutablePropertySources implements PropertySources {
|
|||
|
||||
@Override
|
||||
public boolean contains(String name) {
|
||||
return this.propertySourceList.contains(PropertySource.named(name));
|
||||
for (PropertySource<?> propertySource : this.propertySourceList) {
|
||||
if (propertySource.getName().equals(name)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public PropertySource<?> get(String name) {
|
||||
int index = this.propertySourceList.indexOf(PropertySource.named(name));
|
||||
return (index != -1 ? this.propertySourceList.get(index) : null);
|
||||
for (PropertySource<?> propertySource : this.propertySourceList) {
|
||||
if (propertySource.getName().equals(name)) {
|
||||
return propertySource;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
@ -94,17 +103,21 @@ public class MutablePropertySources implements PropertySources {
|
|||
* Add the given property source object with highest precedence.
|
||||
*/
|
||||
public void addFirst(PropertySource<?> propertySource) {
|
||||
synchronized (this.propertySourceList) {
|
||||
removeIfPresent(propertySource);
|
||||
this.propertySourceList.add(0, propertySource);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the given property source object with lowest precedence.
|
||||
*/
|
||||
public void addLast(PropertySource<?> propertySource) {
|
||||
synchronized (this.propertySourceList) {
|
||||
removeIfPresent(propertySource);
|
||||
this.propertySourceList.add(propertySource);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the given property source object with precedence immediately higher
|
||||
|
@ -112,10 +125,12 @@ public class MutablePropertySources implements PropertySources {
|
|||
*/
|
||||
public void addBefore(String relativePropertySourceName, PropertySource<?> propertySource) {
|
||||
assertLegalRelativeAddition(relativePropertySourceName, propertySource);
|
||||
synchronized (this.propertySourceList) {
|
||||
removeIfPresent(propertySource);
|
||||
int index = assertPresentAndGetIndex(relativePropertySourceName);
|
||||
addAtIndex(index, propertySource);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the given property source object with precedence immediately lower
|
||||
|
@ -123,10 +138,12 @@ public class MutablePropertySources implements PropertySources {
|
|||
*/
|
||||
public void addAfter(String relativePropertySourceName, PropertySource<?> propertySource) {
|
||||
assertLegalRelativeAddition(relativePropertySourceName, propertySource);
|
||||
synchronized (this.propertySourceList) {
|
||||
removeIfPresent(propertySource);
|
||||
int index = assertPresentAndGetIndex(relativePropertySourceName);
|
||||
addAtIndex(index + 1, propertySource);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the precedence of the given property source, {@code -1} if not found.
|
||||
|
@ -141,9 +158,11 @@ public class MutablePropertySources implements PropertySources {
|
|||
*/
|
||||
@Nullable
|
||||
public PropertySource<?> remove(String name) {
|
||||
synchronized (this.propertySourceList) {
|
||||
int index = this.propertySourceList.indexOf(PropertySource.named(name));
|
||||
return (index != -1 ? this.propertySourceList.remove(index) : null);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace the property source with the given name with the given property source object.
|
||||
|
@ -153,9 +172,11 @@ public class MutablePropertySources implements PropertySources {
|
|||
* @see #contains
|
||||
*/
|
||||
public void replace(String name, PropertySource<?> propertySource) {
|
||||
synchronized (this.propertySourceList) {
|
||||
int index = assertPresentAndGetIndex(name);
|
||||
this.propertySourceList.set(index, propertySource);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of {@link PropertySource} objects contained.
|
||||
|
@ -169,6 +190,7 @@ public class MutablePropertySources implements PropertySources {
|
|||
return this.propertySourceList.toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Ensure that the given property source is not being added relative to itself.
|
||||
*/
|
||||
|
|
|
@ -136,7 +136,7 @@ public abstract class PropertySource<T> {
|
|||
@Override
|
||||
public boolean equals(@Nullable Object other) {
|
||||
return (this == other || (other instanceof PropertySource &&
|
||||
ObjectUtils.nullSafeEquals(this.name, ((PropertySource<?>) other).name)));
|
||||
ObjectUtils.nullSafeEquals(getName(), ((PropertySource<?>) other).getName())));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -145,7 +145,7 @@ public abstract class PropertySource<T> {
|
|||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return ObjectUtils.nullSafeHashCode(this.name);
|
||||
return ObjectUtils.nullSafeHashCode(getName());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -161,10 +161,10 @@ public abstract class PropertySource<T> {
|
|||
public String toString() {
|
||||
if (logger.isDebugEnabled()) {
|
||||
return getClass().getSimpleName() + "@" + System.identityHashCode(this) +
|
||||
" {name='" + this.name + "', properties=" + this.source + "}";
|
||||
" {name='" + getName() + "', properties=" + getSource() + "}";
|
||||
}
|
||||
else {
|
||||
return getClass().getSimpleName() + " {name='" + this.name + "'}";
|
||||
return getClass().getSimpleName() + " {name='" + getName() + "'}";
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -296,11 +296,11 @@ public abstract class WebApplicationContextUtils {
|
|||
|
||||
Assert.notNull(sources, "'propertySources' must not be null");
|
||||
String name = StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME;
|
||||
if (servletContext != null && sources.contains(name) && sources.get(name) instanceof StubPropertySource) {
|
||||
if (servletContext != null && sources.get(name) instanceof StubPropertySource) {
|
||||
sources.replace(name, new ServletContextPropertySource(name, servletContext));
|
||||
}
|
||||
name = StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME;
|
||||
if (servletConfig != null && sources.contains(name) && sources.get(name) instanceof StubPropertySource) {
|
||||
if (servletConfig != null && sources.get(name) instanceof StubPropertySource) {
|
||||
sources.replace(name, new ServletConfigPropertySource(name, servletConfig));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue