1 /*
2  * Hibernate Validator, declare and validate application constraints
3  *
4  * License: Apache License, Version 2.0
5  * See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6  */

7 package org.hibernate.validator.internal.engine;
8
9 import java.lang.invoke.MethodHandles;
10 import java.time.Duration;
11 import java.util.HashMap;
12 import java.util.Map;
13
14 import javax.validation.ClockProvider;
15 import javax.validation.ConstraintValidatorFactory;
16 import javax.validation.MessageInterpolator;
17 import javax.validation.ParameterNameProvider;
18 import javax.validation.TraversableResolver;
19 import javax.validation.Validator;
20 import javax.validation.valueextraction.ValueExtractor;
21
22 import org.hibernate.validator.HibernateValidatorContext;
23 import org.hibernate.validator.internal.engine.valueextraction.ValueExtractorDescriptor;
24 import org.hibernate.validator.internal.engine.valueextraction.ValueExtractorManager;
25 import org.hibernate.validator.internal.util.logging.Log;
26 import org.hibernate.validator.internal.util.logging.LoggerFactory;
27
28 /**
29  * @author Emmanuel Bernard
30  * @author Hardy Ferentschik
31  * @author Kevin Pollet &lt;kevin.pollet@serli.com&gt; (C) 2011 SERLI
32  * @author Gunnar Morling
33  * @author Chris Beckey &lt;cbeckey@paypal.com&gt;
34  */

35 public class ValidatorContextImpl implements HibernateValidatorContext {
36
37     private static final Log LOG = LoggerFactory.make( MethodHandles.lookup() );
38
39     private final ValidatorFactoryImpl validatorFactory;
40
41     private ConstraintValidatorFactory constraintValidatorFactory;
42     private final ValidatorFactoryScopedContext.Builder validatorFactoryScopedContextBuilder;
43     private final ConstraintCreationContext constraintCreationContext;
44     private final MethodValidationConfiguration.Builder methodValidationConfigurationBuilder;
45     private final Map<ValueExtractorDescriptor.Key, ValueExtractorDescriptor> valueExtractorDescriptors;
46
47     public ValidatorContextImpl(ValidatorFactoryImpl validatorFactory) {
48         this.validatorFactoryScopedContextBuilder = new ValidatorFactoryScopedContext.Builder( validatorFactory.getValidatorFactoryScopedContext() );
49         this.validatorFactory = validatorFactory;
50         this.constraintValidatorFactory = validatorFactory.getConstraintValidatorFactory();
51         this.constraintCreationContext = validatorFactory.getConstraintCreationContext();
52         this.methodValidationConfigurationBuilder = new MethodValidationConfiguration.Builder( validatorFactory.getMethodValidationConfiguration() );
53         this.valueExtractorDescriptors = new HashMap<>();
54     }
55
56     @Override
57     public HibernateValidatorContext messageInterpolator(MessageInterpolator messageInterpolator) {
58         validatorFactoryScopedContextBuilder.setMessageInterpolator( messageInterpolator );
59         return this;
60     }
61
62     @Override
63     public HibernateValidatorContext traversableResolver(TraversableResolver traversableResolver) {
64         validatorFactoryScopedContextBuilder.setTraversableResolver( traversableResolver );
65         return this;
66     }
67
68     @Override
69     public HibernateValidatorContext constraintValidatorFactory(ConstraintValidatorFactory factory) {
70         if ( factory == null ) {
71             this.constraintValidatorFactory = validatorFactory.getConstraintValidatorFactory();
72         }
73         else {
74             this.constraintValidatorFactory = factory;
75         }
76         return this;
77     }
78
79     @Override
80     public HibernateValidatorContext parameterNameProvider(ParameterNameProvider parameterNameProvider) {
81         validatorFactoryScopedContextBuilder.setParameterNameProvider( parameterNameProvider );
82         return this;
83     }
84
85     @Override
86     public HibernateValidatorContext clockProvider(ClockProvider clockProvider) {
87         validatorFactoryScopedContextBuilder.setClockProvider( clockProvider );
88         return this;
89     }
90
91     @Override
92     public HibernateValidatorContext addValueExtractor(ValueExtractor<?> extractor) {
93         ValueExtractorDescriptor descriptor = new ValueExtractorDescriptor( extractor );
94         ValueExtractorDescriptor previous = valueExtractorDescriptors.put( descriptor.getKey(), descriptor );
95
96         if ( previous != null ) {
97             throw LOG.getValueExtractorForTypeAndTypeUseAlreadyPresentException( extractor, previous.getValueExtractor() );
98         }
99
100         return this;
101     }
102
103     @Override
104     public HibernateValidatorContext failFast(boolean failFast) {
105         validatorFactoryScopedContextBuilder.setFailFast( failFast );
106         return this;
107     }
108
109     @Override
110     public HibernateValidatorContext allowOverridingMethodAlterParameterConstraint(boolean allow) {
111         methodValidationConfigurationBuilder.allowOverridingMethodAlterParameterConstraint( allow );
112         return this;
113     }
114
115     @Override
116     public HibernateValidatorContext allowMultipleCascadedValidationOnReturnValues(boolean allow) {
117         methodValidationConfigurationBuilder.allowMultipleCascadedValidationOnReturnValues( allow );
118         return this;
119     }
120
121     @Override
122     public HibernateValidatorContext allowParallelMethodsDefineParameterConstraints(boolean allow) {
123         methodValidationConfigurationBuilder.allowParallelMethodsDefineParameterConstraints( allow );
124         return this;
125     }
126
127     @Override
128     public HibernateValidatorContext enableTraversableResolverResultCache(boolean enabled) {
129         validatorFactoryScopedContextBuilder.setTraversableResolverResultCacheEnabled( enabled );
130         return this;
131     }
132
133     @Override
134     public HibernateValidatorContext temporalValidationTolerance(Duration temporalValidationTolerance) {
135         validatorFactoryScopedContextBuilder.setTemporalValidationTolerance( temporalValidationTolerance );
136         return this;
137     }
138
139     @Override
140     public HibernateValidatorContext constraintValidatorPayload(Object dynamicPayload) {
141         validatorFactoryScopedContextBuilder.setConstraintValidatorPayload( dynamicPayload );
142         return this;
143     }
144
145     @Override
146     public Validator getValidator() {
147         return validatorFactory.createValidator(
148                 constraintValidatorFactory,
149                 valueExtractorDescriptors.isEmpty()
150                         ? constraintCreationContext
151                         : new ConstraintCreationContext( constraintCreationContext.getConstraintHelper(),
152                                 constraintCreationContext.getConstraintValidatorManager(), constraintCreationContext.getTypeResolutionHelper(),
153                                 new ValueExtractorManager( constraintCreationContext.getValueExtractorManager(), valueExtractorDescriptors ) ),
154                 validatorFactoryScopedContextBuilder.build(),
155                 methodValidationConfigurationBuilder.build() );
156     }
157 }
158