You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by "Olivier.Roger" <ol...@bsb.com> on 2010/09/14 16:58:42 UTC

[Converter] Generics ?

hello Camel!

Is it possible to use generics in annotated converters ?

I tried to do this

    @Converter
    public <T> T toPayload(Message<T> msg){
        return msg.getPayload();
    }

Which does not work. However, replacing <T> by a real class definition is
working for that type.
I would like to avoid to create one converter for each type of payload my
messages can have, is that possible ?

Thanks in advance,

Olivier
-- 
View this message in context: http://camel.465427.n5.nabble.com/Converter-Generics-tp2839250p2839250.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: [Converter] Generics ?

Posted by "Olivier.Roger" <ol...@bsb.com>.
Thanks for this information

My route fairly simple actually since I am testing the custom converters:

        <route streamCache="false">
            <from uri="direct:test" />
            <convertBodyTo type="Message" />
            <convertBodyTo type="NotificationType" />
            <bean ref="converter" method="showMsg" />
            <to uri="mock:test" />
        </route>

Message can contains any type of Payload, NotficationType is one of them.
The bean showMsg method simple log the content of the file:

    public NotificationType showMsg(NotificationType msg) {
        System.out.println(msg.xmlText());
        return msg;
    }

The explicit convertion to NotificationType is not necessary since when I
register a converter, it is used automatocally.

The convertion to Message however cannot be deduced automatically.

As I explained, when I used a normal converter this sample works fine :

    @Converter
    private NotificationType toPayload(Message<NotificationType> msg){
        return msg.getPayload();
    }

Since Message can hold ony type of paylaod I would like this method to be
generic, so I tried to use the FallBackconverter like this:

    @FallbackConverter
    public <T> T toPayload(Class<T> type, Exchange exchange, Object value,
TypeConverterRegistry registry){
        if (Message.class.isAssignableFrom(value.getClass())) {
            return ((Message) value).getPayload();
        }
        return null;
    }

I'll try to debug to find the exact source of the problem.
-- 
View this message in context: http://camel.465427.n5.nabble.com/Converter-Generics-tp2839250p2840278.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: [Converter] Generics ?

Posted by Claus Ibsen <cl...@gmail.com>.
On Wed, Sep 15, 2010 at 10:06 AM, Claus Ibsen <cl...@gmail.com> wrote:
> On Wed, Sep 15, 2010 at 10:03 AM, Olivier.Roger <ol...@bsb.com> wrote:
>>
>> You're right, I spoke too soon. sorry.
>>
>> The documentation mix streamCaching and streamCache on the same page (I
>> suppose it changed on 2.0).
>> However, event with streamCache="fasle", the route still attempt to convert
>> from my custom Type to StreamCache.
>>
>> I don't really see why it would do that if streamCache is set to false. Any
>> idea about that?
>

If you route to a log endpoint then the logger will try to convert to
stream cache to be able to log the stream without causing the stream
to be "closed" afterwards.
In your custom fallback type converter you should just ignore this and
only convert the types you can do. So do an instanceof check
beforehand, and not a direct type cast.

Because its a fallback type converter it will be invoked for many
different types, when Camel cannot find a dedicated type converter at
first.


> No use a debugger.
>
> What do you do in your route? Do you use Jetty by any chance?
>
>
>
>> --
>> View this message in context: http://camel.465427.n5.nabble.com/Converter-Generics-tp2839250p2840234.html
>> Sent from the Camel - Users mailing list archive at Nabble.com.
>>
>
>
>
> --
> Claus Ibsen
> Apache Camel Committer
>
> Author of Camel in Action: http://www.manning.com/ibsen/
> Open Source Integration: http://fusesource.com
> Blog: http://davsclaus.blogspot.com/
> Twitter: http://twitter.com/davsclaus
>



-- 
Claus Ibsen
Apache Camel Committer

Author of Camel in Action: http://www.manning.com/ibsen/
Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus

Re: [Converter] Generics ?

Posted by Claus Ibsen <cl...@gmail.com>.
On Wed, Sep 15, 2010 at 10:03 AM, Olivier.Roger <ol...@bsb.com> wrote:
>
> You're right, I spoke too soon. sorry.
>
> The documentation mix streamCaching and streamCache on the same page (I
> suppose it changed on 2.0).
> However, event with streamCache="fasle", the route still attempt to convert
> from my custom Type to StreamCache.
>
> I don't really see why it would do that if streamCache is set to false. Any
> idea about that?

No use a debugger.

What do you do in your route? Do you use Jetty by any chance?



> --
> View this message in context: http://camel.465427.n5.nabble.com/Converter-Generics-tp2839250p2840234.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>



-- 
Claus Ibsen
Apache Camel Committer

Author of Camel in Action: http://www.manning.com/ibsen/
Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus

Re: [Converter] Generics ?

Posted by "Olivier.Roger" <ol...@bsb.com>.
You're right, I spoke too soon. sorry.

The documentation mix streamCaching and streamCache on the same page (I
suppose it changed on 2.0).
However, event with streamCache="fasle", the route still attempt to convert
from my custom Type to StreamCache.

I don't really see why it would do that if streamCache is set to false. Any
idea about that?
-- 
View this message in context: http://camel.465427.n5.nabble.com/Converter-Generics-tp2839250p2840234.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: [Converter] Generics ?

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

Please double check according to the documentation
http://camel.apache.org/stream-caching.html

The attribute is named streamCache in XML.
Also the XSD contains the correct names etc.

If you use a good editor it can assist you when editing XML files and
help with auto completion and whatnot.


On Wed, Sep 15, 2010 at 9:47 AM, Olivier.Roger <ol...@bsb.com> wrote:
>
> Claus, FallbackConverter seems to be something that could work indeed.
>
> However, when I implement the my converter that way I get this error
> message:
>
> Caused by: java.lang.ClassCastException:
> com.bsb.docgen.xml.notification.impl.NotificationTypeImpl cannot be cast to
> org.apache.camel.StreamCache
>
> Accorindg to the online documentation
> (http://camel.apache.org/stream-caching.html) the stream-caching feature is
> disabled by default in Camel 2.0+
>
> When I try to force disabling the feature by setting <route
> streamCaching="false">, It appears to have been removed completely from the
> XML Schema:
>
> Caused by: org.xml.sax.SAXParseException: cvc-complex-type.3.2.2: Attribute
> 'streamCaching' is not allowed to appear in element 'route'.
>
> Any idea on how to solve this ?
> --
> View this message in context: http://camel.465427.n5.nabble.com/Converter-Generics-tp2839250p2840218.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>



-- 
Claus Ibsen
Apache Camel Committer

Author of Camel in Action: http://www.manning.com/ibsen/
Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus

Re: [Converter] Generics ?

Posted by "Olivier.Roger" <ol...@bsb.com>.
Claus, FallbackConverter seems to be something that could work indeed.

However, when I implement the my converter that way I get this error
message:

Caused by: java.lang.ClassCastException:
com.bsb.docgen.xml.notification.impl.NotificationTypeImpl cannot be cast to
org.apache.camel.StreamCache

Accorindg to the online documentation
(http://camel.apache.org/stream-caching.html) the stream-caching feature is
disabled by default in Camel 2.0+

When I try to force disabling the feature by setting <route
streamCaching="false">, It appears to have been removed completely from the
XML Schema:

Caused by: org.xml.sax.SAXParseException: cvc-complex-type.3.2.2: Attribute
'streamCaching' is not allowed to appear in element 'route'.

Any idea on how to solve this ?
-- 
View this message in context: http://camel.465427.n5.nabble.com/Converter-Generics-tp2839250p2840218.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: [Converter] Generics ?

Posted by Claus Ibsen <cl...@gmail.com>.
On Tue, Sep 14, 2010 at 4:58 PM, Olivier.Roger <ol...@bsb.com> wrote:
>
> hello Camel!
>
> Is it possible to use generics in annotated converters ?
>
> I tried to do this
>
>    @Converter
>    public <T> T toPayload(Message<T> msg){
>        return msg.getPayload();
>    }
>
> Which does not work. However, replacing <T> by a real class definition is
> working for that type.
> I would like to avoid to create one converter for each type of payload my
> messages can have, is that possible ?
>

Use the @FallbackConverter
http://camel.apache.org/type-converter.html


> Thanks in advance,
>
> Olivier
> --
> View this message in context: http://camel.465427.n5.nabble.com/Converter-Generics-tp2839250p2839250.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>



-- 
Claus Ibsen
Apache Camel Committer

Author of Camel in Action: http://www.manning.com/ibsen/
Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus