You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@activemq.apache.org by James Strachan <ja...@gmail.com> on 2007/03/29 18:07:26 UTC

[CAMEL] Heads up - generic type conversion mechanism implemented

Its very common when routing from one endpoint to another to need to
convert the body payloads from one type to another such as to/from
objects of types

byte[]/ByteBuffer/String/InputStream/OutputStream/Reader/Writer/Document/Source/File

I've added a default implementation of the TypeConverter which is
capable of converting between types.  It works by searching the
classpath for files called:
META-INF/services/org/apache/camel/TypeConverter which the contents
are expected to be comma separated package names. These packages are
then recursively searched for any objects with the @Converter
annotation. Then any method marked with @Converter is assumed to be a
conversion method; where the parameter is the from value and the
return is the to value.

e.g. the following shows how to register a converter from File -> InputStream

@Converter
public class IOConverter {

    @Converter
    public static InputStream toInputStream(File file) throws
FileNotFoundException {
        return new BufferedInputStream(new FileInputStream(file));
    }
}


I think we've most of the common converters done. I've documented how
this all works in a little more detail here

http://cwiki.apache.org/CAMEL/type-converter.html

Note this mechanism isn't a replacement for all transformers; custom
transformers can still be plugged into a pipeline. Its just to
automate away lots of the drudgery of turning byte[] into String or
Document or Source etc.
-- 

James
-------
http://radio.weblogs.com/0112098/