net.sf.oval
Class Validator

java.lang.Object
  extended by net.sf.oval.Validator
All Implemented Interfaces:
IValidator
Direct Known Subclasses:
Guard

public class Validator
extends Object
implements IValidator

Instances of this class can validate objects based on declared constraints. Constraints can either be declared using OVal's constraint annotations, XML configuration files or EJB3 JPA annotations.

This class is thread-safe.

Author:
Sebastian Thomschke
See Also:
AnnotationsConfigurer, JPAAnnotationsConfigurer, POJOConfigurer, XMLConfigurer

Nested Class Summary
protected static class Validator.DelegatingParameterNameResolver
           
 
Field Summary
protected  ThreadLocalLinkedList<Set<Object>> currentlyValidatedObjects
           
protected  ThreadLocalLinkedList<List<ConstraintViolation>> currentViolations
           
protected  ExpressionLanguageRegistry expressionLanguageRegistry
           
protected  ObjectGraphNavigatorRegistry ognRegistry
           
protected  Validator.DelegatingParameterNameResolver parameterNameResolver
           
 
Constructor Summary
Validator()
          Constructs a new instance and uses a new instance of AnnotationsConfigurer
Validator(Collection<Configurer> configurers)
          Constructs a new instance and configures it using the given configurers
Validator(Configurer... configurers)
          Constructs a new instance and configures it using the given configurers
 
Method Summary
 void addChecks(Class<?> clazz, Check... checks)
          Registers object-level constraint checks
 void addChecks(Field field, Check... checks)
          Registers constraint checks for the given field
 void addChecks(Method invariantMethod, Check... checks)
          Registers constraint checks for the given getter's return value
 void addConstraintSet(ConstraintSet constraintSet, boolean overwrite)
          Registers a new constraint set.
 void assertValid(Object validatedObject)
          validates the field and getter constrains of the given object and throws an ConstraintsViolatedException if any constraint violations are detected
 void assertValidFieldValue(Object validatedObject, Field validatedField, Object fieldValueToValidate)
          Validates the give value against the defined field constraints and throws an ConstraintsViolatedException if any constraint violations are detected.
protected  void checkConstraint(List<ConstraintViolation> violations, Check check, Object validatedObject, Object valueToValidate, OValContext context, String[] profiles, boolean isContainerValue)
           
protected  void checkConstraintAssertConstraintSet(List<ConstraintViolation> violations, AssertConstraintSetCheck check, Object validatedObject, Object valueToValidate, OValContext context, String[] profiles)
           
protected  void checkConstraintAssertFieldConstraints(List<ConstraintViolation> violations, AssertFieldConstraintsCheck check, Object validatedObject, Object valueToValidate, OValContext context, String[] profiles)
           
protected  void checkConstraintAssertValid(List<ConstraintViolation> violations, AssertValidCheck check, Object validatedObject, Object valueToValidate, OValContext context, String[] profiles)
           
 void disableAllProfiles()
          Disables all constraints profiles globally, i.e. no configured constraint will be validated.
 void disableProfile(String profile)
          Disables a constraints profile globally.
 void enableAllProfiles()
          Enables all constraints profiles globally, i.e. all configured constraint will be validated.
 void enableProfile(String profile)
          Enables a constraints profile globally.
 Check[] getChecks(Class<?> clazz)
          Gets the object-level constraint checks for the given class
 Check[] getChecks(Field field)
          Gets the constraint checks for the given field
 Check[] getChecks(Method method)
          Gets the constraint checks for the given method's return value
protected  ClassChecks getClassChecks(Class<?> clazz)
          Returns the ClassChecks object for the particular class, allowing you to modify the checks
static CollectionFactory getCollectionFactory()
          Returns a shared instance of the CollectionFactory
 Set<Configurer> getConfigurers()
           
 ConstraintSet getConstraintSet(String constraintSetId)
          Returns the given constraint set.
static OValContextRenderer getContextRenderer()
           
 ExceptionTranslator getExceptionTranslator()
           
 ExpressionLanguageRegistry getExpressionLanguageRegistry()
           
static LocaleProvider getLocaleProvider()
           
static LoggerFactory getLoggerFactory()
           
static MessageResolver getMessageResolver()
           
static MessageValueFormatter getMessageValueFormatter()
           
 ObjectGraphNavigatorRegistry getObjectGraphNavigatorRegistry()
           
protected  boolean isAnyProfileEnabled(String[] profilesOfCheck, String[] enabledProfiles)
          Determines if at least one of the given profiles is enabled
protected  boolean isCurrentlyValidated(Object object)
          Determines if the given object is currently validated in the current thread
 boolean isProfileEnabled(String profileId)
          Determines if the given profile is enabled.
 void reconfigureChecks()
          clears the checks and constraint sets => a reconfiguration using the currently registered configurers will automatically happen
 void removeChecks(Class<?> clazz, Check... checks)
          Removes object-level constraint checks
 void removeChecks(Field field, Check... checks)
          Removes constraint checks for the given field
 void removeChecks(Method getter, Check... checks)
          Removes constraint checks for the given getter's return value
 ConstraintSet removeConstraintSet(String id)
          Removes the constraint set with the given id
protected  String renderMessage(OValContext context, Object value, String messageKey, Map<String,?> messageValues)
           
 void reportConstraintViolation(ConstraintViolation constraintViolation)
          Reports an additional constraint violation for the current validation cycle.
protected  Object resolveValue(FieldContext ctx, Object validatedObject)
           
protected  Object resolveValue(MethodReturnValueContext ctx, Object validatedObject)
           
static void setCollectionFactory(CollectionFactory factory)
           
static void setContextRenderer(OValContextRenderer contextRenderer)
           
 void setExceptionTranslator(ExceptionTranslator exceptionTranslator)
           
static void setLocaleProvider(LocaleProvider localeProvider)
           
static void setLoggerFactory(LoggerFactory loggerFactory)
           
static void setMessageResolver(MessageResolver messageResolver)
           
static void setMessageValueFormatter(MessageValueFormatter formatter)
           
protected  RuntimeException translateException(OValException ex)
           
 List<ConstraintViolation> validate(Object validatedObject)
          validates the field and getter constrains of the given object
 List<ConstraintViolation> validate(Object validatedObject, String... profiles)
          validates the field and getter constrains of the given object
 List<ConstraintViolation> validateFieldValue(Object validatedObject, Field validatedField, Object fieldValueToValidate)
          Validates the give value against the defined field constraints.
protected  void validateInvariants(Object validatedObject, List<ConstraintViolation> violations, String[] profiles)
          validates the field and getter constrains of the given object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

currentlyValidatedObjects

protected final ThreadLocalLinkedList<Set<Object>> currentlyValidatedObjects

currentViolations

protected final ThreadLocalLinkedList<List<ConstraintViolation>> currentViolations

expressionLanguageRegistry

protected final ExpressionLanguageRegistry expressionLanguageRegistry

ognRegistry

protected final ObjectGraphNavigatorRegistry ognRegistry

parameterNameResolver

protected final Validator.DelegatingParameterNameResolver parameterNameResolver
Constructor Detail

Validator

public Validator()
Constructs a new instance and uses a new instance of AnnotationsConfigurer


Validator

public Validator(Collection<Configurer> configurers)
Constructs a new instance and configures it using the given configurers


Validator

public Validator(Configurer... configurers)
Constructs a new instance and configures it using the given configurers

Method Detail

getCollectionFactory

public static CollectionFactory getCollectionFactory()
Returns a shared instance of the CollectionFactory


getContextRenderer

public static OValContextRenderer getContextRenderer()

getLocaleProvider

public static LocaleProvider getLocaleProvider()

getLoggerFactory

public static LoggerFactory getLoggerFactory()

getMessageResolver

public static MessageResolver getMessageResolver()

getMessageValueFormatter

public static MessageValueFormatter getMessageValueFormatter()

setCollectionFactory

public static void setCollectionFactory(CollectionFactory factory)
                                 throws IllegalArgumentException
Parameters:
factory - the new collection factory to be used by all validator instances
Throws:
IllegalArgumentException

setContextRenderer

public static void setContextRenderer(OValContextRenderer contextRenderer)
Parameters:
contextRenderer - the contextRenderer to set

setLocaleProvider

public static void setLocaleProvider(LocaleProvider localeProvider)

setLoggerFactory

public static void setLoggerFactory(LoggerFactory loggerFactory)
Parameters:
loggerFactory - the loggerFactory to set

setMessageResolver

public static void setMessageResolver(MessageResolver messageResolver)
                               throws IllegalArgumentException
Parameters:
messageResolver - the messageResolver to set
Throws:
IllegalArgumentException - if messageResolver == null

setMessageValueFormatter

public static void setMessageValueFormatter(MessageValueFormatter formatter)
Parameters:
formatter - the messageValueFormatter to set

addChecks

public void addChecks(Class<?> clazz,
                      Check... checks)
               throws IllegalArgumentException
Registers object-level constraint checks

Parameters:
clazz - the class to register the checks for
checks - the checks to add
Throws:
IllegalArgumentException - if clazz == null or checks == null or checks is empty

addChecks

public void addChecks(Field field,
                      Check... checks)
               throws IllegalArgumentException
Registers constraint checks for the given field

Parameters:
field - the field to declare the checks for
checks - the checks to add
Throws:
IllegalArgumentException - if field == null or checks == null or checks is empty

addChecks

public void addChecks(Method invariantMethod,
                      Check... checks)
               throws IllegalArgumentException,
                      InvalidConfigurationException
Registers constraint checks for the given getter's return value

Parameters:
invariantMethod - a non-void, non-parameterized method (usually a JavaBean Getter style method)
checks - the checks to add
Throws:
IllegalArgumentException - if getter == null or checks == null
InvalidConfigurationException - if getter is not a getter method

addConstraintSet

public void addConstraintSet(ConstraintSet constraintSet,
                             boolean overwrite)
                      throws ConstraintSetAlreadyDefinedException,
                             IllegalArgumentException
Registers a new constraint set.

Parameters:
constraintSet - cannot be null
Throws:
ConstraintSetAlreadyDefinedException - if overwrite == false and a constraint set with the given id exists already
IllegalArgumentException - if constraintSet == null or constraintSet.id == null or constraintSet.id.length == 0
IllegalArgumentException - if constraintSet.id == null

assertValid

public void assertValid(Object validatedObject)
                 throws IllegalArgumentException,
                        ValidationFailedException,
                        ConstraintsViolatedException
Description copied from interface: IValidator
validates the field and getter constrains of the given object and throws an ConstraintsViolatedException if any constraint violations are detected

Specified by:
assertValid in interface IValidator
Parameters:
validatedObject - the object to validate, cannot be null
Throws:
IllegalArgumentException - if validatedObject == null
ValidationFailedException
ConstraintsViolatedException

assertValidFieldValue

public void assertValidFieldValue(Object validatedObject,
                                  Field validatedField,
                                  Object fieldValueToValidate)
                           throws IllegalArgumentException,
                                  ValidationFailedException,
                                  ConstraintsViolatedException
Description copied from interface: IValidator
Validates the give value against the defined field constraints and throws an ConstraintsViolatedException if any constraint violations are detected.

Specified by:
assertValidFieldValue in interface IValidator
Parameters:
validatedObject - the object to validate, cannot be null
validatedField - the field to validate, cannot be null
Throws:
IllegalArgumentException - if validatedObject == null or field == null
ValidationFailedException
ConstraintsViolatedException

checkConstraint

protected void checkConstraint(List<ConstraintViolation> violations,
                               Check check,
                               Object validatedObject,
                               Object valueToValidate,
                               OValContext context,
                               String[] profiles,
                               boolean isContainerValue)
                        throws OValException
Throws:
OValException

checkConstraintAssertConstraintSet

protected void checkConstraintAssertConstraintSet(List<ConstraintViolation> violations,
                                                  AssertConstraintSetCheck check,
                                                  Object validatedObject,
                                                  Object valueToValidate,
                                                  OValContext context,
                                                  String[] profiles)
                                           throws OValException
Throws:
OValException

checkConstraintAssertFieldConstraints

protected void checkConstraintAssertFieldConstraints(List<ConstraintViolation> violations,
                                                     AssertFieldConstraintsCheck check,
                                                     Object validatedObject,
                                                     Object valueToValidate,
                                                     OValContext context,
                                                     String[] profiles)
                                              throws OValException
Throws:
OValException

checkConstraintAssertValid

protected void checkConstraintAssertValid(List<ConstraintViolation> violations,
                                          AssertValidCheck check,
                                          Object validatedObject,
                                          Object valueToValidate,
                                          OValContext context,
                                          String[] profiles)
                                   throws OValException
Throws:
OValException

disableAllProfiles

public void disableAllProfiles()
Disables all constraints profiles globally, i.e. no configured constraint will be validated.


disableProfile

public void disableProfile(String profile)
Disables a constraints profile globally.

Parameters:
profile - the id of the profile

enableAllProfiles

public void enableAllProfiles()
Enables all constraints profiles globally, i.e. all configured constraint will be validated.


enableProfile

public void enableProfile(String profile)
Enables a constraints profile globally.

Parameters:
profile - the id of the profile

getChecks

public Check[] getChecks(Class<?> clazz)
                  throws IllegalArgumentException
Gets the object-level constraint checks for the given class

Parameters:
clazz - the class to get the checks for
Throws:
IllegalArgumentException - if clazz == null

getChecks

public Check[] getChecks(Field field)
                  throws IllegalArgumentException
Gets the constraint checks for the given field

Parameters:
field - the field to get the checks for
Throws:
IllegalArgumentException - if field == null

getChecks

public Check[] getChecks(Method method)
                  throws IllegalArgumentException
Gets the constraint checks for the given method's return value

Parameters:
method - the method to get the checks for
Throws:
IllegalArgumentException - if getter == null

getClassChecks

protected ClassChecks getClassChecks(Class<?> clazz)
                              throws IllegalArgumentException,
                                     InvalidConfigurationException,
                                     ReflectionException
Returns the ClassChecks object for the particular class, allowing you to modify the checks

Parameters:
clazz - cannot be null
Returns:
returns the ClassChecks for the given class
Throws:
IllegalArgumentException - if clazz == null
InvalidConfigurationException
ReflectionException

getConfigurers

public Set<Configurer> getConfigurers()
Returns:
the internal linked set with the registered configurers

getConstraintSet

public ConstraintSet getConstraintSet(String constraintSetId)
                               throws InvalidConfigurationException,
                                      IllegalArgumentException
Returns the given constraint set.

Parameters:
constraintSetId - the id of the constraint set to retrieve
Returns:
the constraint set or null if not found
Throws:
InvalidConfigurationException
IllegalArgumentException - if constraintSetId is null

getExceptionTranslator

public ExceptionTranslator getExceptionTranslator()
Returns:
the exceptionProcessor

getExpressionLanguageRegistry

public ExpressionLanguageRegistry getExpressionLanguageRegistry()
Returns:
the expressionLanguageRegistry

getObjectGraphNavigatorRegistry

public ObjectGraphNavigatorRegistry getObjectGraphNavigatorRegistry()
Returns:
the objectGraphNavigatorRegistry

isAnyProfileEnabled

protected boolean isAnyProfileEnabled(String[] profilesOfCheck,
                                      String[] enabledProfiles)
Determines if at least one of the given profiles is enabled

Parameters:
profilesOfCheck -
enabledProfiles - optional array of profiles (can be null)
Returns:
Returns true if at least one of the given profiles is enabled.

isCurrentlyValidated

protected boolean isCurrentlyValidated(Object object)
Determines if the given object is currently validated in the current thread

Returns:
Returns true if the given object is currently validated in the current thread.

isProfileEnabled

public boolean isProfileEnabled(String profileId)
Determines if the given profile is enabled.

Returns:
Returns true if the given profile is enabled.

reconfigureChecks

public void reconfigureChecks()
clears the checks and constraint sets => a reconfiguration using the currently registered configurers will automatically happen


removeChecks

public void removeChecks(Class<?> clazz,
                         Check... checks)
                  throws IllegalArgumentException
Removes object-level constraint checks

Throws:
IllegalArgumentException - if clazz == null or checks == null or checks is empty

removeChecks

public void removeChecks(Field field,
                         Check... checks)
                  throws IllegalArgumentException
Removes constraint checks for the given field

Throws:
IllegalArgumentException - if field == null or checks == null or checks is empty

removeChecks

public void removeChecks(Method getter,
                         Check... checks)
                  throws IllegalArgumentException
Removes constraint checks for the given getter's return value

Parameters:
getter - a JavaBean Getter style method
Throws:
IllegalArgumentException - if getter == null or checks == null

removeConstraintSet

public ConstraintSet removeConstraintSet(String id)
                                  throws IllegalArgumentException
Removes the constraint set with the given id

Parameters:
id - the id of the constraint set to remove, cannot be null
Returns:
the removed constraint set
Throws:
IllegalArgumentException - if id == null

renderMessage

protected String renderMessage(OValContext context,
                               Object value,
                               String messageKey,
                               Map<String,?> messageValues)

reportConstraintViolation

public void reportConstraintViolation(ConstraintViolation constraintViolation)
Reports an additional constraint violation for the current validation cycle. This method is intended to be executed by check implementations only.

Parameters:
constraintViolation - the constraint violation

resolveValue

protected Object resolveValue(FieldContext ctx,
                              Object validatedObject)
Parameters:
validatedObject - may be null for static fields

resolveValue

protected Object resolveValue(MethodReturnValueContext ctx,
                              Object validatedObject)
Parameters:
validatedObject - may be null for static methods

setExceptionTranslator

public void setExceptionTranslator(ExceptionTranslator exceptionTranslator)
Parameters:
exceptionTranslator - the exceptionTranslator to set

translateException

protected RuntimeException translateException(OValException ex)

validate

public List<ConstraintViolation> validate(Object validatedObject)
                                   throws IllegalArgumentException,
                                          ValidationFailedException
Description copied from interface: IValidator
validates the field and getter constrains of the given object

Specified by:
validate in interface IValidator
Parameters:
validatedObject - the object to validate, cannot be null
Returns:
a list with the detected constraint violations. if no violations are detected an empty list is returned
Throws:
IllegalArgumentException - if validatedObject == null
ValidationFailedException

validate

public List<ConstraintViolation> validate(Object validatedObject,
                                          String... profiles)
                                   throws IllegalArgumentException,
                                          ValidationFailedException
Description copied from interface: IValidator
validates the field and getter constrains of the given object

Specified by:
validate in interface IValidator
Parameters:
validatedObject - the object to validate, cannot be null
profiles - constraint profiles to validate against, by default the globally enabled profiles are used that.
Returns:
a list with the detected constraint violations. if no violations are detected an empty list is returned
Throws:
IllegalArgumentException - if validatedObject == null
ValidationFailedException

validateFieldValue

public List<ConstraintViolation> validateFieldValue(Object validatedObject,
                                                    Field validatedField,
                                                    Object fieldValueToValidate)
                                             throws IllegalArgumentException,
                                                    ValidationFailedException
Description copied from interface: IValidator
Validates the give value against the defined field constraints.

Specified by:
validateFieldValue in interface IValidator
Returns:
a list with the detected constraint violations. if no violations are detected an empty list is returned
Throws:
IllegalArgumentException - if validatedObject == null or validatedField == null
ValidationFailedException

validateInvariants

protected void validateInvariants(Object validatedObject,
                                  List<ConstraintViolation> violations,
                                  String[] profiles)
                           throws IllegalArgumentException,
                                  ValidationFailedException
validates the field and getter constrains of the given object. if the given object is a class the static fields and getters are validated.

Parameters:
validatedObject - the object to validate, cannot be null
Throws:
IllegalArgumentException - if validatedObject == null
ValidationFailedException


Copyright © 2005–2015 The OVal Development Team. All rights reserved.