Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 2 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: 5
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: 364
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  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 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.