Some more steps, hopefully in the right direction:
I defined the logic of a class transformer that inject a new field for every
command line option to be validated and initialize the field with the value
passed as input
There are two missing pieces:
- How can I add annotations the newly added fields such that they will be
validated ?
Does it make any sense ti add Annotations to field based on the
validators' name given as contribution ?
If I not, how can I use the validator without injecting annotations
inside the fields ?
- When should I run the class transformer to avoid ClassCast exception ? I
suspect that I need to process the class once at the very beginning and then
use proxy to it when running the validation.
Am I correct ?
Thanks
-- Alessio
On Sep 25, 2013, at 5:19 PM, Alessio Gambi wrote:
> A little update:
>
> I managed to get rid of the wrong slf4j library via the exclude tag inside
> maven I can use the bean validator with no problem.
>
> To have tapestry-beanvalidator working I had to "copy&paste" a subset of the
> original BeanValidatorModule inside my AppModule; in fact with the original
> code I always got this exception:
>> java.lang.IllegalArgumentException: Contribution
>> org.apache.tapestry5.beanvalidator.BeanValidatorModule.addBeanValidationStack(MappedConfiguration)
>> (at BeanValidatorModule.java:104) is for service 'interface
>> org.apache.tapestry5.services.javascript.JavaScriptStackSource' qualified
>> with marker annotations [], which does not exist.
>
>
> I wrapped commons-CLI inside a service that can be contributed and used it to
> parse the command line inputs;
> the parsed options are then used to fill up a bean object that contains the
> validation annotations, that in turns is validated with the
> javax.validation.Validator as provided by the registry.
>
> The next step is to generate on-the fly the "bean" object to be validated
> with fields and relative annotations as specified in the contributions;
>
> Ideally something like this in my AppModule:
>
> contributions.add( new Option("a","a-very-important-input","This is a
> very important input", "not-null", "min-length=3", "max-length=5"));
>
> Should be used to generate on-the-fly an instance of
> ApplicationConfiguration.class
>
> that has a field
> private String __a-very-important-input;
>
> annotated with:
>
> @NotNull
> @Size( min=3, max=5)
>
>
>
> It would be great also if I can add messages to the annotations as one expect.
>
> Any suggestions on how to achieve such on the fly class creation in the scope
> of T5 ?
>
> Maybe you can point me toward a Service Impl that does a similar job ?
>
> Alternative design decision or reasons against this approach are welcome as
> well.
>
> Thanks
>
> -- Alessio
>
>
>
>
> On Sep 25, 2013, at 1:10 PM, Alessio Gambi wrote:
>
>> Hi again,
>>
>>>> However, I have the strong feeling that we (I and T5IoC) can do much more
>>>> with just a little more effort... but unfortunately I have no time to dive
>>>> into the code to find this out
>>> Don't forget that Tapestry-IoC is about IoC and I think that's the only
>>> thing it should focus.
>>
>> Yes, I meant something like: By fully exploit the power of T5-ioc I can do a
>> lot more with just little effort to do it
>>
>>
>>>> ================================================================================
>>>>
>>>> The Problem
>>>> ========
>>>>
>>>> The non web application under design receives command-line arguments as
>>>> inputs.
>>>> Some of them are optional, some of them have constrained values (i.e.,
>>>> ranges), and some of them must have specific values.
>>>
>>> I agree with Martin Kersten: this is an interface problem, not an IoC one.
>>> Command-line is just another interface between your code and everything
>>> outside it, be it users, other code or other machines.
>>> That shouldn't be part of Tapestry-IoC.
>>
>> I have been misunderstood, sorry for not explaining myself properly... Let
>> me try again:
>>
>> I do not want to extend T5-IoC with a validation, I just want to use its
>> power (as well as the available T5 modules) to create a new module that I
>> can use to
>> validate the inputs my application receive via command-line.
>>
>> To do so I would like to leverage the validation framework(s) that are
>> already in place.
>>
>> Now the question (or dilemma) is: how to best approach this problems ?
>>
>> Using the T5-Web validation is apparently not applicable or not really clean
>> because in my case I don't need to for example "render" validators,
>> bubble/intercept events, and so on.
>> As something simple like inject a "validator" and invoking validate() should
>> be ok in my case.
>>
>> Here we come to Thiago's suggestions:
>>> For the validation, why don't you use Bean Validation aka JSR 303?
>>
>> I never used it before, so I gave it a try... with no luck so far :(
>>
>> These are a list of things I did:
>>
>> - To "enable" the validation I created an ApplicationConfigurationBean and
>> annotate its fields with the required annotations. (My hope is to have a
>> factory that create this bean from the CLI specs/contributions)
>>
>> - Then I tried to use plain "Bean Validation aka JSR 303", and failed
>> because there are incompatible library versions for logging ( -.-' ) (I
>> cannot change the exclusion/inclusion in maven because some lib require some
>> version while the validators one require some others)
>>
>> - So I resort to use the tapestry-beanvalidation module:
>> - I added the dependency in my pom
>> (<dependency><groupId>org.apache.tapestry</groupId><artifactId>tapestry-beanvalidator</artifactId><version>5.3.7</version></dependency>)
>> - Declare: BeanValidatorModule.class as Submodule of my application
>> module
>> - Try to build the registry for eventually getting an instance of
>> Validator like this: javax.validation.Validator validator =
>> registry.getService(Validator.class);
>>
>> During the registry build I got (intermittently) one of the following
>> exceptions (I am using JUnit to rung the test):
>>
>> java.lang.IllegalArgumentException: Contribution
>> org.apache.tapestry5.beanvalidator.BeanValidatorModule.addWorker(OrderedConfiguration)
>> (at BeanValidatorModule.java:111) is for service 'interface
>> org.apache.tapestry5.services.transform.ComponentClassTransformWorker2'
>> qualified with marker annotations [interface
>> org.apache.tapestry5.ioc.annotations.Primary], which does not exist.
>> at
>> org.apache.tapestry5.ioc.internal.RegistryImpl.validateContributeDefs(RegistryImpl.java:246)
>> at
>> org.apache.tapestry5.ioc.internal.RegistryImpl.<init>(RegistryImpl.java:205)
>> at
>> org.apache.tapestry5.ioc.RegistryBuilder.build(RegistryBuilder.java:177)
>> at
>> at.ac.tuwien.iter.validation.JSR303Test.setupValidation(JSR303Test.java:64)
>> << My Test Class
>>
>> --
>>
>> java.lang.IllegalArgumentException: Contribution
>> org.apache.tapestry5.beanvalidator.BeanValidatorModule.addBeanValidationStack(MappedConfiguration)
>> (at BeanValidatorModule.java:104) is for service 'interface
>> org.apache.tapestry5.services.javascript.JavaScriptStackSource' qualified
>> with marker annotations [], which does not exist.
>> at
>> org.apache.tapestry5.ioc.internal.RegistryImpl.validateContributeDefs(RegistryImpl.java:246)
>> at
>> org.apache.tapestry5.ioc.internal.RegistryImpl.<init>(RegistryImpl.java:205)
>> at
>> org.apache.tapestry5.ioc.RegistryBuilder.build(RegistryBuilder.java:177)
>> at
>> at.ac.tuwien.iter.validation.JSR303Test.setupValidation(JSR303Test.java:64)
>> << My Test Class
>>
>> ===============
>>
>> Is there something obviously wrong that I am doing here ? Maybe some missing
>> library/dep , a configuration file, or something else ?
>>
>>
>>
>>
>> BTW, I am using T5 version 5.3.7
>> (<dependency><groupId>org.apache.tapestry</groupId><artifactId>tapestry-ioc</artifactId><version>5.3.7</version></dependency>)
>>
>> Just for the record, I also tried what's described by Taha in his blog
>> (http://tawus.wordpress.com/2011/05/12/tapestry-magic-12-tapestry-ioc-aware-jsr-303-custom-validators/)
>> again with no luck.
>>
>> A small remark/suggestion ... when you publish code - which is a great thing
>> - please remember to put also references for any configuration or dependency
>> that must be met to properly run it.
>> In my case for example, it is not clear what I need to put in my pom
>> (validation dep ? hibernat implementation of the validation API ? both ?
>> tapestry-hibernate (which version) ? tapestry-beanvalidator ? etc.
>>
>> Many thanks for the great work !
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]