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