Class 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
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      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.
      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 generateCacheResources​(Class<?>[] entities)
      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 packageName, String simpleName)
      If the system detects a dynamic class required during serialization, retrieve it from the dump location used by ByteBuddy.
      String hash​(Class<?> entity)
      Create a hash uniquely identifying the class structure, including fields and types in the object graph.
      protected void injectDynamicClasses()
      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)
      Serialize the ModelMapper instances and required classes to mappers.zip and classes.zip, respectively.
      void setBeanClassLoader​(ClassLoader classLoader)  
      com.esotericsoftware.kryo.kryo5.Kryo setup​(Class<?>[] entities)
      Create a Kryo instance and perform the initial setup.
    • Method Detail

      • 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
      • 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)
        Serialize the ModelMapper instances and required classes to mappers.zip and classes.zip, respectively.
        Parameters:
        entities - The entities for which ModelMapper instances are being created
      • 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 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