Class ChainingContentTypeConverter
- All Implemented Interfaces:
DescribableComponent,Converter
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 Summary
ConstructorsConstructorDescriptionChainingContentTypeConverter(ClassLoader classLoader) Initialize a newChainingConverterwith the givenclassLoader. -
Method Summary
Modifier and TypeMethodDescriptionbooleancanConvert(Type sourceType, Type targetType) Indicates whether thisConverteris capable of converting the givensourceTypeto thetargetType.<T> TConverts the giveninputobject into an object of the giventargetType.voiddescribeTo(ComponentDescriptor descriptor) Describe the properties ofthis DescribableComponentwith the givendescriptor.List<ContentTypeConverter<?, ?>> Retrieves the list ofContentTypeConvertersregistered in thisChainingConverterinstance.voidregisterConverter(Class<? extends ContentTypeConverter<?, ?>> converterType) Registers aContentTypeConverterof the givenconverterTypewith this factory, only if initialization of such a converter is possible.voidregisterConverter(ContentTypeConverter<?, ?> converter) Registers the givenContentTypeConverterwith thisChainingConverter.voidsetAdditionalConverters(List<ContentTypeConverter<?, ?>> additionalConverters) AContentTypeConvertersetter for dependency injection frameworks that require property methods.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.axonframework.conversion.Converter
canConvert, convert
-
Constructor Details
-
ChainingContentTypeConverter
public ChainingContentTypeConverter()Initialize a newChainingConverterwith the contextClassLoaderforthis thread.Will autodetect all
ContentTypeConvertersmentioned in/META-INF/services/org.axonframework.conversion.ContentTypeConverterfiles on the class path.Instances of
ChainingConverterare safe for use in a multithreaded environment, except for theregisterConverter(ContentTypeConverter)method. -
ChainingContentTypeConverter
Initialize a newChainingConverterwith the givenclassLoader.Will autodetect all
Instances ofContentTypeConvertersmentioned in/META-INF/services/org.axonframework.conversion.ContentTypeConverterfiles on the class path.ChainingConverterare safe for use in a multithreaded environment, except for theregisterConverter(ContentTypeConverter)method.- Parameters:
classLoader- The class loader used to load theContentTypeConverters.
-
-
Method Details
-
canConvert
Description copied from interface:ConverterIndicates whether thisConverteris capable of converting the givensourceTypeto thetargetType.- Specified by:
canConvertin interfaceConverter- Parameters:
sourceType- The type of data to convert from.targetType- The type of data to convert to.- Returns:
trueif conversion is possible,falseotherwise.
-
convert
Description copied from interface:ConverterConverts the giveninputobject into an object of the giventargetType. -
registerConverter
Registers the givenContentTypeConverterwith thisChainingConverter.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 calledorg.axonframework.conversion.ContentTypeConverterin/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 thisChainingConverter.
-
registerConverter
Registers aContentTypeConverterof the givenconverterTypewith 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
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 calledregisterConverter(ContentTypeConverter), this method allows potentially unsafe (in terms of class dependencies) converters to be registered.org.axonframework.conversion.ContentTypeConverterin/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
AContentTypeConvertersetter for dependency injection frameworks that require property methods.This method is the same as calling
registerConverter(ContentTypeConverter)for each converter in the given list ofadditionalConverters.- Parameters:
additionalConverters- The additionalconvertersto register with thisChainingConverter.
-
getContentTypeConverters
Retrieves the list ofContentTypeConvertersregistered in thisChainingConverterinstance.- Returns:
- Unmodified list of all
ContentTypeConvertersregistered with thisChainingConverter.
-
describeTo
Description copied from interface:DescribableComponentDescribe the properties ofthis DescribableComponentwith the givendescriptor.Components should call the appropriate
describePropertymethods 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
DescribableComponentimplementation 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 thedescribeTomethod, 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:
describeToin interfaceDescribableComponent- Parameters:
descriptor- The component descriptor to describethis DescribableComponentn its properties in.
-