Class ChainingConverter

java.lang.Object
org.axonframework.serialization.ChainingConverter
All Implemented Interfaces:
Converter

public class ChainingConverter extends Object implements Converter
Converter implementation that will combine converters to form chains of converters to be able to convert from one type to another, for which there is no suitable single converter.

This implementation will also autodetect ContentTypeConverter implementations by scanning /META-INF/services/org.axonframework.serialization.ContentTypeConverter files on the classpath. These files must contain the fully qualified class names of the implementations to use.

Since:
2.0
Author:
Allard Buijze
  • Constructor Details

    • ChainingConverter

      public ChainingConverter()
      Initialize a new ChainingConverter with the context ClassLoader for this thread. Will autodetect all converters mentioned in /META-INF/services/org.axonframework.serialization.ContentTypeConverter files on the class path.

      Instances of ChainingConverter are safe for use in a multi-threaded environment, with exception of the registerConverter(ContentTypeConverter) method.

    • ChainingConverter

      public ChainingConverter(ClassLoader classLoader)
      Initialize a new ChainingConverter. Will autodetect all converters mentioned in /META-INF/services/org.axonframework.serialization.ContentTypeConverter files on the class path.

      Instances of ChainingConverter are safe for use in a multi-threaded environment, with exception of the registerConverter(ContentTypeConverter) method.

      Parameters:
      classLoader - the class loader used to load the converters
  • Method Details

    • canConvert

      public boolean canConvert(Class<?> sourceType, Class<?> targetType)
      Description copied from interface: Converter
      Indicates whether this converter is capable of converting the given sourceType to the targetType.
      Specified by:
      canConvert in interface Converter
      Parameters:
      sourceType - The type of data to convert from
      targetType - The type of data to convert to
      Returns:
      true if conversion is possible, false otherwise
    • convert

      public <T> T convert(Object original, Class<?> sourceType, Class<T> targetType)
      Description copied from interface: Converter
      Converts the given object into another using the source type to find the conversion path.
      Specified by:
      convert in interface Converter
      Type Parameters:
      T - the target data type
      Parameters:
      original - the value to convert
      sourceType - the type of data to convert
      targetType - The type of data to convert to
      Returns:
      the converted value
    • registerConverter

      public void registerConverter(ContentTypeConverter converter)
      Registers the given converter with this factory. The converter which is registered last will be inspected first when finding a suitable converter for a given input and output type.

      An alternative to explicit converter registration (but without the ordering guarantees) is to create a file called org.axonframework.serialization.ContentTypeConverter in /META-INF/services/ on the class path which contains the fully qualified class names of the converters, separated by newlines. These implementations must have a public no-arg constructor.

      Parameters:
      converter - the converter to register.
    • registerConverter

      public void registerConverter(Class<? extends ContentTypeConverter> converterType)
      Registers a convert of the given converterType with this factory, only if initialization of such a converter is possible. Both the expected source type and target type classes are checked for availability on the class path. In contrast to registerConverter(ContentTypeConverter), this method allows potentially unsafe (in terms of class dependencies) converters to be registered.

      The converter which is registered last will be inspected first when finding a suitable converter for a given input and output type.

      An alternative to explicit converter registration (but without the ordering guarantees) is to create a file called org.axonframework.serialization.ContentTypeConverter in /META-INF/services/ on the class path which contains the fully qualified class names of the converters, separated by newlines. These implementations must have a public no-arg constructor.

      Parameters:
      converterType - the type of converter to register.
    • setAdditionalConverters

      public void setAdditionalConverters(List<ContentTypeConverter> additionalConverters)
      Setter for dependency injection frameworks that require property methods. This method is the same as calling registerConverter(ContentTypeConverter) for each converter in the given list of additionalConverters.
      Parameters:
      additionalConverters - The converters to register with this factory
    • getContentTypeConverters

      public List<ContentTypeConverter> getContentTypeConverters()
      Retrieves the list of ContentTypeConverter registered in this ChainingConverter instance.
      Returns:
      unmodified list of content type converters.