Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Using InjectingConstraintValidatorFactory
PostPosted: Wed Apr 19, 2017 11:21 am 
Newbie

Joined: Sat Mar 17, 2012 1:33 pm
Posts: 7
Hi,

In Resteasy, we call

Code:
ValidatorFactory.class.cast(context.lookup("java:comp/ValidatorFactory"));


to get a CDI aware ValidatorFactory. Recently, we have had a request to support Validation and CDI in Java SE, and I can get an instance of InjectingConstraintValidatorFactory by calling
Code:
                  HibernateValidatorConfiguration config = Validation.byProvider(HibernateValidator.class).configure();
                  ConstraintValidatorFactory cvf = null;
                  try
                  {
                     Class<?> cvfClass = config.getClass().getClassLoader().loadClass("org.hibernate.validator.internal.cdi.InjectingConstraintValidatorFactory");
                     if (cvfClass != null)
                     {
                        BeanManager beanManager = CDI.current().getBeanManager();
                        Constructor<?> constructor = cvfClass.getConstructor(BeanManager.class);
                        cvf = (ConstraintValidatorFactory) constructor.newInstance(beanManager);
                        validatorFactory = tmpValidatorFactory = config.constraintValidatorFactory(cvf).buildValidatorFactory();
                     }
                  }

Is there a more elegant, safer way of doing that, maybe by way of a different call to Validation.byProvider()?

Thanks,
Ron


Top
 Profile  
 
 Post subject: Re: Using InjectingConstraintValidatorFactory
PostPosted: Fri Apr 21, 2017 5:43 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 386
Hi Ron,

If the HV portable extension for CDI is present, you should be able to just obtain a ValidatorFactory correctly set up from the BeanManager or even the CDI class itself:

Code:
CDI.current().select(ValidatorFactory.class)


Could you give this a try?

Cheers,

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: Using InjectingConstraintValidatorFactory
PostPosted: Thu Apr 27, 2017 8:59 pm 
Newbie

Joined: Sat Mar 17, 2012 1:33 pm
Posts: 7
Hi Gunnar,

Thanks for the rapid reply. When I added
Quote:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator-cdi</artifactId>
<version>5.3.5.Final</version>
</dependency>

and tried
Code:
   Instance<ValidatorFactory> instance = CDI.current().select(ValidatorFactory.class);
   validatorFactory = tmpValidatorFactory = instance.get();

I got
Quote:
org.jboss.weld.exceptions.UnsatisfiedResolutionException: WELD-001334: Unsatisfied dependencies for type ValidatorFactory with qualifiers

Is that not what you meant?

-Ron


Top
 Profile  
 
 Post subject: Re: Using InjectingConstraintValidatorFactory
PostPosted: Fri Apr 28, 2017 3:52 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 386
Hi Ron,

Yes, that's what I had in mind indeed. Surprised that it doesn't work. Do you maybe have a GitHub branch or something like that somewhere so that we can take a look ourselves?

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: Using InjectingConstraintValidatorFactory
PostPosted: Fri Apr 28, 2017 10:45 am 
Newbie

Joined: Sat Mar 17, 2012 1:33 pm
Posts: 7
Thanks,Gunnar. I've committed the latest version to https://github.com/ronsigal/Resteasy/tree/1617. The code I've referred to is in the method getValidatorFactory() in https://github.com/ronsigal/Resteasy/bl ... olver.java.

I've been using the test class https://github.com/ronsigal/Resteasy/bl ... t1617.java to experiment with. It runs with an embedded undertow, so it doesn't need any external setup.

-Ron


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.