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

    Constructors
    Constructor
    Description
    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 Summary

    Modifier and Type
    Method
    Description
    protected void
    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)
    Using ModelMapperDomainMapperMember, exercise the system to create ModelMapper instances for the requested entities.
    protected void
    curateMapper(org.modelmapper.ModelMapper mapper)
    Process the ModelMapper before populating in the cache
    org.modelmapper.ModelMapper
    deserialize(com.esotericsoftware.kryo.kryo5.Kryo kryo, byte[] serialized)
    Given a byte array representation of a ModelMapper instance, deserialize into a concrete instance using Kryo.
    void
    A rich lifecycle method, calls will generate ModelMapper instances for the specified entities, serialize those ModelMappers, and persist those mappers to disk in a file entitled mappers.zip.
    void
    generateCacheResources(Class<?>[] entities, boolean hashShapes)
    A rich lifecycle method, calls will generate ModelMapper instances for the specified entities, serialize those ModelMappers, and persist those mappers to disk in a file entitled mappers.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.
    hash(Class<?> entity)
    Create a hash uniquely identifying the class structure, including fields and types in the object graph.
    Map<String,byte[]>
     
    Map<String,byte[]>
     
    protected void
    Inject all classes in classes.zip into the classloader
    void
    loadCacheResources(Class<?>[] entities)
    A rich lifecycle method, calls will read ModelMapper serialized files from mappers.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 cache
    byte[]
    serialize(com.esotericsoftware.kryo.kryo5.Kryo kryo, org.modelmapper.ModelMapper mapper)
    Serialize the ModelMapper 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
     
    void
    setObjectMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
     
    void
    setProjectionFactory(com.broadleafcommerce.common.extension.projection.ProjectionFactory projectionFactory)
     
    com.esotericsoftware.kryo.kryo5.Kryo
    setup(Class<?>[] entities)
    Create a Kryo instance and perform the initial setup.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • Method Details

    • setBeanClassLoader

      public void setBeanClassLoader(ClassLoader classLoader)
      Specified by:
      setBeanClassLoader in interface org.springframework.beans.factory.BeanClassLoaderAware
    • setup

      public com.esotericsoftware.kryo.kryo5.Kryo setup(Class<?>[] entities)
      Description copied from interface: ModelMapperSerializer
      Create a Kryo instance and perform the initial setup. See KryoCustomizer.
      Specified by:
      setup in interface ModelMapperSerializer
      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 the ModelMapper instance to a byte array - presumably to save to a file.
      Specified by:
      serialize in interface ModelMapperSerializer
      Parameters:
      kryo - The Kryo instance to use for serialization
      mapper - The ModelMapper 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 a ModelMapper instance, deserialize into a concrete instance using Kryo.
      Specified by:
      deserialize in interface ModelMapperSerializer
      Parameters:
      kryo - The Kryo instance to use for deserialization
      serialized - The serialized model mapper
      Returns:
      The resulting ModelMapper instance after deserialization
    • hash

      public String hash(Class<?> entity)
      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 interface ModelMapperSerializer
      Parameters:
      entity - The entity class to evaluate
      Returns:
      The hash uniquely identifying the current structure of the entity class
    • generateCacheResources

      public void generateCacheResources(Class<?>[] entities)
      Description copied from interface: ModelMapperSerializer
      A rich lifecycle method, calls will generate ModelMapper instances for the specified entities, serialize those ModelMappers, and persist those mappers to disk in a file entitled mappers.zip. The file is usually saved in the current application's source under the maven resources/cache directory structure. See ResourceProvider, and concrete implementations, for where and how the mappers.zip file is stored.

      A file called 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 (see ModelMapperSerializer.loadCacheResources(Class[])).
      Specified by:
      generateCacheResources in interface ModelMapperSerializer
      Parameters:
      entities - The entities from which to derive ModelMapper instances for serialization
    • generateCacheResources

      public void generateCacheResources(Class<?>[] entities, boolean hashShapes)
      Description copied from interface: ModelMapperSerializer
      A rich lifecycle method, calls will generate ModelMapper instances for the specified entities, serialize those ModelMappers, and persist those mappers to disk in a file entitled mappers.zip. The file is usually saved in the current application's source under the maven resources/cache directory structure. See ResourceProvider, and concrete implementations, for where and how the mappers.zip file is stored.

      A file called 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 (see ModelMapperSerializer.loadCacheResources(Class[])).
      Specified by:
      generateCacheResources in interface ModelMapperSerializer
      Parameters:
      entities - The entities from which to derive ModelMapper instances for serialization
      hashShapes - 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

      public void loadCacheResources(Class<?>[] entities)
      Description copied from interface: ModelMapperSerializer
      A rich lifecycle method, calls will read ModelMapper serialized files from mappers.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. See ModelMapperSerializer.generateCacheResources(Class[]) for information on how mappers.zip and classes.zip originate.
      Specified by:
      loadCacheResources in interface ModelMapperSerializer
      Parameters:
      entities - The entities from which to deserialize ModelMapper instances and populate cache
    • serializeResources

      protected void serializeResources(Class<?>[] entities, boolean hashShape)
      Serialize the ModelMapper instances and required classes and put to mappers.zip and classes.zip by default. Alternatively, if broadleaf.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 created
      hashShape - see generateCacheResources(Class[], boolean).
    • appendAnyMissingClasses

      protected 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.
      Parameters:
      dumpDir - The location where ByteBuddy dumps dynamically created classes
      context - 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. See ModelMapperCacheProperties.getGenerate() #getDumpFile(String, String, String)} for configuration of the location of the dump directory.
      Parameters:
      dumpDir - The location where ByteBuddy dumps dynamically created classes
      packageName - The package name for the class file
      simpleName - The simple name of the class
      Returns:
      Stream to load the class file from disk
    • compileMappers

      protected void compileMappers(Class<?>[] entities)
      Using ModelMapperDomainMapperMember, exercise the system to create ModelMapper instances for the requested entities.
      Parameters:
      entities - The entities for which to generate ModelMapper instances
    • loadModelMappers

      protected void loadModelMappers(Class<?>[] entities)
      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
    • ingestArchive

      public Map<String,byte[]> ingestArchive()
    • ingestClasspath

      public Map<String,byte[]> ingestClasspath()
    • setProjectionFactory

      @Autowired public void setProjectionFactory(com.broadleafcommerce.common.extension.projection.ProjectionFactory projectionFactory)
    • setObjectMapper

      @Autowired public void setObjectMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper)