Class DefaultModelMapperSerializer
java.lang.Object
com.broadleafcommerce.data.tracking.core.mapping.cache.DefaultModelMapperSerializer
- All Implemented Interfaces:
ModelMapperSerializer
,org.springframework.beans.factory.Aware
,org.springframework.beans.factory.BeanClassLoaderAware
public class DefaultModelMapperSerializer
extends Object
implements ModelMapperSerializer, org.springframework.beans.factory.BeanClassLoaderAware
Default version of
ModelMapperSerializer
- Author:
- Jeff Fischer
-
Constructor Summary
ConstructorDescriptionDefaultModelMapperSerializer
(DomainMapperMember domainMapperMember, DomainMapperManager domainMapperManager, ResourceProvider resourceProvider, com.broadleafcommerce.common.extension.TypeFactory typeFactory, KryoCustomizer kryoCustomizer, com.broadleafcommerce.common.extension.data.DataRouteReference reference, ModelMapperCacheProperties properties) -
Method Summary
Modifier and TypeMethodDescriptionprotected void
appendAnyMissingClasses
(String dumpDir, DynamicClassContext context) Review the dump dir and add any pertinent dynamic classes that may have been missed during class resolution at the time of serialization.protected void
compileMappers
(Class<?>[] entities) UsingModelMapperDomainMapperMember
, exercise the system to createModelMapper
instances for the requested entities.protected void
curateMapper
(org.modelmapper.ModelMapper mapper) Process the ModelMapper before populating in the cacheorg.modelmapper.ModelMapper
deserialize
(com.esotericsoftware.kryo.kryo5.Kryo kryo, byte[] serialized) Given a byte array representation of aModelMapper
instance, deserialize into a concrete instance usingKryo
.void
generateCacheResources
(Class<?>[] entities) A rich lifecycle method, calls will generateModelMapper
instances for the specified entities, serialize those ModelMappers, and persist those mappers to disk in a file entitledmappers.zip
.void
generateCacheResources
(Class<?>[] entities, boolean hashShapes) A rich lifecycle method, calls will generateModelMapper
instances for the specified entities, serialize those ModelMappers, and persist those mappers to disk in a file entitledmappers.zip
.protected com.broadleafcommerce.data.tracking.core.mapping.cache.DefaultModelMapperSerializer.ClassFile
getDumpFile
(String dumpDir, String[] files, String packageName, String simpleName) If the system detects a dynamic class required during serialization, retrieve it from the dump location used by ByteBuddy.Create a hash uniquely identifying the class structure, including fields and types in the object graph.protected void
Inject all classes in classes.zip into the classloadervoid
loadCacheResources
(Class<?>[] entities) A rich lifecycle method, calls will readModelMapper
serialized files frommappers.zip
for the specified entities, deserialize those ModelMappers, and inject those mappers into cache upon application startup.protected void
loadModelMappers
(Class<?>[] entities) Load ModelMapper instances form mappers.zip and put in the cachebyte[]
serialize
(com.esotericsoftware.kryo.kryo5.Kryo kryo, org.modelmapper.ModelMapper mapper) Serialize theModelMapper
instance to a byte array - presumably to save to a file.protected void
serializeResources
(Class<?>[] entities, boolean hashShape) Serialize the ModelMapper instances and required classes and put to mappers.zip and classes.zip by default.void
setBeanClassLoader
(ClassLoader classLoader) void
setObjectMapper
(com.fasterxml.jackson.databind.ObjectMapper objectMapper) void
setProjectionFactory
(com.broadleafcommerce.common.extension.projection.ProjectionFactory projectionFactory) com.esotericsoftware.kryo.kryo5.Kryo
Create aKryo
instance and perform the initial setup.
-
Constructor Details
-
DefaultModelMapperSerializer
public DefaultModelMapperSerializer(DomainMapperMember domainMapperMember, DomainMapperManager domainMapperManager, ResourceProvider resourceProvider, com.broadleafcommerce.common.extension.TypeFactory typeFactory, KryoCustomizer kryoCustomizer, com.broadleafcommerce.common.extension.data.DataRouteReference reference, ModelMapperCacheProperties properties)
-
-
Method Details
-
setBeanClassLoader
- Specified by:
setBeanClassLoader
in interfaceorg.springframework.beans.factory.BeanClassLoaderAware
-
setup
Description copied from interface:ModelMapperSerializer
Create aKryo
instance and perform the initial setup. SeeKryoCustomizer
.- Specified by:
setup
in interfaceModelMapperSerializer
- Parameters:
entities
- The list of entities registered in the ORM.- Returns:
- The configured
Kryo
instance.
-
serialize
public byte[] serialize(com.esotericsoftware.kryo.kryo5.Kryo kryo, org.modelmapper.ModelMapper mapper) Description copied from interface:ModelMapperSerializer
Serialize theModelMapper
instance to a byte array - presumably to save to a file.- Specified by:
serialize
in interfaceModelMapperSerializer
- Parameters:
kryo
- TheKryo
instance to use for serializationmapper
- TheModelMapper
instance to serialize- Returns:
- The resulting byte array representing the serialized ModelMapper instance
-
deserialize
public org.modelmapper.ModelMapper deserialize(com.esotericsoftware.kryo.kryo5.Kryo kryo, byte[] serialized) Description copied from interface:ModelMapperSerializer
Given a byte array representation of aModelMapper
instance, deserialize into a concrete instance usingKryo
.- Specified by:
deserialize
in interfaceModelMapperSerializer
- Parameters:
kryo
- TheKryo
instance to use for deserializationserialized
- The serialized model mapper- Returns:
- The resulting ModelMapper instance after deserialization
-
hash
Description copied from interface:ModelMapperSerializer
Create a hash uniquely identifying the class structure, including fields and types in the object graph.- Specified by:
hash
in interfaceModelMapperSerializer
- Parameters:
entity
- The entity class to evaluate- Returns:
- The hash uniquely identifying the current structure of the entity class
-
generateCacheResources
Description copied from interface:ModelMapperSerializer
A rich lifecycle method, calls will generateModelMapper
instances for the specified entities, serialize those ModelMappers, and persist those mappers to disk in a file entitledmappers.zip
. The file is usually saved in the current application's source under the maven resources/cache directory structure. SeeResourceProvider
, and concrete implementations, for where and how the mappers.zip file is stored.classes.zip
is also created in the same location to hold any additional required classes that are needed to successfully deserialize the ModelMapper instances. These classes are generally injected into the target classloader upon deserialization (seeModelMapperSerializer.loadCacheResources(Class[])
).- Specified by:
generateCacheResources
in interfaceModelMapperSerializer
- Parameters:
entities
- The entities from which to derive ModelMapper instances for serialization
-
generateCacheResources
Description copied from interface:ModelMapperSerializer
A rich lifecycle method, calls will generateModelMapper
instances for the specified entities, serialize those ModelMappers, and persist those mappers to disk in a file entitledmappers.zip
. The file is usually saved in the current application's source under the maven resources/cache directory structure. SeeResourceProvider
, and concrete implementations, for where and how the mappers.zip file is stored.classes.zip
is also created in the same location to hold any additional required classes that are needed to successfully deserialize the ModelMapper instances. These classes are generally injected into the target classloader upon deserialization (seeModelMapperSerializer.loadCacheResources(Class[])
).- Specified by:
generateCacheResources
in interfaceModelMapperSerializer
- Parameters:
entities
- The entities from which to derive ModelMapper instances for serializationhashShapes
- Whether or not the mapper cache should include a deep hash of the entity shape. This is useful if the cache archive is separated from the build process and can become stale. Stale cache entries will not match the entity shape at runtime and will not be accidentally included. This value should be false if the cache is always built during project compilation.
-
loadCacheResources
Description copied from interface:ModelMapperSerializer
A rich lifecycle method, calls will readModelMapper
serialized files frommappers.zip
for the specified entities, deserialize those ModelMappers, and inject those mappers into cache upon application startup.classes.zip
is also consulted and classes discovered there are injected into the current classloader prior to attempting deserialization. SeeModelMapperSerializer.generateCacheResources(Class[])
for information on how mappers.zip and classes.zip originate.- Specified by:
loadCacheResources
in interfaceModelMapperSerializer
- Parameters:
entities
- The entities from which to deserialize ModelMapper instances and populate cache
-
serializeResources
Serialize the ModelMapper instances and required classes and put to mappers.zip and classes.zip by default. Alternatively, ifbroadleaf.modelmapper.cache.generate.useCacheInf
is true, the serialized output will instead be put on the classpath at CACHE-INF/modelmapper.- Parameters:
entities
- The entities for which ModelMapper instances are being createdhashShape
- seegenerateCacheResources(Class[], boolean)
.
-
appendAnyMissingClasses
Review the dump dir and add any pertinent dynamic classes that may have been missed during class resolution at the time of serialization.- Parameters:
dumpDir
- The location where ByteBuddy dumps dynamically created classescontext
- The context object containing the dynamic class registrations detected during serialization
-
getDumpFile
protected com.broadleafcommerce.data.tracking.core.mapping.cache.DefaultModelMapperSerializer.ClassFile getDumpFile(String dumpDir, String[] files, String packageName, String simpleName) If the system detects a dynamic class required during serialization, retrieve it from the dump location used by ByteBuddy. SeeModelMapperCacheProperties.getGenerate()
#getDumpFile(String, String, String)} for configuration of the location of the dump directory.- Parameters:
dumpDir
- The location where ByteBuddy dumps dynamically created classespackageName
- The package name for the class filesimpleName
- The simple name of the class- Returns:
- Stream to load the class file from disk
-
compileMappers
UsingModelMapperDomainMapperMember
, exercise the system to createModelMapper
instances for the requested entities.- Parameters:
entities
- The entities for which to generate ModelMapper instances
-
loadModelMappers
Load ModelMapper instances form mappers.zip and put in the cache- Parameters:
entities
- The entities for which ModelMappers should be identified
-
curateMapper
protected void curateMapper(org.modelmapper.ModelMapper mapper) Process the ModelMapper before populating in the cache- Parameters:
mapper
- The ModelMapper to modify prior to usage
-
injectDynamicClasses
protected void injectDynamicClasses()Inject all classes in classes.zip into the classloader -
setProjectionFactory
@Autowired public void setProjectionFactory(com.broadleafcommerce.common.extension.projection.ProjectionFactory projectionFactory) -
setObjectMapper
@Autowired public void setObjectMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
-