Class ChainingContentTypeConverter

java.lang.Object
org.axonframework.conversion.ChainingContentTypeConverter
All Implemented Interfaces:
DescribableComponent, Converter

public class ChainingContentTypeConverter extends Object implements Converter
A Converter implementation that will combine ContentTypeConverters 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.conversion.ContentTypeConverter files on the classpath. These files must contain the fully qualified class names of the implementations to use.

Note that since this Converter acts on the ContentTypeConverter, and a ContentTypeConverter only works with Classes, that the ChainingContentTypeConverter can only work with source and target types that are a Class. Hence, if the Type that is given to canConvert(Type, Type) or convert(Object, Type) is not a Class, those methods return early. In case of canConvert, false will be returned. For convert, an ConversionException is thrown.

Since:
2.0.0
Author:
Allard Buijze
  • Constructor Details

    • ChainingContentTypeConverter

      public ChainingContentTypeConverter()
      Initialize a new ChainingConverter with the context ClassLoader for this thread.

      Will autodetect all ContentTypeConverters mentioned in /META-INF/services/org.axonframework.conversion.ContentTypeConverter files on the class path.

      Instances of ChainingConverter are safe for use in a multithreaded environment, except for the registerConverter(ContentTypeConverter) method.

    • ChainingContentTypeConverter

      public ChainingContentTypeConverter(@Nonnull ClassLoader classLoader)
      Initialize a new ChainingConverter with the given classLoader.

      Will autodetect all ContentTypeConverters mentioned in /META-INF/services/org.axonframework.conversion.ContentTypeConverter files on the class path.

      Instances of ChainingConverter are safe for use in a multithreaded environment, except for the registerConverter(ContentTypeConverter) method.

      Parameters:
      classLoader - The class loader used to load the ContentTypeConverters.
  • Method Details

    • canConvert

      public boolean canConvert(@Nonnull Type sourceType, @Nonnull Type 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

      @Nullable public <T> T convert(@Nullable Object input, @Nonnull Type targetType)
      Description copied from interface: Converter
      Converts the given input object into an object of the given targetType.
      Specified by:
      convert in interface Converter
      Type Parameters:
      T - The target data type.
      Parameters:
      input - The value to convert.
      targetType - The type to convert the given input into.
      Returns:
      A converted version of the given input into the given targetType.
    • registerConverter

      public void registerConverter(@Nonnull ContentTypeConverter<?,?> converter)
      Registers the given ContentTypeConverter with this ChainingConverter.

      The converter that 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.conversion.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 with this ChainingConverter.
    • registerConverter

      public void registerConverter(@Nonnull Class<? extends ContentTypeConverter<?,?>> converterType)
      Registers a ContentTypeConverter 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 that 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.conversion.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(@Nonnull List<ContentTypeConverter<?,?>> additionalConverters)
      A ContentTypeConverter 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 additional converters to register with this ChainingConverter.
    • getContentTypeConverters

      @Nonnull public List<ContentTypeConverter<?,?>> getContentTypeConverters()
      Retrieves the list of ContentTypeConverters registered in this ChainingConverter instance.
      Returns:
      Unmodified list of all ContentTypeConverters registered with this ChainingConverter.
    • describeTo

      public void describeTo(@Nonnull ComponentDescriptor descriptor)
      Description copied from interface: DescribableComponent
      Describe the properties of this DescribableComponent with the given descriptor.

      Components should call the appropriate describeProperty methods on the descriptor to register their properties. The descriptor is responsible for determining how these properties are formatted and structured in the final output.

      Best Practices: As a general rule, all relevant fields of a DescribableComponent implementation should be described in this method. However, developers have discretion to include only the fields that make sense in the context. Not every field may be meaningful for description purposes, especially internal implementation details. Furthermore, components might want to expose different information based on their current state. The final decision on what properties to include lies with the person implementing the describeTo method, who should focus on providing information that is useful for understanding the component's configuration and state.

      Example implementation:

       public void describeTo(ComponentDescriptor descriptor) {
           descriptor.describeProperty("name", this.name);
           descriptor.describeProperty("enabled", this.enabled);
           descriptor.describeProperty("configuration", this.configuration); // A nested component
           descriptor.describeProperty("handlers", this.eventHandlers);      // A collection
       }
       
      Specified by:
      describeTo in interface DescribableComponent
      Parameters:
      descriptor - The component descriptor to describe this DescribableComponentn its properties in.