Skip to content

Commit 1910764

Browse files
committed
Revise concurrent JAXBContext creation towards computeIfAbsent
Closes spring-projectsgh-23879
1 parent 42e7ade commit 1910764

File tree

4 files changed

+22
-24
lines changed

4 files changed

+22
-24
lines changed

spring-web/src/main/java/org/springframework/http/codec/xml/Jaxb2XmlDecoder.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ private Object unmarshal(List<XMLEvent> events, Class<?> outputClass) {
221221
}
222222
}
223223

224-
private Unmarshaller initUnmarshaller(Class<?> outputClass) throws JAXBException {
224+
private Unmarshaller initUnmarshaller(Class<?> outputClass) throws CodecException, JAXBException {
225225
Unmarshaller unmarshaller = this.jaxbContexts.createUnmarshaller(outputClass);
226226
return this.unmarshallerProcessor.apply(unmarshaller);
227227
}

spring-web/src/main/java/org/springframework/http/codec/xml/Jaxb2XmlEncoder.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public DataBuffer encodeValue(Object value, DataBufferFactory bufferFactory,
140140
}
141141
}
142142

143-
private Marshaller initMarshaller(Class<?> clazz) throws JAXBException {
143+
private Marshaller initMarshaller(Class<?> clazz) throws CodecException, JAXBException {
144144
Marshaller marshaller = this.jaxbContexts.createMarshaller(clazz);
145145
marshaller.setProperty(Marshaller.JAXB_ENCODING, StandardCharsets.UTF_8.name());
146146
marshaller = this.marshallerProcessor.apply(marshaller);

spring-web/src/main/java/org/springframework/http/codec/xml/JaxbContextContainer.java

+15-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -24,37 +24,40 @@
2424
import javax.xml.bind.Marshaller;
2525
import javax.xml.bind.Unmarshaller;
2626

27-
import org.springframework.util.Assert;
27+
import org.springframework.core.codec.CodecException;
2828

2929
/**
3030
* Holder for {@link JAXBContext} instances.
3131
*
3232
* @author Arjen Poutsma
33+
* @author Juergen Hoeller
3334
* @since 5.0
3435
*/
3536
final class JaxbContextContainer {
3637

3738
private final ConcurrentMap<Class<?>, JAXBContext> jaxbContexts = new ConcurrentHashMap<>(64);
3839

3940

40-
public Marshaller createMarshaller(Class<?> clazz) throws JAXBException {
41+
public Marshaller createMarshaller(Class<?> clazz) throws CodecException, JAXBException {
4142
JAXBContext jaxbContext = getJaxbContext(clazz);
4243
return jaxbContext.createMarshaller();
4344
}
4445

45-
public Unmarshaller createUnmarshaller(Class<?> clazz) throws JAXBException {
46+
public Unmarshaller createUnmarshaller(Class<?> clazz) throws CodecException, JAXBException {
4647
JAXBContext jaxbContext = getJaxbContext(clazz);
4748
return jaxbContext.createUnmarshaller();
4849
}
4950

50-
private JAXBContext getJaxbContext(Class<?> clazz) throws JAXBException {
51-
Assert.notNull(clazz, "Class must not be null");
52-
JAXBContext jaxbContext = this.jaxbContexts.get(clazz);
53-
if (jaxbContext == null) {
54-
jaxbContext = JAXBContext.newInstance(clazz);
55-
this.jaxbContexts.putIfAbsent(clazz, jaxbContext);
56-
}
57-
return jaxbContext;
51+
private JAXBContext getJaxbContext(Class<?> clazz) throws CodecException {
52+
return this.jaxbContexts.computeIfAbsent(clazz, key -> {
53+
try {
54+
return JAXBContext.newInstance(clazz);
55+
}
56+
catch (JAXBException ex) {
57+
throw new CodecException(
58+
"Could not create JAXBContext for class [" + clazz + "]: " + ex.getMessage(), ex);
59+
}
60+
});
5861
}
5962

6063
}

spring-web/src/main/java/org/springframework/http/converter/xml/AbstractJaxb2HttpMessageConverter.java

+5-10
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-2019 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.
@@ -25,7 +25,6 @@
2525
import javax.xml.bind.Unmarshaller;
2626

2727
import org.springframework.http.converter.HttpMessageConversionException;
28-
import org.springframework.util.Assert;
2928

3029
/**
3130
* Abstract base class for {@link org.springframework.http.converter.HttpMessageConverter HttpMessageConverters}
@@ -106,19 +105,15 @@ protected void customizeUnmarshaller(Unmarshaller unmarshaller) {
106105
* @throws HttpMessageConversionException in case of JAXB errors
107106
*/
108107
protected final JAXBContext getJaxbContext(Class<?> clazz) {
109-
Assert.notNull(clazz, "Class must not be null");
110-
JAXBContext jaxbContext = this.jaxbContexts.get(clazz);
111-
if (jaxbContext == null) {
108+
return this.jaxbContexts.computeIfAbsent(clazz, key -> {
112109
try {
113-
jaxbContext = JAXBContext.newInstance(clazz);
114-
this.jaxbContexts.putIfAbsent(clazz, jaxbContext);
110+
return JAXBContext.newInstance(clazz);
115111
}
116112
catch (JAXBException ex) {
117113
throw new HttpMessageConversionException(
118-
"Could not instantiate JAXBContext for class [" + clazz + "]: " + ex.getMessage(), ex);
114+
"Could not create JAXBContext for class [" + clazz + "]: " + ex.getMessage(), ex);
119115
}
120-
}
121-
return jaxbContext;
116+
});
122117
}
123118

124119
}

0 commit comments

Comments
 (0)