MutablePropertySources uses an internal CopyOnWriteArrayList for defensiveness against concurrent modifications

Issue: SPR-12428
This commit is contained in:
Juergen Hoeller 2014-11-22 16:12:28 +01:00
parent b4167be52d
commit 1ef06cc743
2 changed files with 33 additions and 36 deletions

View File

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

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2014 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.
@ -20,15 +20,12 @@ import org.junit.Test;
import org.springframework.mock.env.MockPropertySource; import org.springframework.mock.env.MockPropertySource;
import static java.lang.String.format;
import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.springframework.core.env.MutablePropertySources.*;
/** /**
* Unit tests for {@link MutablePropertySources}
*
* @author Chris Beams * @author Chris Beams
* @author Juergen Hoeller
*/ */
public class MutablePropertySourcesTests { public class MutablePropertySourcesTests {
@ -106,9 +103,9 @@ public class MutablePropertySourcesTests {
try { try {
sources.addAfter(bogusPS, new MockPropertySource("h")); sources.addAfter(bogusPS, new MockPropertySource("h"));
fail("expected non-existent PropertySource exception"); fail("expected non-existent PropertySource exception");
} catch (IllegalArgumentException ex) { }
assertThat(ex.getMessage(), catch (IllegalArgumentException ex) {
equalTo(format(NON_EXISTENT_PROPERTY_SOURCE_MESSAGE, bogusPS))); assertTrue(ex.getMessage().contains("does not exist"));
} }
sources.addFirst(new MockPropertySource("a")); sources.addFirst(new MockPropertySource("a"));
@ -128,25 +125,25 @@ public class MutablePropertySourcesTests {
try { try {
sources.replace(bogusPS, new MockPropertySource("bogus-replaced")); sources.replace(bogusPS, new MockPropertySource("bogus-replaced"));
fail("expected non-existent PropertySource exception"); fail("expected non-existent PropertySource exception");
} catch (IllegalArgumentException ex) { }
assertThat(ex.getMessage(), catch (IllegalArgumentException ex) {
equalTo(format(NON_EXISTENT_PROPERTY_SOURCE_MESSAGE, bogusPS))); assertTrue(ex.getMessage().contains("does not exist"));
} }
try { try {
sources.addBefore("b", new MockPropertySource("b")); sources.addBefore("b", new MockPropertySource("b"));
fail("expected exception"); fail("expected exception");
} catch (IllegalArgumentException ex) { }
assertThat(ex.getMessage(), catch (IllegalArgumentException ex) {
equalTo(format(ILLEGAL_RELATIVE_ADDITION_MESSAGE, "b"))); assertTrue(ex.getMessage().contains("cannot be added relative to itself"));
} }
try { try {
sources.addAfter("b", new MockPropertySource("b")); sources.addAfter("b", new MockPropertySource("b"));
fail("expected exception"); fail("expected exception");
} catch (IllegalArgumentException ex) { }
assertThat(ex.getMessage(), catch (IllegalArgumentException ex) {
equalTo(format(ILLEGAL_RELATIVE_ADDITION_MESSAGE, "b"))); assertTrue(ex.getMessage().contains("cannot be added relative to itself"));
} }
} }