Search in sources :

Example 1 with ScriptEvaluatorFactory

use of org.hibernate.validator.spi.scripting.ScriptEvaluatorFactory in project hibernate-validator by hibernate.

the class ValidatorFactoryConfigurationHelper method determineScriptEvaluatorFactory.

static ScriptEvaluatorFactory determineScriptEvaluatorFactory(ConfigurationState configurationState, Map<String, String> properties, ClassLoader externalClassLoader) {
    if (configurationState instanceof AbstractConfigurationImpl) {
        AbstractConfigurationImpl<?> hibernateSpecificConfig = (AbstractConfigurationImpl<?>) configurationState;
        if (hibernateSpecificConfig.getScriptEvaluatorFactory() != null) {
            LOG.usingScriptEvaluatorFactory(hibernateSpecificConfig.getScriptEvaluatorFactory().getClass());
            return hibernateSpecificConfig.getScriptEvaluatorFactory();
        }
    }
    String scriptEvaluatorFactoryFqcn = properties.get(HibernateValidatorConfiguration.SCRIPT_EVALUATOR_FACTORY_CLASSNAME);
    if (scriptEvaluatorFactoryFqcn != null) {
        try {
            @SuppressWarnings("unchecked") Class<? extends ScriptEvaluatorFactory> clazz = (Class<? extends ScriptEvaluatorFactory>) run(LoadClass.action(scriptEvaluatorFactoryFqcn, externalClassLoader));
            ScriptEvaluatorFactory scriptEvaluatorFactory = run(NewInstance.action(clazz, "script evaluator factory class"));
            LOG.usingScriptEvaluatorFactory(clazz);
            return scriptEvaluatorFactory;
        } catch (Exception e) {
            throw LOG.getUnableToInstantiateScriptEvaluatorFactoryClassException(scriptEvaluatorFactoryFqcn, e);
        }
    }
    return new DefaultScriptEvaluatorFactory(externalClassLoader);
}
Also used : DefaultScriptEvaluatorFactory(org.hibernate.validator.internal.engine.scripting.DefaultScriptEvaluatorFactory) LoadClass(org.hibernate.validator.internal.util.privilegedactions.LoadClass) DefaultScriptEvaluatorFactory(org.hibernate.validator.internal.engine.scripting.DefaultScriptEvaluatorFactory) ScriptEvaluatorFactory(org.hibernate.validator.spi.scripting.ScriptEvaluatorFactory)

Example 2 with ScriptEvaluatorFactory

use of org.hibernate.validator.spi.scripting.ScriptEvaluatorFactory in project hibernate-validator by hibernate.

the class ConstraintValidatorManagerTest method testValidatorsAreCachedPerConstraintAndAnnotationMembersAndScriptEvaluatorFactory.

@Test
@TestForIssue(jiraKey = "HV-1589")
public void testValidatorsAreCachedPerConstraintAndAnnotationMembersAndScriptEvaluatorFactory() {
    Validator validator = getConfiguration().addMapping(ConstraintValidatorManagerTest.class.getResourceAsStream("hv-1589-mapping.xml")).buildValidatorFactory().getValidator();
    ConstraintDescriptorImpl<?> sizeOnMiddleNameDescriptor = getSingleConstraintDescriptorForProperty(validator, User.class, "middleName");
    ConstraintDescriptorImpl<?> sizeOnAddress1Descriptor = getSingleConstraintDescriptorForProperty(validator, User.class, "address1");
    ConstraintDescriptorImpl<?> sizeOnAddress2Descriptor = getSingleConstraintDescriptorForProperty(validator, User.class, "address2");
    ScriptEvaluatorFactory scriptEvaluatorFactory1 = new DefaultScriptEvaluatorFactory(null);
    ScriptEvaluatorFactory scriptEvaluatorFactory2 = new DefaultScriptEvaluatorFactory(null);
    HibernateConstraintValidatorInitializationContext initializationContext1 = getConstraintValidatorInitializationContext(scriptEvaluatorFactory1, DefaultClockProvider.INSTANCE, Duration.ZERO);
    HibernateConstraintValidatorInitializationContext initializationContext2 = getConstraintValidatorInitializationContext(scriptEvaluatorFactory2, DefaultClockProvider.INSTANCE, Duration.ZERO);
    ConstraintValidator<?, ?> sizeValidatorForMiddleNameCtx1 = constraintValidatorManager.getInitializedValidator(String.class, sizeOnMiddleNameDescriptor, constraintValidatorFactory, initializationContext1);
    ConstraintValidator<?, ?> sizeValidatorForAddress1Ctx1 = constraintValidatorManager.getInitializedValidator(String.class, sizeOnAddress1Descriptor, constraintValidatorFactory, initializationContext1);
    ConstraintValidator<?, ?> sizeValidatorForAddress2Ctx1 = constraintValidatorManager.getInitializedValidator(String.class, sizeOnAddress2Descriptor, constraintValidatorFactory, initializationContext1);
    ConstraintValidator<?, ?> sizeValidatorForAddress2Ctx2 = constraintValidatorManager.getInitializedValidator(String.class, sizeOnAddress2Descriptor, constraintValidatorFactory, initializationContext2);
    ConstraintValidator<?, ?> sizeValidatorForAddress1Ctx2 = constraintValidatorManager.getInitializedValidator(String.class, sizeOnAddress1Descriptor, constraintValidatorFactory, initializationContext2);
    assertThat(sizeValidatorForMiddleNameCtx1).isNotSameAs(sizeValidatorForAddress1Ctx1);
    assertThat(sizeValidatorForAddress1Ctx1).isSameAs(sizeValidatorForAddress2Ctx1);
    assertThat(sizeValidatorForAddress1Ctx1).isNotSameAs(sizeValidatorForAddress2Ctx2);
    assertThat(sizeValidatorForAddress2Ctx2).isSameAs(sizeValidatorForAddress1Ctx2);
}
Also used : DefaultScriptEvaluatorFactory(org.hibernate.validator.internal.engine.scripting.DefaultScriptEvaluatorFactory) HibernateConstraintValidatorInitializationContext(org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorInitializationContext) DefaultScriptEvaluatorFactory(org.hibernate.validator.internal.engine.scripting.DefaultScriptEvaluatorFactory) ScriptEvaluatorFactory(org.hibernate.validator.spi.scripting.ScriptEvaluatorFactory) Validator(jakarta.validation.Validator) ConstraintValidator(jakarta.validation.ConstraintValidator) ValidatorUtil.getValidator(org.hibernate.validator.testutils.ValidatorUtil.getValidator) NotNullValidator(org.hibernate.validator.internal.constraintvalidators.bv.NotNullValidator) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 3 with ScriptEvaluatorFactory

use of org.hibernate.validator.spi.scripting.ScriptEvaluatorFactory in project hibernate-validator by hibernate.

the class ConstraintValidatorManagerTest method testValidatorsAreCachedPerConstraintAndAnnotationMembersAndTemporalValidationTolerance.

@Test
@TestForIssue(jiraKey = "HV-1589")
public void testValidatorsAreCachedPerConstraintAndAnnotationMembersAndTemporalValidationTolerance() {
    Validator validator = getConfiguration().addMapping(ConstraintValidatorManagerTest.class.getResourceAsStream("hv-1589-mapping.xml")).buildValidatorFactory().getValidator();
    ConstraintDescriptorImpl<?> sizeOnMiddleNameDescriptor = getSingleConstraintDescriptorForProperty(validator, User.class, "middleName");
    ConstraintDescriptorImpl<?> sizeOnAddress1Descriptor = getSingleConstraintDescriptorForProperty(validator, User.class, "address1");
    ConstraintDescriptorImpl<?> sizeOnAddress2Descriptor = getSingleConstraintDescriptorForProperty(validator, User.class, "address2");
    ScriptEvaluatorFactory scriptEvaluatorFactory = new DefaultScriptEvaluatorFactory(null);
    HibernateConstraintValidatorInitializationContext initializationContext1 = getConstraintValidatorInitializationContext(scriptEvaluatorFactory, DefaultClockProvider.INSTANCE, Duration.ofDays(1));
    HibernateConstraintValidatorInitializationContext initializationContext2 = getConstraintValidatorInitializationContext(scriptEvaluatorFactory, DefaultClockProvider.INSTANCE, Duration.ofDays(999));
    ConstraintValidator<?, ?> sizeValidatorForMiddleNameCtx1 = constraintValidatorManager.getInitializedValidator(String.class, sizeOnMiddleNameDescriptor, constraintValidatorFactory, initializationContext1);
    ConstraintValidator<?, ?> sizeValidatorForAddress1Ctx1 = constraintValidatorManager.getInitializedValidator(String.class, sizeOnAddress1Descriptor, constraintValidatorFactory, initializationContext1);
    ConstraintValidator<?, ?> sizeValidatorForAddress2Ctx1 = constraintValidatorManager.getInitializedValidator(String.class, sizeOnAddress2Descriptor, constraintValidatorFactory, initializationContext1);
    ConstraintValidator<?, ?> sizeValidatorForAddress2Ctx2 = constraintValidatorManager.getInitializedValidator(String.class, sizeOnAddress2Descriptor, constraintValidatorFactory, initializationContext2);
    ConstraintValidator<?, ?> sizeValidatorForAddress1Ctx2 = constraintValidatorManager.getInitializedValidator(String.class, sizeOnAddress1Descriptor, constraintValidatorFactory, initializationContext2);
    assertThat(sizeValidatorForMiddleNameCtx1).isNotSameAs(sizeValidatorForAddress1Ctx1);
    assertThat(sizeValidatorForAddress1Ctx1).isSameAs(sizeValidatorForAddress2Ctx1);
    assertThat(sizeValidatorForAddress1Ctx1).isNotSameAs(sizeValidatorForAddress2Ctx2);
    assertThat(sizeValidatorForAddress2Ctx2).isSameAs(sizeValidatorForAddress1Ctx2);
}
Also used : DefaultScriptEvaluatorFactory(org.hibernate.validator.internal.engine.scripting.DefaultScriptEvaluatorFactory) HibernateConstraintValidatorInitializationContext(org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorInitializationContext) DefaultScriptEvaluatorFactory(org.hibernate.validator.internal.engine.scripting.DefaultScriptEvaluatorFactory) ScriptEvaluatorFactory(org.hibernate.validator.spi.scripting.ScriptEvaluatorFactory) Validator(jakarta.validation.Validator) ConstraintValidator(jakarta.validation.ConstraintValidator) ValidatorUtil.getValidator(org.hibernate.validator.testutils.ValidatorUtil.getValidator) NotNullValidator(org.hibernate.validator.internal.constraintvalidators.bv.NotNullValidator) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 4 with ScriptEvaluatorFactory

use of org.hibernate.validator.spi.scripting.ScriptEvaluatorFactory in project hibernate-validator by hibernate.

the class ConstraintValidatorManagerTest method testValidatorsAreCachedPerConstraintAndAnnotationMembersAndClockProvider.

@Test
@TestForIssue(jiraKey = "HV-1589")
public void testValidatorsAreCachedPerConstraintAndAnnotationMembersAndClockProvider() {
    Validator validator = getConfiguration().addMapping(ConstraintValidatorManagerTest.class.getResourceAsStream("hv-1589-mapping.xml")).buildValidatorFactory().getValidator();
    ConstraintDescriptorImpl<?> sizeOnMiddleNameDescriptor = getSingleConstraintDescriptorForProperty(validator, User.class, "middleName");
    ConstraintDescriptorImpl<?> sizeOnAddress1Descriptor = getSingleConstraintDescriptorForProperty(validator, User.class, "address1");
    ConstraintDescriptorImpl<?> sizeOnAddress2Descriptor = getSingleConstraintDescriptorForProperty(validator, User.class, "address2");
    ScriptEvaluatorFactory scriptEvaluatorFactory = new DefaultScriptEvaluatorFactory(null);
    ClockProvider clockProvider1 = new ClockProvider() {

        @Override
        public Clock getClock() {
            return null;
        }
    };
    ClockProvider clockProvider2 = new ClockProvider() {

        @Override
        public Clock getClock() {
            return null;
        }
    };
    HibernateConstraintValidatorInitializationContext initializationContext1 = getConstraintValidatorInitializationContext(scriptEvaluatorFactory, clockProvider1, Duration.ZERO);
    HibernateConstraintValidatorInitializationContext initializationContext2 = getConstraintValidatorInitializationContext(scriptEvaluatorFactory, clockProvider2, Duration.ZERO);
    ConstraintValidator<?, ?> sizeValidatorForMiddleNameCtx1 = constraintValidatorManager.getInitializedValidator(String.class, sizeOnMiddleNameDescriptor, constraintValidatorFactory, initializationContext1);
    ConstraintValidator<?, ?> sizeValidatorForAddress1Ctx1 = constraintValidatorManager.getInitializedValidator(String.class, sizeOnAddress1Descriptor, constraintValidatorFactory, initializationContext1);
    ConstraintValidator<?, ?> sizeValidatorForAddress2Ctx1 = constraintValidatorManager.getInitializedValidator(String.class, sizeOnAddress2Descriptor, constraintValidatorFactory, initializationContext1);
    ConstraintValidator<?, ?> sizeValidatorForAddress2Ctx2 = constraintValidatorManager.getInitializedValidator(String.class, sizeOnAddress2Descriptor, constraintValidatorFactory, initializationContext2);
    ConstraintValidator<?, ?> sizeValidatorForAddress1Ctx2 = constraintValidatorManager.getInitializedValidator(String.class, sizeOnAddress1Descriptor, constraintValidatorFactory, initializationContext2);
    assertThat(sizeValidatorForMiddleNameCtx1).isNotSameAs(sizeValidatorForAddress1Ctx1);
    assertThat(sizeValidatorForAddress1Ctx1).isSameAs(sizeValidatorForAddress2Ctx1);
    assertThat(sizeValidatorForAddress1Ctx1).isNotSameAs(sizeValidatorForAddress2Ctx2);
    assertThat(sizeValidatorForAddress2Ctx2).isSameAs(sizeValidatorForAddress1Ctx2);
}
Also used : DefaultScriptEvaluatorFactory(org.hibernate.validator.internal.engine.scripting.DefaultScriptEvaluatorFactory) HibernateConstraintValidatorInitializationContext(org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorInitializationContext) ClockProvider(jakarta.validation.ClockProvider) DefaultClockProvider(org.hibernate.validator.internal.engine.DefaultClockProvider) DefaultScriptEvaluatorFactory(org.hibernate.validator.internal.engine.scripting.DefaultScriptEvaluatorFactory) ScriptEvaluatorFactory(org.hibernate.validator.spi.scripting.ScriptEvaluatorFactory) Validator(jakarta.validation.Validator) ConstraintValidator(jakarta.validation.ConstraintValidator) ValidatorUtil.getValidator(org.hibernate.validator.testutils.ValidatorUtil.getValidator) NotNullValidator(org.hibernate.validator.internal.constraintvalidators.bv.NotNullValidator) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 5 with ScriptEvaluatorFactory

use of org.hibernate.validator.spi.scripting.ScriptEvaluatorFactory in project quarkus by quarkusio.

the class HibernateValidatorRecorder method initializeValidatorFactory.

public BeanContainerListener initializeValidatorFactory(Set<Class<?>> classesToBeValidated, Set<String> detectedBuiltinConstraints, boolean hasXmlConfiguration, boolean jpaInClasspath, ShutdownContext shutdownContext, LocalesBuildTimeConfig localesBuildTimeConfig, HibernateValidatorBuildTimeConfig hibernateValidatorBuildTimeConfig) {
    BeanContainerListener beanContainerListener = new BeanContainerListener() {

        @Override
        public void created(BeanContainer container) {
            PredefinedScopeHibernateValidatorConfiguration configuration = Validation.byProvider(PredefinedScopeHibernateValidator.class).configure();
            if (!hasXmlConfiguration) {
                configuration.ignoreXmlConfiguration();
            }
            LocaleResolver localeResolver = null;
            InstanceHandle<LocaleResolver> configuredLocaleResolver = Arc.container().instance("locale-resolver-wrapper");
            if (configuredLocaleResolver.isAvailable()) {
                localeResolver = configuredLocaleResolver.get();
                configuration.localeResolver(localeResolver);
            }
            configuration.builtinConstraints(detectedBuiltinConstraints).initializeBeanMetaData(classesToBeValidated).locales(localesBuildTimeConfig.locales).defaultLocale(localesBuildTimeConfig.defaultLocale).beanMetaDataClassNormalizer(new ArcProxyBeanMetaDataClassNormalizer());
            if (hibernateValidatorBuildTimeConfig.expressionLanguage.constraintExpressionFeatureLevel.isPresent()) {
                configuration.constraintExpressionLanguageFeatureLevel(hibernateValidatorBuildTimeConfig.expressionLanguage.constraintExpressionFeatureLevel.get());
            }
            InstanceHandle<ConstraintValidatorFactory> configuredConstraintValidatorFactory = Arc.container().instance(ConstraintValidatorFactory.class);
            if (configuredConstraintValidatorFactory.isAvailable()) {
                configuration.constraintValidatorFactory(configuredConstraintValidatorFactory.get());
            } else {
                configuration.constraintValidatorFactory(new ArcConstraintValidatorFactoryImpl());
            }
            InstanceHandle<MessageInterpolator> configuredMessageInterpolator = Arc.container().instance(MessageInterpolator.class);
            if (configuredMessageInterpolator.isAvailable()) {
                configuration.messageInterpolator(configuredMessageInterpolator.get());
            }
            InstanceHandle<TraversableResolver> configuredTraversableResolver = Arc.container().instance(TraversableResolver.class);
            if (configuredTraversableResolver.isAvailable()) {
                configuration.traversableResolver(configuredTraversableResolver.get());
            } else {
                // we still define the one we want to use so that we do not rely on runtime automatic detection
                if (jpaInClasspath) {
                    configuration.traversableResolver(new JPATraversableResolver());
                } else {
                    configuration.traversableResolver(new TraverseAllTraversableResolver());
                }
            }
            InstanceHandle<ParameterNameProvider> configuredParameterNameProvider = Arc.container().instance(ParameterNameProvider.class);
            if (configuredParameterNameProvider.isAvailable()) {
                configuration.parameterNameProvider(configuredParameterNameProvider.get());
            }
            InstanceHandle<ClockProvider> configuredClockProvider = Arc.container().instance(ClockProvider.class);
            if (configuredClockProvider.isAvailable()) {
                configuration.clockProvider(configuredClockProvider.get());
            }
            // Hibernate Validator-specific configuration
            configuration.failFast(hibernateValidatorBuildTimeConfig.failFast);
            configuration.allowOverridingMethodAlterParameterConstraint(hibernateValidatorBuildTimeConfig.methodValidation.allowOverridingParameterConstraints);
            configuration.allowParallelMethodsDefineParameterConstraints(hibernateValidatorBuildTimeConfig.methodValidation.allowParameterConstraintsOnParallelMethods);
            configuration.allowMultipleCascadedValidationOnReturnValues(hibernateValidatorBuildTimeConfig.methodValidation.allowMultipleCascadedValidationOnReturnValues);
            InstanceHandle<ScriptEvaluatorFactory> configuredScriptEvaluatorFactory = Arc.container().instance(ScriptEvaluatorFactory.class);
            if (configuredScriptEvaluatorFactory.isAvailable()) {
                configuration.scriptEvaluatorFactory(configuredScriptEvaluatorFactory.get());
            }
            InstanceHandle<GetterPropertySelectionStrategy> configuredGetterPropertySelectionStrategy = Arc.container().instance(GetterPropertySelectionStrategy.class);
            if (configuredGetterPropertySelectionStrategy.isAvailable()) {
                configuration.getterPropertySelectionStrategy(configuredGetterPropertySelectionStrategy.get());
            }
            InstanceHandle<PropertyNodeNameProvider> configuredPropertyNodeNameProvider = Arc.container().instance(PropertyNodeNameProvider.class);
            if (configuredPropertyNodeNameProvider.isAvailable()) {
                configuration.propertyNodeNameProvider(configuredPropertyNodeNameProvider.get());
            }
            // Automatically add all the values extractors declared as beans
            for (ValueExtractor<?> valueExtractor : Arc.container().beanManager().createInstance().select(TYPE_LITERAL_VALUE_EXTRACTOR_WITH_WILDCARD)) {
                configuration.addValueExtractor(valueExtractor);
            }
            ValidatorFactory validatorFactory = configuration.buildValidatorFactory();
            ValidatorHolder.initialize(validatorFactory);
            // Close the ValidatorFactory on shutdown
            shutdownContext.addShutdownTask(new Runnable() {

                @Override
                public void run() {
                    validatorFactory.close();
                }
            });
        }
    };
    return beanContainerListener;
}
Also used : ConstraintValidatorFactory(javax.validation.ConstraintValidatorFactory) GetterPropertySelectionStrategy(org.hibernate.validator.spi.properties.GetterPropertySelectionStrategy) BeanContainer(io.quarkus.arc.runtime.BeanContainer) JPATraversableResolver(org.hibernate.validator.internal.engine.resolver.JPATraversableResolver) ClockProvider(javax.validation.ClockProvider) PredefinedScopeHibernateValidator(org.hibernate.validator.PredefinedScopeHibernateValidator) ScriptEvaluatorFactory(org.hibernate.validator.spi.scripting.ScriptEvaluatorFactory) PropertyNodeNameProvider(org.hibernate.validator.spi.nodenameprovider.PropertyNodeNameProvider) LocaleResolver(org.hibernate.validator.spi.messageinterpolation.LocaleResolver) ValidatorFactory(javax.validation.ValidatorFactory) ConstraintValidatorFactory(javax.validation.ConstraintValidatorFactory) JPATraversableResolver(org.hibernate.validator.internal.engine.resolver.JPATraversableResolver) TraversableResolver(javax.validation.TraversableResolver) ParameterNameProvider(javax.validation.ParameterNameProvider) BeanContainerListener(io.quarkus.arc.runtime.BeanContainerListener) PredefinedScopeHibernateValidatorConfiguration(org.hibernate.validator.PredefinedScopeHibernateValidatorConfiguration) MessageInterpolator(javax.validation.MessageInterpolator)

Aggregations

ScriptEvaluatorFactory (org.hibernate.validator.spi.scripting.ScriptEvaluatorFactory)5 DefaultScriptEvaluatorFactory (org.hibernate.validator.internal.engine.scripting.DefaultScriptEvaluatorFactory)4 ConstraintValidator (jakarta.validation.ConstraintValidator)3 Validator (jakarta.validation.Validator)3 HibernateConstraintValidatorInitializationContext (org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorInitializationContext)3 NotNullValidator (org.hibernate.validator.internal.constraintvalidators.bv.NotNullValidator)3 TestForIssue (org.hibernate.validator.testutil.TestForIssue)3 ValidatorUtil.getValidator (org.hibernate.validator.testutils.ValidatorUtil.getValidator)3 Test (org.testng.annotations.Test)3 BeanContainer (io.quarkus.arc.runtime.BeanContainer)1 BeanContainerListener (io.quarkus.arc.runtime.BeanContainerListener)1 ClockProvider (jakarta.validation.ClockProvider)1 ClockProvider (javax.validation.ClockProvider)1 ConstraintValidatorFactory (javax.validation.ConstraintValidatorFactory)1 MessageInterpolator (javax.validation.MessageInterpolator)1 ParameterNameProvider (javax.validation.ParameterNameProvider)1 TraversableResolver (javax.validation.TraversableResolver)1 ValidatorFactory (javax.validation.ValidatorFactory)1 PredefinedScopeHibernateValidator (org.hibernate.validator.PredefinedScopeHibernateValidator)1 PredefinedScopeHibernateValidatorConfiguration (org.hibernate.validator.PredefinedScopeHibernateValidatorConfiguration)1