You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by lb <lb...@gmail.com> on 2013/01/10 14:58:47 UTC

[camel-bindy] bindy ignore bean class type

Hi all,
it looks like camel-bindy does not take into account the class specified in
the method:

    bindy(BindyType.Csv, MyCamelBeans.TEST.class)

I have class MyCamelBeans which includes several "bindies" among which I
have a really simple MyCamelBeans.TEST:

    class MyCamelBeans {
        @CsvRecord(separator=";",isOrdered=true)
        public static final class TEST {
            @DataField(trim=true,pos=  1) public String tradingDate;
            @DataField(trim=true,pos=  2) public String isinCode;
        }

        ... some other @CsvRecord ....
    }

I also have some @FixedLengthRecord classes defined in the same package.
If I run the following route:

   
from("file:data/test?noop=true&idempotent=true&fileName=test.csv&delay=60000")
        .split(body(String.class).tokenize("\n")).streaming()
        .unmarshal().bindy(BindyType.Csv, MyCamelBeans.TEST.class)
        .to("log:DS?level=INFO&showHeaders=true");

Camel (2.10.3) fails with an NPE:

    java.lang.NullPointerException
	    at
org.apache.camel.dataformat.bindy.BindyCsvFactory.setDefaultValuesForFields(BindyCsvFactory.java:583)
	    at
org.apache.camel.dataformat.bindy.BindyCsvFactory.bind(BindyCsvFactory.java:230)

if I remove the other bindies, camel-bindy works as expected.
It looks like camel ignore the class specified in the fluent method
bindy(type,class): am I wrong?

thx - Luca






--
View this message in context: http://camel.465427.n5.nabble.com/camel-bindy-bindy-ignore-bean-class-type-tp5725275.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: [camel-bindy] bindy ignore bean class type

Posted by lb <lb...@gmail.com>.
I fixed CAMEL-5958 in my own repo on GitHub
(https://github.com/lburgazzoli/camel) 
I would greatly appreciate any feedback before to open a pull request.



--
View this message in context: http://camel.465427.n5.nabble.com/camel-bindy-bindy-ignore-bean-class-type-tp5725275p5745206.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: [camel-bindy] bindy ignore bean class type

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

On Tue, Jan 15, 2013 at 8:54 AM, lb <lb...@gmail.com> wrote:
> Hi,
> if you give me some hint I may start working on it (not sure to have enough
> skill but I may try).
>

That sounds great.

You may at first polish the code a bit just to get more familiar with
the existing code base.

The idea for the avoid package scanning stuff would roughly be something a like

the model init code should use the given class type as the "root".
Then traverse the root object and do "its init stuff" eg scan for
bindy annotations etc.
There is some annotations that refer to other class types (so you can
do parent -> child).
And when you hit these, then traverse down them to do "its init stuff"
as well. But mind about
circular dependencies which should be detected/avoided.

eg the idea is to use the "root" and its references to other types
only when init the model.
Then you avoid loading in other types, etc.

And we can have 2+ types in the same package without any problems.


> thx
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/camel-bindy-bindy-ignore-bean-class-type-tp5725275p5725566.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: cibsen@redhat.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Re: [camel-bindy] bindy ignore bean class type

Posted by lb <lb...@gmail.com>.
Hi, 
if you give me some hint I may start working on it (not sure to have enough
skill but I may try).

thx



--
View this message in context: http://camel.465427.n5.nabble.com/camel-bindy-bindy-ignore-bean-class-type-tp5725275p5725566.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: [camel-bindy] bindy ignore bean class type

Posted by lb <lb...@gmail.com>.
Done: https://issues.apache.org/jira/browse/CAMEL-5958



--
View this message in context: http://camel.465427.n5.nabble.com/camel-bindy-bindy-ignore-bean-class-type-tp5725275p5725402.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: [camel-bindy] bindy ignore bean class type

Posted by Claus Ibsen <cl...@gmail.com>.
On Fri, Jan 11, 2013 at 3:35 PM, lb <lb...@gmail.com> wrote:
> Shall I open a Jira?
>

Yeah bindy needs to be refactored so it
- requires the class type (no more packages)
- uses the class type as root model
- ... and initializes the model from the root, and follow and
references to other classes (if the root has childs)

This avoids picking up any other models.
And we would know to marshal/unmarshal based on the root.




>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/camel-bindy-bindy-ignore-bean-class-type-tp5725275p5725393.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: cibsen@redhat.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Re: [camel-bindy] bindy ignore bean class type

Posted by lb <lb...@gmail.com>.
Shall I open a Jira?



--
View this message in context: http://camel.465427.n5.nabble.com/camel-bindy-bindy-ignore-bean-class-type-tp5725275p5725393.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: [camel-bindy] bindy ignore bean class type

Posted by Claus Ibsen <cl...@gmail.com>.
On Fri, Jan 11, 2013 at 2:27 PM, lb <lb...@gmail.com> wrote:
> That is the method I'm currently using but according to the class
> BindyAbstractFactory, bindy scan the packages of the class used as parameter
> instead of the class itself, isn't it?
>

Yes as said bindy was created with that package scanning from the start.
It takes a bigger refactoring of the codebase to improve and fix on that.



>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/camel-bindy-bindy-ignore-bean-class-type-tp5725275p5725380.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: cibsen@redhat.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Re: [camel-bindy] bindy ignore bean class type

Posted by lb <lb...@gmail.com>.
That is the method I'm currently using but according to the class
BindyAbstractFactory, bindy scan the packages of the class used as parameter
instead of the class itself, isn't it?



--
View this message in context: http://camel.465427.n5.nabble.com/camel-bindy-bindy-ignore-bean-class-type-tp5725275p5725380.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: [camel-bindy] bindy ignore bean class type

Posted by Claus Ibsen <cl...@gmail.com>.
On Fri, Jan 11, 2013 at 11:56 AM, lb <lb...@gmail.com> wrote:
>
> What about to add an additional parameter to the bindy method in fluent
> language?
> Like an enum or a constant to instruct bindy to perform a package scan or to
> use the class configured.
>

Isn't there such already? See this test
https://svn.apache.org/repos/asf/camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/classtype/BindyCsvClassTypeTest.java

If there is a Class parameter then it ought to work with the class directly
and not do any package scanning.



>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/camel-bindy-bindy-ignore-bean-class-type-tp5725275p5725367.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: cibsen@redhat.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Re: [camel-bindy] bindy ignore bean class type

Posted by lb <lb...@gmail.com>.
What about to add an additional parameter to the bindy method in fluent
language? 
Like an enum or a constant to instruct bindy to perform a package scan or to
use the class configured. 




--
View this message in context: http://camel.465427.n5.nabble.com/camel-bindy-bindy-ignore-bean-class-type-tp5725275p5725367.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: [camel-bindy] bindy ignore bean class type

Posted by Claus Ibsen <cl...@gmail.com>.
On Thu, Jan 10, 2013 at 4:35 PM, lb <lb...@gmail.com> wrote:
>
> I think the problem is in BindyAbstractFactory.java :
>
>     /**
>      * method uses to initialize the model representing the classes who will
>      * bind the data. This process will scan for classes according to the
>      * package name provided, check the annotated classes and fields.
>      *
>      * @throws Exception
>      */
>     public void initModel() throws Exception {
>         // Find classes defined as Model
>         if (packageNames != null)  {
>             initModelClasses(this.packageNames);
>         } else if (type != null) {
>             // use the package name from the type as it may refer to types
> in the same package
>             String pckName = type.getPackage().getName();
>             initModelClasses(pckName);
>         } else {
>             throw new IllegalArgumentException("Either packagenames or type
> should be configured");
>         }
>     }
>
> Looks like bindy scan the package even a class has been specified.
> This force people to write beans in different packages and in my opinion it
> is a wrong behavior; isn't it?
>

Yeah unfortunately bindy was created with the package scanning from the start.
This is a mistake, but its not so easy to fix, as it breaks backwards
compatibility.

In Camel 3.0 we have the luxury of being able to fix that.


>
>
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/camel-bindy-bindy-ignore-bean-class-type-tp5725275p5725298.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: cibsen@redhat.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Re: [camel-bindy] bindy ignore bean class type

Posted by lb <lb...@gmail.com>.
I think the problem is in BindyAbstractFactory.java :

    /**
     * method uses to initialize the model representing the classes who will
     * bind the data. This process will scan for classes according to the
     * package name provided, check the annotated classes and fields.
     *
     * @throws Exception
     */
    public void initModel() throws Exception {
        // Find classes defined as Model
        if (packageNames != null)  {
            initModelClasses(this.packageNames);
        } else if (type != null) {
            // use the package name from the type as it may refer to types
in the same package
            String pckName = type.getPackage().getName();
            initModelClasses(pckName);
        } else {
            throw new IllegalArgumentException("Either packagenames or type
should be configured");
        }
    }

Looks like bindy scan the package even a class has been specified. 
This force people to write beans in different packages and in my opinion it
is a wrong behavior; isn't it?







--
View this message in context: http://camel.465427.n5.nabble.com/camel-bindy-bindy-ignore-bean-class-type-tp5725275p5725298.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: [camel-bindy] bindy ignore bean class type

Posted by lb <lb...@gmail.com>.
Hi, it works if the beans are in a different package not if they are in the
same one



--
View this message in context: http://camel.465427.n5.nabble.com/camel-bindy-bindy-ignore-bean-class-type-tp5725275p5725291.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: [camel-bindy] bindy ignore bean class type

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

Try moving your class outside, so its not inlined in another class.
I would assume Bindy requires that.

On Thu, Jan 10, 2013 at 2:58 PM, lb <lb...@gmail.com> wrote:
> Hi all,
> it looks like camel-bindy does not take into account the class specified in
> the method:
>
>     bindy(BindyType.Csv, MyCamelBeans.TEST.class)
>
> I have class MyCamelBeans which includes several "bindies" among which I
> have a really simple MyCamelBeans.TEST:
>
>     class MyCamelBeans {
>         @CsvRecord(separator=";",isOrdered=true)
>         public static final class TEST {
>             @DataField(trim=true,pos=  1) public String tradingDate;
>             @DataField(trim=true,pos=  2) public String isinCode;
>         }
>
>         ... some other @CsvRecord ....
>     }
>
> I also have some @FixedLengthRecord classes defined in the same package.
> If I run the following route:
>
>
> from("file:data/test?noop=true&idempotent=true&fileName=test.csv&delay=60000")
>         .split(body(String.class).tokenize("\n")).streaming()
>         .unmarshal().bindy(BindyType.Csv, MyCamelBeans.TEST.class)
>         .to("log:DS?level=INFO&showHeaders=true");
>
> Camel (2.10.3) fails with an NPE:
>
>     java.lang.NullPointerException
>             at
> org.apache.camel.dataformat.bindy.BindyCsvFactory.setDefaultValuesForFields(BindyCsvFactory.java:583)
>             at
> org.apache.camel.dataformat.bindy.BindyCsvFactory.bind(BindyCsvFactory.java:230)
>
> if I remove the other bindies, camel-bindy works as expected.
> It looks like camel ignore the class specified in the fluent method
> bindy(type,class): am I wrong?
>
> thx - Luca
>
>
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/camel-bindy-bindy-ignore-bean-class-type-tp5725275.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: cibsen@redhat.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen