Java Code Examples for javax.validation.Configuration#buildValidatorFactory()

The following examples show how to use javax.validation.Configuration#buildValidatorFactory() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ValidationManager.java    From seed with Mozilla Public License 2.0 6 votes vote down vote up
public ValidatorFactory createValidatorFactory(Consumer<Configuration> customizer) {
    boolean skipAutoconfig = "false".equalsIgnoreCase(System.getProperty(SEEDSTACK_VALIDATION_AUTOCONFIG, "true"));
    boolean hasXmlConfiguration = findMostCompleteClassLoader(ValidationManager.class)
            .getResource(VALIDATION_XML_FILE) != null;
    if (validationLevel == ValidationLevel.NONE) {
        throw SeedException.createNew(CoreErrorCode.UNABLE_TO_CREATE_VALIDATOR_FACTORY);
    } else {
        try {
            Configuration<?> configuration = Validation.byDefaultProvider().configure();
            if (!hasXmlConfiguration && !skipAutoconfig) {
                Classes.optional("org.seedstack.seed.core.internal.validation.ReflectionParameterNameProvider")
                        .map(Classes::instantiateDefault)
                        .ifPresent(c -> configuration.parameterNameProvider((ParameterNameProvider) c));
                Classes.optional("org.seedstack.seed.core.internal.validation.HibernateMessageInterpolator")
                        .map(Classes::instantiateDefault)
                        .ifPresent(c -> configuration.messageInterpolator((MessageInterpolator) c));
            }
            if (customizer != null) {
                customizer.accept(configuration);
            }
            return configuration.buildValidatorFactory();
        } catch (Exception e) {
            throw SeedException.wrap(e, CoreErrorCode.UNABLE_TO_CREATE_VALIDATOR_FACTORY);
        }
    }
}
 
Example 2
Source File: BeanValidationUtil.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a validator factory
 * @return The factory
 */
public static ValidatorFactory createValidatorFactory()
{
   Configuration configuration = Validation.byDefaultProvider().configure();
   Configuration<?> conf = configuration.traversableResolver(new JCATraversableResolver());

   return conf.buildValidatorFactory();
}
 
Example 3
Source File: BeanValidation.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructor
 */
public BeanValidation()
{
   Configuration configuration = Validation.byDefaultProvider().configure();
   Configuration<?> conf = configuration.traversableResolver(new JCATraversableResolver());

   validatorFactory = conf.buildValidatorFactory();
   validator = validatorFactory.getValidator();
}
 
Example 4
Source File: ValidationBundle.java    From packagedrone with Eclipse Public License 1.0 5 votes vote down vote up
private static Validator buildValidator ()
{
    final GenericBootstrap bootstrap = Validation.byDefaultProvider ();
    bootstrap.providerResolver ( tracker );

    final Configuration<?> cfg = bootstrap.configure ();
    final OsgiMessageInterpolator mi = getMessageInterpolator ();
    mi.setFallback ( cfg.getDefaultMessageInterpolator () );
    cfg.messageInterpolator ( mi );

    final ValidatorFactory factory = cfg.buildValidatorFactory ();
    return factory.getValidator ();
}
 
Example 5
Source File: BeanValidationProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
public BeanValidationProvider(ValidationConfiguration cfg) {
    try {
        Configuration<?> factoryCfg = Validation.byDefaultProvider().configure();
        initFactoryConfig(factoryCfg, cfg);
        factory = factoryCfg.buildValidatorFactory();
    } catch (final ValidationException ex) {
        LOG.severe("Bean Validation provider can not be found, no validation will be performed");
        throw ex;
    }
}
 
Example 6
Source File: BeanValidationProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
public <T extends Configuration<T>, U extends ValidationProvider<T>> BeanValidationProvider(
    ValidationProviderResolver resolver,
    Class<U> providerType,
    ValidationConfiguration cfg) {
    try {
        Configuration<?> factoryCfg = providerType != null
            ? Validation.byProvider(providerType).providerResolver(resolver).configure()
            : Validation.byDefaultProvider().providerResolver(resolver).configure();
        initFactoryConfig(factoryCfg, cfg);
        factory = factoryCfg.buildValidatorFactory();
    } catch (final ValidationException ex) {
        LOG.severe("Bean Validation provider can not be found, no validation will be performed");
        throw ex;
    }
}
 
Example 7
Source File: BeanValidationImpl.java    From ironjacamar with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Create a validator factory
 * @return The factory
 */
static ValidatorFactory createValidatorFactory()
{
   Configuration configuration = Validation.byDefaultProvider().configure();
   Configuration<?> conf = configuration.traversableResolver(new IronJacamarTraversableResolver());

   return conf.buildValidatorFactory();
}
 
Example 8
Source File: ValidationConfigurationContextResolver.java    From jaxrs-beanvalidation-javaee7 with Apache License 2.0 5 votes vote down vote up
/**
 * Get a context of type {@code GeneralValidator} that is applicable to the supplied type.
 *
 * @param type the class of object for which a context is desired
 * @return a context for the supplied type or {@code null} if a context for the supplied type is not available from
 *         this provider.
 */
@Override
public GeneralValidator getContext(Class<?> type) {
    Configuration<?> config = Validation.byDefaultProvider().configure();
    BootstrapConfiguration bootstrapConfiguration = config.getBootstrapConfiguration();

    config.messageInterpolator(new LocaleSpecificMessageInterpolator(Validation.byDefaultProvider().configure()
            .getDefaultMessageInterpolator()));
    config.parameterNameProvider(new CustomParameterNameProvider());

    return new GeneralValidatorImpl(config.buildValidatorFactory(),
            bootstrapConfiguration.isExecutableValidationEnabled(),
            bootstrapConfiguration.getDefaultValidatedExecutableTypes());
}
 
Example 9
Source File: ValidatorBuilder.java    From tomee with Apache License 2.0 5 votes vote down vote up
public static ValidatorFactory buildFactory(final ValidationInfo config, final ClassLoader classLoader) {
    ValidatorFactory factory = null;
    final Thread thread = Thread.currentThread();
    final ClassLoader oldContextLoader = thread.getContextClassLoader();
    try {
        thread.setContextClassLoader(classLoader);
        if (config == null) {
            factory = Validation.buildDefaultValidatorFactory();
        } else {
            final Configuration<?> configuration = getConfig(config);
            try {
                factory = configuration.buildValidatorFactory();
            } catch (final ValidationException ve) {
                thread.setContextClassLoader(ValidatorBuilder.class.getClassLoader());
                factory = Validation.buildDefaultValidatorFactory();
                thread.setContextClassLoader(classLoader);

                logger.warning("Unable create validator factory with config " + config
                    + " (" + ve.getMessage() + ")."
                    + " Default factory will be used.");
            }
        }
    } finally {
        thread.setContextClassLoader(oldContextLoader);
    }
    return factory;
}
 
Example 10
Source File: DefaultValidationContext.java    From sejda with GNU Affero General Public License v3.0 5 votes vote down vote up
private DefaultValidationContext() {
    Configuration<?> validationConfig = Validation.byDefaultProvider().configure();
    if (new DefaultSejdaContext().isIgnoreXmlConfiguration()) {
        validationConfig.ignoreXmlConfiguration();
    }
    ValidatorFactory factory = validationConfig.buildValidatorFactory();
    validator = factory.getValidator();
}
 
Example 11
Source File: ValidationExtension.java    From morphia with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a ValidationExtension
 *
 * @param mapper the Mapper instance to use
 */
public ValidationExtension(final Mapper mapper) {
    final Configuration<?> configuration = Validation.byDefaultProvider().configure();
    validationFactory = configuration.buildValidatorFactory();

    mapper.addInterceptor(this);
}
 
Example 12
Source File: ActionValidator.java    From lastaflute with Apache License 2.0 4 votes vote down vote up
protected ValidatorFactory buildValidatorFactory(VaConfigSetupper hibernateConfigSetupper) {
    final Configuration<?> configuration = createConfiguration();
    setupFrameworkConfiguration(configuration);
    setupYourConfiguration(configuration, hibernateConfigSetupper);
    return configuration.buildValidatorFactory();
}
 
Example 13
Source File: TestUtils.java    From sejda with GNU Affero General Public License v3.0 4 votes vote down vote up
private ValidatorHolder() {
    Configuration<?> validationConfig = Validation.byDefaultProvider().configure();
    validationConfig.ignoreXmlConfiguration();
    ValidatorFactory factory = validationConfig.buildValidatorFactory();
    validator = factory.getValidator();
}