This is an automated email from the ASF dual-hosted git repository.

danhaywood pushed a commit to branch maint-1.16.2
in repository https://gitbox.apache.org/repos/asf/isis.git

commit 0d92ae7cbbe20077739255f796dae25cf68b11df
Author: Dan Haywood <d...@haywood-associates.co.uk>
AuthorDate: Fri Feb 23 16:58:59 2018 +0000

    ISIS-1867: caches JAXBContext (backported from 2.0.0-M1)
---
 .../isis/applib/services/jaxb/JaxbService.java     | 32 ++++++++++----------
 .../java/org/apache/isis/applib/util/JaxbUtil.java | 24 ++++++++++-----
 .../apache/isis/schema/utils/ChangesDtoUtils.java  |  7 ++---
 .../apache/isis/schema/utils/CommandDtoUtils.java  |  7 ++---
 .../isis/schema/utils/InteractionDtoUtils.java     |  7 ++---
 .../isis/schema/utils/MemberExecutionDtoUtils.java |  7 ++++-
 .../services/grid/GridLoaderServiceDefault.java    | 35 ++++++++++++----------
 .../core/unittestsupport/jaxb/JaxbMatchers.java    | 25 +++++++++++-----
 8 files changed, 82 insertions(+), 62 deletions(-)

diff --git 
a/core/applib/src/main/java/org/apache/isis/applib/services/jaxb/JaxbService.java
 
b/core/applib/src/main/java/org/apache/isis/applib/services/jaxb/JaxbService.java
index a4cebb8..2ff7c01 100644
--- 
a/core/applib/src/main/java/org/apache/isis/applib/services/jaxb/JaxbService.java
+++ 
b/core/applib/src/main/java/org/apache/isis/applib/services/jaxb/JaxbService.java
@@ -31,13 +31,14 @@ import javax.xml.bind.Unmarshaller;
 import com.google.common.base.Function;
 import com.google.common.base.Joiner;
 import com.google.common.collect.Iterables;
+import com.google.common.collect.MapMaker;
 import com.google.common.collect.Maps;
 
 import org.apache.isis.applib.ApplicationException;
-import org.apache.isis.applib.DomainObjectContainer;
 import org.apache.isis.applib.NonRecoverableException;
 import org.apache.isis.applib.annotation.Programmatic;
 import org.apache.isis.applib.services.dto.Dto_downloadXsd;
+import org.apache.isis.applib.util.JaxbUtil;
 
 public interface JaxbService {
 
@@ -103,7 +104,7 @@ public interface JaxbService {
 
         @Override
         public Object fromXml(final JAXBContext jaxbContext, final String xml) 
{
-            return fromXml(jaxbContext, xml, Maps.<String,Object>newHashMap());
+            return fromXml(jaxbContext, xml, Maps.newHashMap());
         }
         @Override
         public Object fromXml(final JAXBContext jaxbContext, final String xml, 
final Map<String, Object> unmarshallerProperties) {
@@ -127,22 +128,25 @@ public interface JaxbService {
 
         @Override
         public <T> T fromXml(final Class<T> domainClass, final String xml) {
-            return fromXml(domainClass, xml, Maps.<String,Object>newHashMap());
+            return fromXml(domainClass, xml, Maps.newHashMap());
         }
         @Override
         public <T> T fromXml(final Class<T> domainClass, final String xml, 
final Map<String, Object> unmarshallerProperties) {
-            try {
-                final JAXBContext context = 
JAXBContext.newInstance(domainClass);
-                return (T) fromXml(context, xml, unmarshallerProperties);
+            final JAXBContext context = jaxbContextFor(domainClass);
+            return (T) fromXml(context, xml, unmarshallerProperties);
+        }
 
-            } catch (final JAXBException ex) {
-                throw new NonRecoverableException("Error unmarshalling XML to 
class '" + domainClass.getName() + "'", ex);
+        private <T> JAXBContext jaxbContextFor(final Class<T> clazz)  {
+            try {
+                return JaxbUtil.jaxbContextFor(clazz);
+            } catch (RuntimeException e) {
+                throw new NonRecoverableException("Error obtaining JAXBContext 
for class '" + clazz + "'", e.getCause());
             }
         }
 
         @Override
         public String toXml(final Object domainObject) {
-            return toXml(domainObject, Maps.<String,Object>newHashMap());
+            return toXml(domainObject, Maps.newHashMap());
         }
 
         @Override
@@ -150,7 +154,7 @@ public interface JaxbService {
 
             final Class<?> domainClass = domainObject.getClass();
             try {
-                final JAXBContext context = 
JAXBContext.newInstance(domainClass);
+                final JAXBContext context = jaxbContextFor(domainClass);
 
                 final Marshaller marshaller = context.createMarshaller();
 
@@ -218,20 +222,16 @@ public interface JaxbService {
 
             try {
                 final Class<?> domainClass = domainObject.getClass();
-                final JAXBContext context = 
JAXBContext.newInstance(domainClass);
+                final JAXBContext context = jaxbContextFor(domainClass);
 
                 final CatalogingSchemaOutputResolver outputResolver = new 
CatalogingSchemaOutputResolver(isisSchemas);
                 context.generateSchema(outputResolver);
 
                 return outputResolver.asMap();
-            } catch (final JAXBException | IOException ex) {
+            } catch (final IOException ex) {
                 throw new ApplicationException(ex);
             }
         }
-
-
-        @javax.inject.Inject
-        DomainObjectContainer container;
     }
 
 
diff --git 
a/core/applib/src/main/java/org/apache/isis/applib/util/JaxbUtil.java 
b/core/applib/src/main/java/org/apache/isis/applib/util/JaxbUtil.java
index b2cd974..e8be77f 100644
--- a/core/applib/src/main/java/org/apache/isis/applib/util/JaxbUtil.java
+++ b/core/applib/src/main/java/org/apache/isis/applib/util/JaxbUtil.java
@@ -23,14 +23,17 @@ 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;
 import javax.xml.bind.Marshaller;
 import javax.xml.bind.Unmarshaller;
 
+import com.google.common.collect.MapMaker;
 import com.google.common.io.Resources;
 
+
 /**
  * Helper methods for converting {@link 
javax.xml.bind.annotation.XmlRootElement}-annotated class to-and-from XML.  
Intended primarily for
  * test use only (the {@link JAXBContext} is not cached).
@@ -48,7 +51,7 @@ public class JaxbUtil {
             final Class<T> dtoClass) {
         Unmarshaller un = null;
         try {
-            un = getJaxbContext(dtoClass).createUnmarshaller();
+            un = jaxbContextFor(dtoClass).createUnmarshaller();
             return (T) un.unmarshal(reader);
         } catch (JAXBException e) {
             throw new RuntimeException(e);
@@ -75,7 +78,7 @@ public class JaxbUtil {
         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) {
@@ -83,11 +86,18 @@ public class JaxbUtil {
         }
     }
 
-    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;
     }
 }
diff --git 
a/core/applib/src/main/java/org/apache/isis/schema/utils/ChangesDtoUtils.java 
b/core/applib/src/main/java/org/apache/isis/schema/utils/ChangesDtoUtils.java
index 25f8627..575ca9d 100644
--- 
a/core/applib/src/main/java/org/apache/isis/schema/utils/ChangesDtoUtils.java
+++ 
b/core/applib/src/main/java/org/apache/isis/schema/utils/ChangesDtoUtils.java
@@ -34,6 +34,7 @@ import javax.xml.bind.Unmarshaller;
 
 import com.google.common.io.Resources;
 
+import org.apache.isis.applib.util.JaxbUtil;
 import org.apache.isis.schema.chg.v1.ChangesDto;
 
 public final class ChangesDtoUtils {
@@ -46,11 +47,7 @@ public final class ChangesDtoUtils {
     static JAXBContext jaxbContext;
     static JAXBContext getJaxbContext() {
         if(jaxbContext == null) {
-            try {
-                jaxbContext = JAXBContext.newInstance(ChangesDto.class);
-            } catch (JAXBException e) {
-                throw new RuntimeException(e);
-            }
+            jaxbContext = JaxbUtil.jaxbContextFor(ChangesDto.class);
         }
         return jaxbContext;
     }
diff --git 
a/core/applib/src/main/java/org/apache/isis/schema/utils/CommandDtoUtils.java 
b/core/applib/src/main/java/org/apache/isis/schema/utils/CommandDtoUtils.java
index f61260c..dddebc6 100644
--- 
a/core/applib/src/main/java/org/apache/isis/schema/utils/CommandDtoUtils.java
+++ 
b/core/applib/src/main/java/org/apache/isis/schema/utils/CommandDtoUtils.java
@@ -33,6 +33,7 @@ import javax.xml.bind.Unmarshaller;
 
 import com.google.common.io.Resources;
 
+import org.apache.isis.applib.util.JaxbUtil;
 import org.apache.isis.schema.cmd.v1.ActionDto;
 import org.apache.isis.schema.cmd.v1.CommandDto;
 import org.apache.isis.schema.cmd.v1.MapDto;
@@ -50,11 +51,7 @@ public final class CommandDtoUtils {
     static JAXBContext jaxbContext;
     static JAXBContext getJaxbContext() {
         if(jaxbContext == null) {
-            try {
-                jaxbContext = JAXBContext.newInstance(CommandDto.class);
-            } catch (JAXBException e) {
-                throw new RuntimeException(e);
-            }
+            jaxbContext = JaxbUtil.jaxbContextFor(CommandDto.class);
         }
         return jaxbContext;
     }
diff --git 
a/core/applib/src/main/java/org/apache/isis/schema/utils/InteractionDtoUtils.java
 
b/core/applib/src/main/java/org/apache/isis/schema/utils/InteractionDtoUtils.java
index ccf48dd..b329af0 100644
--- 
a/core/applib/src/main/java/org/apache/isis/schema/utils/InteractionDtoUtils.java
+++ 
b/core/applib/src/main/java/org/apache/isis/schema/utils/InteractionDtoUtils.java
@@ -41,6 +41,7 @@ import com.google.common.io.Resources;
 import org.apache.isis.applib.services.bookmark.Bookmark;
 import org.apache.isis.applib.services.bookmark.BookmarkService;
 import org.apache.isis.applib.services.iactn.Interaction;
+import org.apache.isis.applib.util.JaxbUtil;
 import org.apache.isis.schema.cmd.v1.ParamDto;
 import org.apache.isis.schema.cmd.v1.ParamsDto;
 import org.apache.isis.schema.common.v1.InteractionType;
@@ -65,11 +66,7 @@ public final class InteractionDtoUtils {
     static JAXBContext jaxbContext;
     static JAXBContext getJaxbContext() {
         if(jaxbContext == null) {
-            try {
-                jaxbContext = JAXBContext.newInstance(InteractionDto.class);
-            } catch (JAXBException e) {
-                throw new RuntimeException(e);
-            }
+            jaxbContext = JaxbUtil.jaxbContextFor(InteractionDto.class);
         }
         return jaxbContext;
     }
diff --git 
a/core/applib/src/main/java/org/apache/isis/schema/utils/MemberExecutionDtoUtils.java
 
b/core/applib/src/main/java/org/apache/isis/schema/utils/MemberExecutionDtoUtils.java
index 121ea92..9563507 100644
--- 
a/core/applib/src/main/java/org/apache/isis/schema/utils/MemberExecutionDtoUtils.java
+++ 
b/core/applib/src/main/java/org/apache/isis/schema/utils/MemberExecutionDtoUtils.java
@@ -29,6 +29,7 @@ import javax.xml.bind.Unmarshaller;
 import javax.xml.namespace.QName;
 import javax.xml.transform.stream.StreamSource;
 
+import org.apache.isis.applib.util.JaxbUtil;
 import org.apache.isis.schema.common.v1.DifferenceDto;
 import org.apache.isis.schema.common.v1.PeriodDto;
 import org.apache.isis.schema.ixn.v1.MemberExecutionDto;
@@ -44,7 +45,7 @@ public final class MemberExecutionDtoUtils {
 
     private static <T> T clone(final T dto, final Class<T> dtoClass) {
         try {
-            JAXBContext jaxbContext = JAXBContext.newInstance(dtoClass);
+            JAXBContext jaxbContext = jaxbContextFor(dtoClass);
 
             final Marshaller marshaller = jaxbContext.createMarshaller();
 
@@ -67,6 +68,10 @@ public final class MemberExecutionDtoUtils {
         }
     }
 
+    private static <T> JAXBContext jaxbContextFor(final Class<T> dtoClass)  {
+        return JaxbUtil.jaxbContextFor(dtoClass);
+    }
+
     public static MetricsDto metricsFor(final MemberExecutionDto executionDto) 
{
         MetricsDto metrics = executionDto.getMetrics();
         if(metrics == null) {
diff --git 
a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/services/grid/GridLoaderServiceDefault.java
 
b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/services/grid/GridLoaderServiceDefault.java
index 192a9fa..bb9d9c3 100644
--- 
a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/services/grid/GridLoaderServiceDefault.java
+++ 
b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/services/grid/GridLoaderServiceDefault.java
@@ -25,6 +25,7 @@ import java.util.Objects;
 
 import javax.annotation.PostConstruct;
 import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
 
 import com.google.common.base.Function;
 import com.google.common.collect.FluentIterable;
@@ -59,20 +60,22 @@ public class GridLoaderServiceDefault implements 
GridLoaderService {
     // cache (used only in prototyping mode)
     private final Map<String, Grid> gridByXml = Maps.newHashMap();
 
-    private List<Class<? extends Grid>> pageImplementations;
-
-
+    private JAXBContext jaxbContext;
 
     @PostConstruct
     public void init(){
-        pageImplementations = FluentIterable.from(gridSystemServices)
-                .transform(new Function<GridSystemService, Class<? extends 
Grid>>() {
-                    @Override
-                    public Class<? extends Grid> apply(final GridSystemService 
gridSystemService) {
-                        return gridSystemService.gridImplementation();
-                    }
-                })
-                .toList();
+        final List<Class<? extends Grid>> pageImplementations =
+                FluentIterable.from(gridSystemServices)
+                    .transform(
+                            (Function<GridSystemService, Class<? extends 
Grid>>) gss -> gss.gridImplementation())
+                    .toList();
+
+        final Class[] clazz = 
pageImplementations.toArray(_Constants.emptyClasses);
+        try {
+            jaxbContext = JAXBContext.newInstance(clazz);
+        } catch (JAXBException e) {
+            // leave as null
+        }
     }
 
     @Override
@@ -126,10 +129,12 @@ public class GridLoaderServiceDefault implements 
GridLoaderService {
         }
 
         try {
-            // all known implementations of Page
-            final JAXBContext context = 
JAXBContext.newInstance(pageImplementations.toArray(new Class[0]));
-
-            final Grid grid = (Grid) jaxbService.fromXml(context, xml);
+            if(jaxbContext == null) {
+                // shouldn't occur, indicates that initialization failed to 
locate any GridSystemService implementations.
+                return null;
+            }
+            
+            final Grid grid = (Grid) jaxbService.fromXml(jaxbContext, xml);
             grid.setDomainClass(domainClass);
             if(supportsReloading()) {
                 gridByXml.put(xml, grid);
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
danhayw...@apache.org.

Reply via email to