Skip to content

Commit 01bab89

Browse files
committed
Consistently use PropertySource.getName() for comparison
Includes synchronization for mutators on MutablePropertySources. Closes spring-projectsgh-25369
1 parent 5846d9c commit 01bab89

File tree

3 files changed

+47
-25
lines changed

3 files changed

+47
-25
lines changed

spring-core/src/main/java/org/springframework/core/env/MutablePropertySources.java

+40-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -79,31 +79,44 @@ public Stream<PropertySource<?>> stream() {
7979

8080
@Override
8181
public boolean contains(String name) {
82-
return this.propertySourceList.contains(PropertySource.named(name));
82+
for (PropertySource<?> propertySource : this.propertySourceList) {
83+
if (propertySource.getName().equals(name)) {
84+
return true;
85+
}
86+
}
87+
return false;
8388
}
8489

8590
@Override
8691
@Nullable
8792
public PropertySource<?> get(String name) {
88-
int index = this.propertySourceList.indexOf(PropertySource.named(name));
89-
return (index != -1 ? this.propertySourceList.get(index) : null);
93+
for (PropertySource<?> propertySource : this.propertySourceList) {
94+
if (propertySource.getName().equals(name)) {
95+
return propertySource;
96+
}
97+
}
98+
return null;
9099
}
91100

92101

93102
/**
94103
* Add the given property source object with highest precedence.
95104
*/
96105
public void addFirst(PropertySource<?> propertySource) {
97-
removeIfPresent(propertySource);
98-
this.propertySourceList.add(0, propertySource);
106+
synchronized (this.propertySourceList) {
107+
removeIfPresent(propertySource);
108+
this.propertySourceList.add(0, propertySource);
109+
}
99110
}
100111

101112
/**
102113
* Add the given property source object with lowest precedence.
103114
*/
104115
public void addLast(PropertySource<?> propertySource) {
105-
removeIfPresent(propertySource);
106-
this.propertySourceList.add(propertySource);
116+
synchronized (this.propertySourceList) {
117+
removeIfPresent(propertySource);
118+
this.propertySourceList.add(propertySource);
119+
}
107120
}
108121

109122
/**
@@ -112,9 +125,11 @@ public void addLast(PropertySource<?> propertySource) {
112125
*/
113126
public void addBefore(String relativePropertySourceName, PropertySource<?> propertySource) {
114127
assertLegalRelativeAddition(relativePropertySourceName, propertySource);
115-
removeIfPresent(propertySource);
116-
int index = assertPresentAndGetIndex(relativePropertySourceName);
117-
addAtIndex(index, propertySource);
128+
synchronized (this.propertySourceList) {
129+
removeIfPresent(propertySource);
130+
int index = assertPresentAndGetIndex(relativePropertySourceName);
131+
addAtIndex(index, propertySource);
132+
}
118133
}
119134

120135
/**
@@ -123,9 +138,11 @@ public void addBefore(String relativePropertySourceName, PropertySource<?> prope
123138
*/
124139
public void addAfter(String relativePropertySourceName, PropertySource<?> propertySource) {
125140
assertLegalRelativeAddition(relativePropertySourceName, propertySource);
126-
removeIfPresent(propertySource);
127-
int index = assertPresentAndGetIndex(relativePropertySourceName);
128-
addAtIndex(index + 1, propertySource);
141+
synchronized (this.propertySourceList) {
142+
removeIfPresent(propertySource);
143+
int index = assertPresentAndGetIndex(relativePropertySourceName);
144+
addAtIndex(index + 1, propertySource);
145+
}
129146
}
130147

131148
/**
@@ -141,8 +158,10 @@ public int precedenceOf(PropertySource<?> propertySource) {
141158
*/
142159
@Nullable
143160
public PropertySource<?> remove(String name) {
144-
int index = this.propertySourceList.indexOf(PropertySource.named(name));
145-
return (index != -1 ? this.propertySourceList.remove(index) : null);
161+
synchronized (this.propertySourceList) {
162+
int index = this.propertySourceList.indexOf(PropertySource.named(name));
163+
return (index != -1 ? this.propertySourceList.remove(index) : null);
164+
}
146165
}
147166

148167
/**
@@ -153,8 +172,10 @@ public PropertySource<?> remove(String name) {
153172
* @see #contains
154173
*/
155174
public void replace(String name, PropertySource<?> propertySource) {
156-
int index = assertPresentAndGetIndex(name);
157-
this.propertySourceList.set(index, propertySource);
175+
synchronized (this.propertySourceList) {
176+
int index = assertPresentAndGetIndex(name);
177+
this.propertySourceList.set(index, propertySource);
178+
}
158179
}
159180

160181
/**
@@ -169,6 +190,7 @@ public String toString() {
169190
return this.propertySourceList.toString();
170191
}
171192

193+
172194
/**
173195
* Ensure that the given property source is not being added relative to itself.
174196
*/

spring-core/src/main/java/org/springframework/core/env/PropertySource.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public boolean containsProperty(String name) {
136136
@Override
137137
public boolean equals(@Nullable Object other) {
138138
return (this == other || (other instanceof PropertySource &&
139-
ObjectUtils.nullSafeEquals(this.name, ((PropertySource<?>) other).name)));
139+
ObjectUtils.nullSafeEquals(getName(), ((PropertySource<?>) other).getName())));
140140
}
141141

142142
/**
@@ -145,7 +145,7 @@ public boolean equals(@Nullable Object other) {
145145
*/
146146
@Override
147147
public int hashCode() {
148-
return ObjectUtils.nullSafeHashCode(this.name);
148+
return ObjectUtils.nullSafeHashCode(getName());
149149
}
150150

151151
/**
@@ -161,10 +161,10 @@ public int hashCode() {
161161
public String toString() {
162162
if (logger.isDebugEnabled()) {
163163
return getClass().getSimpleName() + "@" + System.identityHashCode(this) +
164-
" {name='" + this.name + "', properties=" + this.source + "}";
164+
" {name='" + getName() + "', properties=" + getSource() + "}";
165165
}
166166
else {
167-
return getClass().getSimpleName() + " {name='" + this.name + "'}";
167+
return getClass().getSimpleName() + " {name='" + getName() + "'}";
168168
}
169169
}
170170

spring-web/src/main/java/org/springframework/web/context/support/WebApplicationContextUtils.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -296,11 +296,11 @@ public static void initServletPropertySources(MutablePropertySources sources,
296296

297297
Assert.notNull(sources, "'propertySources' must not be null");
298298
String name = StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME;
299-
if (servletContext != null && sources.contains(name) && sources.get(name) instanceof StubPropertySource) {
299+
if (servletContext != null && sources.get(name) instanceof StubPropertySource) {
300300
sources.replace(name, new ServletContextPropertySource(name, servletContext));
301301
}
302302
name = StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME;
303-
if (servletConfig != null && sources.contains(name) && sources.get(name) instanceof StubPropertySource) {
303+
if (servletConfig != null && sources.get(name) instanceof StubPropertySource) {
304304
sources.replace(name, new ServletConfigPropertySource(name, servletConfig));
305305
}
306306
}

0 commit comments

Comments
 (0)