Skip to content

Commit

Permalink
Consistently use PropertySource.getName() for comparison
Browse files Browse the repository at this point in the history
Includes synchronization for mutators on MutablePropertySources.

Closes spring-projectsgh-25369
  • Loading branch information
jhoeller committed Jul 17, 2020
1 parent 5846d9c commit 01bab89
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 25 deletions.
@@ -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.
Expand Down Expand Up @@ -79,31 +79,44 @@ public Stream<PropertySource<?>> stream() {

@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;
}


/**
* Add the given property source object with highest precedence.
*/
public void addFirst(PropertySource<?> propertySource) {
removeIfPresent(propertySource);
this.propertySourceList.add(0, 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) {
removeIfPresent(propertySource);
this.propertySourceList.add(propertySource);
synchronized (this.propertySourceList) {
removeIfPresent(propertySource);
this.propertySourceList.add(propertySource);
}
}

/**
Expand All @@ -112,9 +125,11 @@ public void addLast(PropertySource<?> propertySource) {
*/
public void addBefore(String relativePropertySourceName, PropertySource<?> propertySource) {
assertLegalRelativeAddition(relativePropertySourceName, propertySource);
removeIfPresent(propertySource);
int index = assertPresentAndGetIndex(relativePropertySourceName);
addAtIndex(index, propertySource);
synchronized (this.propertySourceList) {
removeIfPresent(propertySource);
int index = assertPresentAndGetIndex(relativePropertySourceName);
addAtIndex(index, propertySource);
}
}

/**
Expand All @@ -123,9 +138,11 @@ public void addBefore(String relativePropertySourceName, PropertySource<?> prope
*/
public void addAfter(String relativePropertySourceName, PropertySource<?> propertySource) {
assertLegalRelativeAddition(relativePropertySourceName, propertySource);
removeIfPresent(propertySource);
int index = assertPresentAndGetIndex(relativePropertySourceName);
addAtIndex(index + 1, propertySource);
synchronized (this.propertySourceList) {
removeIfPresent(propertySource);
int index = assertPresentAndGetIndex(relativePropertySourceName);
addAtIndex(index + 1, propertySource);
}
}

/**
Expand All @@ -141,8 +158,10 @@ public int precedenceOf(PropertySource<?> propertySource) {
*/
@Nullable
public PropertySource<?> remove(String name) {
int index = this.propertySourceList.indexOf(PropertySource.named(name));
return (index != -1 ? this.propertySourceList.remove(index) : null);
synchronized (this.propertySourceList) {
int index = this.propertySourceList.indexOf(PropertySource.named(name));
return (index != -1 ? this.propertySourceList.remove(index) : null);
}
}

/**
Expand All @@ -153,8 +172,10 @@ public PropertySource<?> remove(String name) {
* @see #contains
*/
public void replace(String name, PropertySource<?> propertySource) {
int index = assertPresentAndGetIndex(name);
this.propertySourceList.set(index, propertySource);
synchronized (this.propertySourceList) {
int index = assertPresentAndGetIndex(name);
this.propertySourceList.set(index, propertySource);
}
}

/**
Expand All @@ -169,6 +190,7 @@ public String toString() {
return this.propertySourceList.toString();
}


/**
* Ensure that the given property source is not being added relative to itself.
*/
Expand Down
Expand Up @@ -136,7 +136,7 @@ public boolean containsProperty(String name) {
@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())));
}

/**
Expand All @@ -145,7 +145,7 @@ public boolean equals(@Nullable Object other) {
*/
@Override
public int hashCode() {
return ObjectUtils.nullSafeHashCode(this.name);
return ObjectUtils.nullSafeHashCode(getName());
}

/**
Expand All @@ -161,10 +161,10 @@ public int hashCode() {
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() + "'}";
}
}

Expand Down
@@ -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.
Expand Down Expand Up @@ -296,11 +296,11 @@ public static void initServletPropertySources(MutablePropertySources sources,

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));
}
}
Expand Down

0 comments on commit 01bab89

Please sign in to comment.