Consistently use PropertySource.getName() for comparison

Includes synchronization for mutators on MutablePropertySources.

Closes gh-25369
This commit is contained in:
Juergen Hoeller 2020-07-17 17:45:58 +02:00
parent 5846d9c2ea
commit 01bab89dba
3 changed files with 47 additions and 25 deletions

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -79,14 +79,23 @@ public class MutablePropertySources implements PropertySources {
@Override @Override
public boolean contains(String name) { 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 @Override
@Nullable @Nullable
public PropertySource<?> get(String name) { public PropertySource<?> get(String name) {
int index = this.propertySourceList.indexOf(PropertySource.named(name)); for (PropertySource<?> propertySource : this.propertySourceList) {
return (index != -1 ? this.propertySourceList.get(index) : null); if (propertySource.getName().equals(name)) {
return propertySource;
}
}
return null;
} }
@ -94,16 +103,20 @@ public class MutablePropertySources implements PropertySources {
* Add the given property source object with highest precedence. * Add the given property source object with highest precedence.
*/ */
public void addFirst(PropertySource<?> propertySource) { public void addFirst(PropertySource<?> propertySource) {
removeIfPresent(propertySource); synchronized (this.propertySourceList) {
this.propertySourceList.add(0, propertySource); removeIfPresent(propertySource);
this.propertySourceList.add(0, propertySource);
}
} }
/** /**
* Add the given property source object with lowest precedence. * Add the given property source object with lowest precedence.
*/ */
public void addLast(PropertySource<?> propertySource) { public void addLast(PropertySource<?> propertySource) {
removeIfPresent(propertySource); synchronized (this.propertySourceList) {
this.propertySourceList.add(propertySource); removeIfPresent(propertySource);
this.propertySourceList.add(propertySource);
}
} }
/** /**
@ -112,9 +125,11 @@ public class MutablePropertySources implements PropertySources {
*/ */
public void addBefore(String relativePropertySourceName, PropertySource<?> propertySource) { public void addBefore(String relativePropertySourceName, PropertySource<?> propertySource) {
assertLegalRelativeAddition(relativePropertySourceName, propertySource); assertLegalRelativeAddition(relativePropertySourceName, propertySource);
removeIfPresent(propertySource); synchronized (this.propertySourceList) {
int index = assertPresentAndGetIndex(relativePropertySourceName); removeIfPresent(propertySource);
addAtIndex(index, propertySource); int index = assertPresentAndGetIndex(relativePropertySourceName);
addAtIndex(index, propertySource);
}
} }
/** /**
@ -123,9 +138,11 @@ public class MutablePropertySources implements PropertySources {
*/ */
public void addAfter(String relativePropertySourceName, PropertySource<?> propertySource) { public void addAfter(String relativePropertySourceName, PropertySource<?> propertySource) {
assertLegalRelativeAddition(relativePropertySourceName, propertySource); assertLegalRelativeAddition(relativePropertySourceName, propertySource);
removeIfPresent(propertySource); synchronized (this.propertySourceList) {
int index = assertPresentAndGetIndex(relativePropertySourceName); removeIfPresent(propertySource);
addAtIndex(index + 1, propertySource); int index = assertPresentAndGetIndex(relativePropertySourceName);
addAtIndex(index + 1, propertySource);
}
} }
/** /**
@ -141,8 +158,10 @@ public class MutablePropertySources implements PropertySources {
*/ */
@Nullable @Nullable
public PropertySource<?> remove(String name) { public PropertySource<?> remove(String name) {
int index = this.propertySourceList.indexOf(PropertySource.named(name)); synchronized (this.propertySourceList) {
return (index != -1 ? this.propertySourceList.remove(index) : null); int index = this.propertySourceList.indexOf(PropertySource.named(name));
return (index != -1 ? this.propertySourceList.remove(index) : null);
}
} }
/** /**
@ -153,8 +172,10 @@ public class MutablePropertySources implements PropertySources {
* @see #contains * @see #contains
*/ */
public void replace(String name, PropertySource<?> propertySource) { public void replace(String name, PropertySource<?> propertySource) {
int index = assertPresentAndGetIndex(name); synchronized (this.propertySourceList) {
this.propertySourceList.set(index, propertySource); int index = assertPresentAndGetIndex(name);
this.propertySourceList.set(index, propertySource);
}
} }
/** /**
@ -169,6 +190,7 @@ public class MutablePropertySources implements PropertySources {
return this.propertySourceList.toString(); return this.propertySourceList.toString();
} }
/** /**
* Ensure that the given property source is not being added relative to itself. * Ensure that the given property source is not being added relative to itself.
*/ */

View File

@ -136,7 +136,7 @@ public abstract class PropertySource<T> {
@Override @Override
public boolean equals(@Nullable Object other) { public boolean equals(@Nullable Object other) {
return (this == other || (other instanceof PropertySource && 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 @Override
public int hashCode() { public int hashCode() {
return ObjectUtils.nullSafeHashCode(this.name); return ObjectUtils.nullSafeHashCode(getName());
} }
/** /**
@ -161,10 +161,10 @@ public abstract class PropertySource<T> {
public String toString() { public String toString() {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
return getClass().getSimpleName() + "@" + System.identityHashCode(this) + return getClass().getSimpleName() + "@" + System.identityHashCode(this) +
" {name='" + this.name + "', properties=" + this.source + "}"; " {name='" + getName() + "', properties=" + getSource() + "}";
} }
else { else {
return getClass().getSimpleName() + " {name='" + this.name + "'}"; return getClass().getSimpleName() + " {name='" + getName() + "'}";
} }
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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"); Assert.notNull(sources, "'propertySources' must not be null");
String name = StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME; 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)); sources.replace(name, new ServletContextPropertySource(name, servletContext));
} }
name = StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME; 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)); sources.replace(name, new ServletConfigPropertySource(name, servletConfig));
} }
} }