I tend to like option 3 best at the moment. So is Pete Muir the CDI
spec lead.
Sounds good to me.
Just to be sure: so you're willing to accept that other BV artifacts
besides validators (such as message interpolators) can't be CDI
enabled if BV has to instantiate them?
Personally I'm ok with that since
* validators are the objects which are *always* instantiated by BV
(meaning we need a solution here) while the others are (currently)
only instantiated by BV when using the XML configuration but not when
using the bootstrap API.
* there are probably more custom validators out there than message
interpolators etc. and hence the benefit for using CDI might be larger
for the former
So this option surely addresses the majority of use cases. It
represents some kind of irregularity, but I personally think we can
accept that. I just want to make sure we all share the same
understanding.
And should a public demand for CDI support for the other artifacts
(when instantiated by BV) arise, we might also change to the
InstanceProvider approach in a later spec. revision rather easily.
Speaking of XML and assuming we're going for #3, I'm wondering what
should happen when a CVF is specified in validation.xml.
Intuitively I'd expect that the default validator (either retrieved
via the BV API or via CDI injection) uses the factory given in the
XML. This would mean that CDI needs some understanding of
validation.xml in order to not use its own CVF implementation when one
is given there. Letting CDI directly handle validation.xml doesn't
seem very desirable, so it might be useful to have some API in BV like
this:
Validation.byDefaultProvider().configure().isConstrainedValidatorFactoryRedefined()
Any thoughts?
--Gunnar
2012/1/6 Emmanuel Bernard <emmanuel(a)hibernate.org>:
I have updated the various options in the website
http://beanvalidation.org/proposals/BVAL-238/
I tend to like option 3 best at the moment. So is Pete Muir the CDI spec lead. I've
also verified a few things with him and left opened a few questions in the EE - CDI -BV
area.
Thanks for all this feedback.
On 3 janv. 2012, at 20:25, Emmanuel Bernard wrote:
> Hi all,
>
> First of all happy new year to everyone. I wish you the best for 2012.
>
> I have spend some time to hammer the last details of BVAL-238. I would like your
feedback on how CDI should be integrated with Bean Validation and whether or not we
facilitate other DI solutions.
>
> Below is a copy of the discussion as provided on the website at
http://beanvalidation.org/proposals/BVAL-238/ (look for "How is CDI BeanManager (or
equivalent) injected into Bean Validation?".
>
> Please provide any comment, I'd like to settle this asap and move forward.
That's the last big open question IMO before I apply these changes to the spec.
>
> Thanks
>
> Emmanuel
>
> ---
>
> ### How is CDI `BeanManager` (or equivalent) injected into Bean Validation?
>
> I'm assuming CDI exposes the ability to instantiate and destroy CDI beans via a
`BeanManager` interface.
>
> #### Option 1: Add a Method to inject the BeanManager instance on Bean Validation
bootstrap sequence
>
> One approach would be to let the container set a `BeanManager` instance
>
> ValidatorFactory factory = Validation
> .byDefaultProvider()
> .configure()
> .cdiBeanManager(beanManager)
> .buildValidatorFactory();
>
> However that would add a hard dependency between CDI and Bean Validation which is
probably not welcomed.
>
> An alternative is to use an untyped version (which should probably be favored):
>
>
> ValidatorFactory factory = Validation
> .byDefaultProvider()
> .configure()
> // T cdiBeanManager(Object beanManager) //raises an exception if
that's not a BeanManager
> .cdiBeanManager(beanManager)
> .buildValidatorFactory();
>
>
> vs
>
> ValidatorFactory factory = Validation
> .byDefaultProvider()
> .configure()
> //raises an exception if that's not a BeanManager
> .addObject(Validation.CDI_BEAN_MANAGER, beanManager) // T addObject(String
key, Objet value)
> .buildValidatorFactory();
>
> I however feel chagrined that the nicely typed `Configuration` API requires such
untyped approach.
> (I don't think introducing CdiBeanManagerFactory solves any issue, is that
true?).
>
>
> - have an untyped version of the above proposal
> - offer a generic `Map<String,Object> addObject(String key, Object value)`
method on `Configuration`
>
> #### Option 2: Use CDI facility to retrive the current `BeanManager`
>
> CDI exposes `BeanManager` via JNDI in EE, we could use it.
>
> Also CDI 1.1 offers programmatic lookup via the CDI class, see EDR1 spec for
details.
> <
http://docs.jboss.org/cdi/spec/1.1.EDR1/html/spi.html#provider>
>
> #### Option 3: Ask CDI to inject a CDI aware `ConstraintValidatorFactory` when
creating the `ValidatorFactory` object
>
> Another idea would be to integrate BV/CDI via a CDI-aware
`ConstraintValidatorFactory` to be provided by CDI runtimes:
>
> ValidatorFactory factory = Validation
> .byDefaultProvider()
> .configure()
> .constraintValidatorFactory( new CdiAwareConstraintValidatorFactory(
beanManager ) )
> .buildValidatorFactory();
>
> That way the integration is completely managed by the CDI-side. `Validator` and
`ValidatorFactory` are already
> built-in beans in CDI so this wouldn't add much complexity.
> The CDI runtime would use this factory whenever a `Validator` or `ValidatorFactory`
is retrieved.
>
> #### Option 4: Add a method accepting an `InstanceProvider` implementation in Bean
Validation's bootstrap
>
> ValidatorFactory factory = Validation
> .byDefaultProvider()
> .configure()
> .instanceProvider(cdiInstanceProvider)
> .buildValidatorFactory();
>
> public interface InstanceProvider {
> public <T> T createInstance(Class<T> type);
> public destroyInstance(Object instance);
> }
>
> The default implementation can be the no-arg constructor we have today. We can either
ask CDI to
> provide a `CDIInstanceProvider` at `ValidatorFactory` creation like in option 3 or
make it the
> default implementation if CDI is present according to option 2.
>
> This option works fine as long as we don't require more complex object creation
logic.
>
> #### Which option?
>
> Option 1 has many drawbacks and should be avoided.
>
> Option 2 is the easiest solution but puts CDI above other DI technologies. This is
not bad per se but that's a point.
>
> Option 3 is quite elegant as most of the time CDI is responsible for the lifecycle of
`ValidatorFactory` and can
> interject in the bootstrap process. When `ValidatorFactory` is created manually, we
can ask the user to use a
> provider specific `CdiAwareConstraintValidatorFactory`.
>
> My main concern with option 3 is whether or not we (will) need access to CDI's
`BeanManager` to handle the
> lifecycle of other objects in the Bean Validation universe.
> `MessageInterpolator` and `TraversableResolver` could also benefit from CDI.
> Note that these examples do not create objects, they are objects.
>
> Option 4 tries to address the shortcomings of option 3 provided we keep the simple
> object creation logic. It has my preference so far.
>
> #### How would injection of `MessageInterpolator` and `TraversableResolver` be
solved?
>
> If instances are provided, we could still let CDI do setter-style `inject()`ion.
constructor injection won't work.
> But it seems to me it's preferable to *not* do any injection on provided
instances.
>
> We should however let people provide `MessageInterpolator` and `TraversableResolver`
implementation classes
> that will be used to ask for a CDI bean instance (like `ConstraintValidator` are
resolved).
>
> Suggestions?
>
>
>
> On 24 oct. 2011, at 22:26, Gunnar Morling wrote:
>
>>> What you're saying is that assuming @ValidatorFactory is injected in EE
or SE, it is the CDI responsibility to set the right ConstraintValidatorFactory. We can
consider this approach definitely. Can you add it as an alternative in the website page?
>>
>> Sure. Can you provide me with write access for the
beanvalidation.org repo?
>>
>>> Note that CDI strategy is to let other specs describe their integration, we
are responsible for describing how that will work.
>>
>> I see. Validator and ValidatorFactory are built-in beans in CDI 1.0
>> already, so I think one could extend on that and require that the
>> runtime must use a CDI-enabled ConstraintValidatorFactory. Maybe this
>> is something which we could contribute to CDI 1.1.
>>
>> It would be interesting to know how CDI integration is solved for JPA
>> entity listeners. Do you have any information on that?
>>
>>
>> 2011/10/24 Emmanuel Bernard <emmanuel(a)hibernate.org>:
>>> There are a couple of reasons why I proposed this approach. Of course nothing
i settled.
>>>
>>> - IFAIR, BeanManager was to be responsible from the creation and destruction
of the object. We do have a hook for creation but not for destruction yet. So it's
likely that ConstraintValidatorfactory needs to be enhanced
>>> - I was trying to facilitate the work of the container or the "SE"
container.
>>>
>>> What you're saying is that assuming @ValidatorFactory is injected in EE
or SE, it is the CDI responsibility to set the right ConstraintValidatorFactory. We can
consider this approach definitely. Can you add it as an alternative in the website page?
>>>
>>> Note that CDI strategy is to let other specs describe their integration, we
are responsible for describing how that will work.
>>>
>>> Emmanuel
>>>
>>> On 22 oct. 2011, at 10:51, Gunnar Morling wrote:
>>>
>>>> Hi,
>>>>
>>>> concerning "How is CDI BeanManager (or equivalent) injected into
Bean
>>>> Validation?" I'm wondering whether we really require something
like
>>>> cdiBeanManager().
>>>>
>>>> Couldn't the CDI runtime just set a CDI-aware
>>>> ConstraintValidatorFactory when creating the Validator(Factory). We
>>>> then wouldn't have any dependency to CDI in BV at all. On the
downside
>>>> this would mean CDI-enabled validators would only work when using a
>>>> validator managed by CDI but not using a validator retrieved via
>>>> Validation.buildDefaultValidatorFactory() but that would be ok IMO.
>>>>
>>>> So generally I think the BV/CDI integration should be more
>>>> triggered/managed from the CDI side than the other way around.
>>>>
>>>> --Gunnar
>>>>
>>>>
>>>> 2011/10/21 Emmanuel Bernard <emmanuel(a)hibernate.org>:
>>>>> Hi team,
>>>>> I've been thinking about BVAL-238 and came up with a first round
of ideas and open questions.
>>>>> It is available here
http://beanvalidation.org/proposals/BVAL-238/
>>>>>
>>>>> Please give me your feedback. I think this issue can be closed quite
quickly.
>>>>>
>>>>> I've also created a proposals section on the website that will
contain such work in progress proposals before inclusion in the spec proper. Check out
http://beanvalidation.org/proposals
>>>>>
>>>>> On a side note, for casual website editing, you can use GitHub's
`Edit this file` button (see
https://github.com/beanvalidation/beanvalidation.org/blob/master/proposal... ).
It's not quite a wiki but that's pretty close. One thing you cannot do is create a
new file unfortunately. Anyone that have asked for write access should see this button.
>>>>>
>>>>> Emmanuel
>>>>> _______________________________________________
>>>>> beanvalidation-dev mailing list
>>>>> beanvalidation-dev(a)lists.jboss.org
>>>>>
https://lists.jboss.org/mailman/listinfo/beanvalidation-dev
>>>>>
>>>>
>>>> _______________________________________________
>>>> beanvalidation-dev mailing list
>>>> beanvalidation-dev(a)lists.jboss.org
>>>>
https://lists.jboss.org/mailman/listinfo/beanvalidation-dev
>>>
>>>
>>> _______________________________________________
>>> beanvalidation-dev mailing list
>>> beanvalidation-dev(a)lists.jboss.org
>>>
https://lists.jboss.org/mailman/listinfo/beanvalidation-dev
>>>
>>
>> _______________________________________________
>> beanvalidation-dev mailing list
>> beanvalidation-dev(a)lists.jboss.org
>>
https://lists.jboss.org/mailman/listinfo/beanvalidation-dev
>
>
> _______________________________________________
> beanvalidation-dev mailing list
> beanvalidation-dev(a)lists.jboss.org
>
https://lists.jboss.org/mailman/listinfo/beanvalidation-dev
_______________________________________________
beanvalidation-dev mailing list
beanvalidation-dev(a)lists.jboss.org
https://lists.jboss.org/mailman/listinfo/beanvalidation-dev