You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by da...@apache.org on 2018/02/21 15:36:37 UTC
[isis] 09/11: ISIS-1867: caches JAXBContext within JaxbMatchers
(unit test support)
This is an automated email from the ASF dual-hosted git repository.
danhaywood pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git
commit 1eb350d0646f06928f6ff0cb204a96c2c4b51b53
Author: Dan Haywood <da...@haywood-associates.co.uk>
AuthorDate: Wed Feb 21 15:07:18 2018 +0000
ISIS-1867: caches JAXBContext within JaxbMatchers (unit test support)
---
.../core/unittestsupport/jaxb/JaxbMatchers.java | 25 +++++++++++++++-------
1 file changed, 17 insertions(+), 8 deletions(-)
diff --git a/core/unittestsupport/src/main/java/org/apache/isis/core/unittestsupport/jaxb/JaxbMatchers.java b/core/unittestsupport/src/main/java/org/apache/isis/core/unittestsupport/jaxb/JaxbMatchers.java
index 10e67e4..c126f57 100644
--- a/core/unittestsupport/src/main/java/org/apache/isis/core/unittestsupport/jaxb/JaxbMatchers.java
+++ b/core/unittestsupport/src/main/java/org/apache/isis/core/unittestsupport/jaxb/JaxbMatchers.java
@@ -23,6 +23,7 @@ import java.io.StringReader;
import java.io.Writer;
import java.net.URL;
import java.nio.charset.Charset;
+import java.util.Map;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
@@ -30,6 +31,7 @@ import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import com.google.common.base.Objects;
+import com.google.common.collect.MapMaker;
import com.google.common.io.Resources;
import org.hamcrest.Matcher;
@@ -73,8 +75,8 @@ class JaxbUtil2 {
final Class<T> dtoClass) {
Unmarshaller un = null;
try {
- un = getJaxbContext(dtoClass).createUnmarshaller();
- return (T) un.unmarshal(reader);
+ un = jaxbContextFor(dtoClass).createUnmarshaller();
+ return (T)un.unmarshal(reader);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
@@ -100,7 +102,7 @@ class JaxbUtil2 {
Marshaller m = null;
try {
final Class<?> aClass = dto.getClass();
- m = getJaxbContext(aClass).createMarshaller();
+ m = jaxbContextFor(aClass).createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(dto, writer);
} catch (JAXBException e) {
@@ -108,11 +110,18 @@ class JaxbUtil2 {
}
}
- private static JAXBContext getJaxbContext(Class<?> dtoClass) {
- try {
- return JAXBContext.newInstance(dtoClass);
- } catch (JAXBException e) {
- throw new RuntimeException(e);
+ private static Map<Class<?>, JAXBContext> jaxbContextByClass = new MapMaker().concurrencyLevel(10).makeMap();
+
+ public static <T> JAXBContext jaxbContextFor(final Class<T> dtoClass) {
+ JAXBContext jaxbContext = jaxbContextByClass.get(dtoClass);
+ if(jaxbContext == null) {
+ try {
+ jaxbContext = JAXBContext.newInstance(dtoClass);
+ } catch (JAXBException e) {
+ throw new RuntimeException(e);
+ }
+ jaxbContextByClass.put(dtoClass, jaxbContext);
}
+ return jaxbContext;
}
}
--
To stop receiving notification emails like this one, please contact
danhaywood@apache.org.