Remove initialization concerns fom HttpMessageConverters

pull/172/head
Dave Syer 11 years ago
parent 513c6a1de2
commit bdcb94a139

@ -23,10 +23,8 @@ import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.xml.AbstractXmlHttpMessageConverter; import org.springframework.http.converter.xml.AbstractXmlHttpMessageConverter;
import org.springframework.util.Assert;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
/** /**
@ -47,15 +45,11 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupp
* @see #HttpMessageConverters(HttpMessageConverter...) * @see #HttpMessageConverters(HttpMessageConverter...)
* @see #HttpMessageConverters(Collection) * @see #HttpMessageConverters(Collection)
* @see #getConverters() * @see #getConverters()
* @see #setConverters(List)
*/ */
public class HttpMessageConverters implements Iterable<HttpMessageConverter<?>>, public class HttpMessageConverters implements Iterable<HttpMessageConverter<?>> {
InitializingBean {
private List<HttpMessageConverter<?>> converters; private List<HttpMessageConverter<?>> converters;
private boolean initialized;
/** /**
* Create a new {@link HttpMessageConverters} instance with the specified additional * Create a new {@link HttpMessageConverters} instance with the specified additional
* converters. * converters.
@ -77,18 +71,19 @@ public class HttpMessageConverters implements Iterable<HttpMessageConverter<?>>,
* converter manipulation. * converter manipulation.
*/ */
public HttpMessageConverters(Collection<HttpMessageConverter<?>> additionalConverters) { public HttpMessageConverters(Collection<HttpMessageConverter<?>> additionalConverters) {
this.converters = new ArrayList<HttpMessageConverter<?>>(); List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
List<HttpMessageConverter<?>> defaultConverters = getDefaultConverters(); List<HttpMessageConverter<?>> defaultConverters = getDefaultConverters();
for (HttpMessageConverter<?> converter : additionalConverters) { for (HttpMessageConverter<?> converter : additionalConverters) {
int defaultConverterIndex = indexOfItemClass(defaultConverters, converter); int defaultConverterIndex = indexOfItemClass(defaultConverters, converter);
if (defaultConverterIndex == -1) { if (defaultConverterIndex == -1) {
this.converters.add(converter); converters.add(converter);
} }
else { else {
defaultConverters.set(defaultConverterIndex, converter); defaultConverters.set(defaultConverterIndex, converter);
} }
} }
this.converters.addAll(defaultConverters); converters.addAll(defaultConverters);
this.converters = Collections.unmodifiableList(converters);
} }
private List<HttpMessageConverter<?>> getDefaultConverters() { private List<HttpMessageConverter<?>> getDefaultConverters() {
@ -139,19 +134,4 @@ public class HttpMessageConverters implements Iterable<HttpMessageConverter<?>>,
return this.converters; return this.converters;
} }
/**
* Set the converters to use, replacing any existing values. This method can only be
* called before the bean has been initialized.
* @param converters the converters to set
*/
public void setConverters(List<HttpMessageConverter<?>> converters) {
Assert.state(!this.initialized, "Unable to set converters once initialized");
this.converters = converters;
}
@Override
public void afterPropertiesSet() throws Exception {
this.initialized = true;
this.converters = Collections.unmodifiableList(this.converters);
}
} }

@ -64,37 +64,6 @@ public class HttpMessageConvertersTests {
Jaxb2RootElementHttpMessageConverter.class))); Jaxb2RootElementHttpMessageConverter.class)));
} }
@Test
public void canModifyBeforeInitialize() throws Exception {
HttpMessageConverters converters = new HttpMessageConverters();
HttpMessageConverter<?> converter = mock(HttpMessageConverter.class);
converters.getConverters().add(converter);
assertThat(converters.getConverters().contains(converter), equalTo(true));
}
@Test
public void cannotModifyAfterInitialize() throws Exception {
HttpMessageConverters converters = new HttpMessageConverters();
converters.afterPropertiesSet();
this.thrown.expect(UnsupportedOperationException.class);
converters.getConverters().add(mock(HttpMessageConverter.class));
}
@Test
public void canSetBeforeInitialize() throws Exception {
HttpMessageConverters converters = new HttpMessageConverters();
converters.setConverters(new ArrayList<HttpMessageConverter<?>>());
assertThat(converters.getConverters().size(), equalTo(0));
}
@Test
public void cannotSetAfterInitailzie() throws Exception {
HttpMessageConverters converters = new HttpMessageConverters();
converters.afterPropertiesSet();
this.thrown.expect(IllegalStateException.class);
converters.setConverters(new ArrayList<HttpMessageConverter<?>>());
}
@Test @Test
public void overrideExistingConverter() { public void overrideExistingConverter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();

Loading…
Cancel
Save