You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Bengt Rodehav <be...@rodehav.com> on 2014/03/20 10:27:11 UTC

Adding custom type converter

I'm using Camel 2.12.3 running in Karaf 2.3.4.

I'm trying to add a custom type converter to my context but can't get it to
work. My code is essentially as follows:

getContext().getTypeConverterRegistry().addTypeConverter(MmlMessage.class,
InputStream.class, new MmlMessage2StringTypeConverter());

from(...).process(new Processor() {
  public void process(Exchange theExchange) throws Exception {
    Message message = theExchange.getIn();
    MmlMessage mmlMessage = ... // Create an MmlMessage

    System.out.println("Trying to convert");
    try {
      TypeConverter tc = theExchange.getContext().getTypeConverter();
      System.out.println("tc: " + tc);
      InputStream is = tc.convertTo(InputStream.class, mmlMessage);
      System.out.println("is: " + is);
    } catch (Exception e) {
      System.out.println("Conversion failed");
      e.printStackTrace();
    }
  }
})

My type converter looks something like this:

public class MmlMessage2StringTypeConverter extends TypeConverterSupport {
  @SuppressWarnings("unchecked")
  public <T> T convertTo(Class<T> theType, Exchange theExchange, Object
theValue) {
    System.out.println("convertTo: " + theValue);
    InputStream stream = null;
    ... // Create an InputStream from an MmlMessage
    return (T) stream;
  }
}

The output I get in the Karaf console is:

Trying to convert
tc: org.apache.camel.core.osgi.OsgiTypeConverter@4316e1c9
is: null

Thus my type converter is never found and never used.

Am I'm doing this wrong? Isn't this the way to add a custom type converter?

/Bengt

Re: Adding custom type converter

Posted by Bengt Rodehav <be...@rodehav.com>.
Sorry - an idiotic mistake from my side. I had switched the "from class"
and the "to class".

getContext().getTypeConverterRegistry().addTypeConverter(MmlMessage.class,
InputStream.class, new MmlMessage2StringTypeConverter());

should instead be:

getContext().getTypeConverterRegistry().addTypeConverter(InputStream.class,
MmlMessage.class, new MmlMessage2StringTypeConverter());

/Bengt


2014-03-20 10:27 GMT+01:00 Bengt Rodehav <be...@rodehav.com>:

> I'm using Camel 2.12.3 running in Karaf 2.3.4.
>
> I'm trying to add a custom type converter to my context but can't get it
> to work. My code is essentially as follows:
>
> getContext().getTypeConverterRegistry().addTypeConverter(MmlMessage.class,
> InputStream.class, new MmlMessage2StringTypeConverter());
>
> from(...).process(new Processor() {
>   public void process(Exchange theExchange) throws Exception {
>     Message message = theExchange.getIn();
>     MmlMessage mmlMessage = ... // Create an MmlMessage
>
>     System.out.println("Trying to convert");
>     try {
>       TypeConverter tc = theExchange.getContext().getTypeConverter();
>       System.out.println("tc: " + tc);
>       InputStream is = tc.convertTo(InputStream.class, mmlMessage);
>       System.out.println("is: " + is);
>     } catch (Exception e) {
>       System.out.println("Conversion failed");
>       e.printStackTrace();
>     }
>   }
> })
>
> My type converter looks something like this:
>
> public class MmlMessage2StringTypeConverter extends TypeConverterSupport {
>   @SuppressWarnings("unchecked")
>   public <T> T convertTo(Class<T> theType, Exchange theExchange, Object
> theValue) {
>     System.out.println("convertTo: " + theValue);
>     InputStream stream = null;
>     ... // Create an InputStream from an MmlMessage
>     return (T) stream;
>   }
> }
>
> The output I get in the Karaf console is:
>
> Trying to convert
> tc: org.apache.camel.core.osgi.OsgiTypeConverter@4316e1c9
> is: null
>
> Thus my type converter is never found and never used.
>
> Am I'm doing this wrong? Isn't this the way to add a custom type converter?
>
> /Bengt
>
>