Search in sources :

Example 1 with GenericBootstrap

use of javax.validation.bootstrap.GenericBootstrap in project camel by apache.

the class ValidatorFactories method buildValidatorFactory.

public static ValidatorFactory buildValidatorFactory(boolean osgi, ValidationProviderResolver validationProviderResolver, MessageInterpolator messageInterpolator, TraversableResolver traversableResolver, ConstraintValidatorFactory constraintValidatorFactory) {
    ValidationProviderResolver resolvedValidationProviderResolver = resolveValidationProviderResolver(osgi, validationProviderResolver);
    GenericBootstrap bootstrap = Validation.byDefaultProvider();
    if (resolvedValidationProviderResolver != null) {
        bootstrap.providerResolver(resolvedValidationProviderResolver);
    }
    Configuration<?> configuration = bootstrap.configure();
    if (messageInterpolator != null) {
        configuration.messageInterpolator(messageInterpolator);
    }
    if (traversableResolver != null) {
        configuration.traversableResolver(traversableResolver);
    }
    if (constraintValidatorFactory != null) {
        configuration.constraintValidatorFactory(constraintValidatorFactory);
    }
    return configuration.buildValidatorFactory();
}
Also used : ValidationProviderResolver(javax.validation.ValidationProviderResolver) GenericBootstrap(javax.validation.bootstrap.GenericBootstrap)

Example 2 with GenericBootstrap

use of javax.validation.bootstrap.GenericBootstrap in project Payara by payara.

the class BVIntegrationTestServlet method configureValidation.

private Validator configureValidation(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    PrintWriter out = resp.getWriter();
    GenericBootstrap bootstrap = Validation.byDefaultProvider();
    Configuration config = bootstrap.configure();
    InputStream mappingsXml = new ByteArrayInputStream(validationXml.getBytes());
    config.addMapping(mappingsXml);
    ValidatorFactory factory = config.buildValidatorFactory();
    ValidatorContext validatorContext = factory.usingContext();
    Validator validator = validatorContext.getValidator();
    if (validator == null) {
        factory = Validation.byDefaultProvider().configure().buildValidatorFactory();
        validator = factory.getValidator();
    }
    out.print("<p>");
    out.print("Obtained ValidatorFactory: " + factory + ".");
    out.print("</p>");
    return validator;
}
Also used : Configuration(javax.validation.Configuration) ValidatorFactory(javax.validation.ValidatorFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) ValidatorContext(javax.validation.ValidatorContext) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) GenericBootstrap(javax.validation.bootstrap.GenericBootstrap) Validator(javax.validation.Validator) PrintWriter(java.io.PrintWriter)

Example 3 with GenericBootstrap

use of javax.validation.bootstrap.GenericBootstrap in project Payara by payara.

the class ConnectorDeployer method registerBeanValidator.

private void registerBeanValidator(String rarName, ReadableArchive archive, ClassLoader classLoader) {
    ClassLoader contextCL = null;
    try {
        contextCL = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(classLoader);
        Validator beanValidator = null;
        ValidatorFactory validatorFactory = null;
        try {
            List<String> mappingsList = getValidationMappingDescriptors(archive);
            if (mappingsList.size() > 0) {
                GenericBootstrap bootstrap = Validation.byDefaultProvider();
                Configuration config = bootstrap.configure();
                InputStream inputStream = null;
                try {
                    for (String fileName : mappingsList) {
                        inputStream = archive.getEntry(fileName);
                        config.addMapping(inputStream);
                    }
                    validatorFactory = config.buildValidatorFactory();
                    ValidatorContext validatorContext = validatorFactory.usingContext();
                    beanValidator = validatorContext.getValidator();
                } catch (IOException e) {
                    if (_logger.isLoggable(Level.FINE)) {
                        _logger.log(Level.FINE, "Exception while processing xml files for detecting " + "bean-validation-mapping", e);
                    }
                } finally {
                    try {
                        if (inputStream != null) {
                            inputStream.close();
                        }
                    } catch (Exception e) {
                    // ignore ?
                    }
                }
            }
        } catch (Exception e) {
            Object[] params = new Object[] { rarName, e };
            _logger.log(Level.WARNING, "error.processing.xml.for.bean.validation.mapping", params);
        }
        if (beanValidator == null) {
            validatorFactory = Validation.byDefaultProvider().configure().buildValidatorFactory();
            beanValidator = validatorFactory.getValidator();
        }
        ConnectorRegistry registry = ConnectorRegistry.getInstance();
        registry.addBeanValidator(rarName, beanValidator);
    } finally {
        Thread.currentThread().setContextClassLoader(contextCL);
    }
}
Also used : DelegatingClassLoader(org.glassfish.internal.api.DelegatingClassLoader) GenericBootstrap(javax.validation.bootstrap.GenericBootstrap) ConnectorRegistry(com.sun.enterprise.connectors.ConnectorRegistry) PropertyVetoException(java.beans.PropertyVetoException) ConnectorRuntimeException(com.sun.appserv.connectors.internal.api.ConnectorRuntimeException)

Aggregations

GenericBootstrap (javax.validation.bootstrap.GenericBootstrap)3 ConnectorRuntimeException (com.sun.appserv.connectors.internal.api.ConnectorRuntimeException)1 ConnectorRegistry (com.sun.enterprise.connectors.ConnectorRegistry)1 PropertyVetoException (java.beans.PropertyVetoException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 PrintWriter (java.io.PrintWriter)1 Configuration (javax.validation.Configuration)1 ValidationProviderResolver (javax.validation.ValidationProviderResolver)1 Validator (javax.validation.Validator)1 ValidatorContext (javax.validation.ValidatorContext)1 ValidatorFactory (javax.validation.ValidatorFactory)1 DelegatingClassLoader (org.glassfish.internal.api.DelegatingClassLoader)1