LibraryLink ToToggle FramesPrintFeedback

Implementing a Custom Type Converter

The type conversion mechanism can easily be customized by adding a new slave type converter. This section describes how to implement a slave type converter and how to integrate it with FUSE Mediation Router, so that it is automatically loaded by the annotation type converter loader.

To implement a custom type converter, perform the following steps:

You can implement a custom type converter class using the @Converter annotation. You must annotate the class itself and each of the methods intended to perform type conversion. Each converter method must take a single argument, which defines the from type, and a non-void return value, which defines the to type. The type converter loader uses Java reflection to find the annotated methods and integrate them into the type converter mechanism. Example 3.3 shows an example of an annotated converter class that defines a single converter method for converting from java.io.File to java.io.InputStream.


The toInputStream() method is responsible for performing the conversion from the File type to the InputStream type.

[Note]Note

The method name is unimportant, and can be anything you choose. What is important are the argument type, the return type, and the presence of the @Converter annotation.

To enable the discovery mechanism (which is implemented by the annotation type converter loader) for your custom converter, create a TypeConverter file at the following location:

META-INF/services/org/apache/camel/TypeConverter

The TypeConverter file must contain a comma-separated list of package names identifying the packages that contain type converter classes. For example, if you want the type converter loader to search the com.YourDomain.YourPackageName package for annotated converter classes, the TypeConverter file would have the following contents:

com.YourDomain.YourPackageName

The type converter is packaged as a JAR file containing the compiled classes of your custom type converters and the META-INF directory. Put this JAR file on your classpath to make it available to your FUSE Mediation Router application.