|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectnet.sf.oval.Validator
public class Validator
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.
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 validator instance and uses a new instance of AnnotationsConfigurer |
|
Validator(Collection<Configurer> configurers)
Constructs a new validator instance and configures it using the given configurers |
|
Validator(Configurer... configurers)
Constructs a new validator 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 |
|---|
protected final ThreadLocalLinkedList<Set<Object>> currentlyValidatedObjects
protected final ThreadLocalLinkedList<List<ConstraintViolation>> currentViolations
protected final ExpressionLanguageRegistry expressionLanguageRegistry
protected final ObjectGraphNavigatorRegistry ognRegistry
protected final Validator.DelegatingParameterNameResolver parameterNameResolver
| Constructor Detail |
|---|
public Validator()
public Validator(Collection<Configurer> configurers)
configurers - public Validator(Configurer... configurers)
configurers - | Method Detail |
|---|
public static CollectionFactory getCollectionFactory()
public static OValContextRenderer getContextRenderer()
public static LocaleProvider getLocaleProvider()
public static LoggerFactory getLoggerFactory()
public static MessageResolver getMessageResolver()
public static MessageValueFormatter getMessageValueFormatter()
public static void setCollectionFactory(CollectionFactory factory)
throws IllegalArgumentException
factory - the new collection factory to be used by all validator instances
IllegalArgumentExceptionpublic static void setContextRenderer(OValContextRenderer contextRenderer)
contextRenderer - the contextRenderer to setpublic static void setLocaleProvider(LocaleProvider localeProvider)
public static void setLoggerFactory(LoggerFactory loggerFactory)
loggerFactory - the loggerFactory to set
public static void setMessageResolver(MessageResolver messageResolver)
throws IllegalArgumentException
messageResolver - the messageResolver to set
IllegalArgumentException - if messageResolver == nullpublic static void setMessageValueFormatter(MessageValueFormatter formatter)
formatter - the messageValueFormatter to set
public void addChecks(Class<?> clazz,
Check... checks)
throws IllegalArgumentException
clazz - the class to register the checks forchecks - the checks to add
IllegalArgumentException - if clazz == null or checks == null or checks is empty
public void addChecks(Field field,
Check... checks)
throws IllegalArgumentException
field - the field to declare the checks forchecks - the checks to add
IllegalArgumentException - if field == null or checks == null or checks is empty
public void addChecks(Method invariantMethod,
Check... checks)
throws IllegalArgumentException,
InvalidConfigurationException
invariantMethod - a non-void, non-parameterized method (usually a JavaBean Getter style method)checks - the checks to add
IllegalArgumentException - if getter == null or checks == null
InvalidConfigurationException - if getter is not a getter method
public void addConstraintSet(ConstraintSet constraintSet,
boolean overwrite)
throws ConstraintSetAlreadyDefinedException,
IllegalArgumentException
constraintSet - cannot be nulloverwrite -
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
public void assertValid(Object validatedObject)
throws IllegalArgumentException,
ValidationFailedException,
ConstraintsViolatedException
assertValid in interface IValidatorvalidatedObject - the object to validate, cannot be null
IllegalArgumentException - if validatedObject == null
ValidationFailedException
ConstraintsViolatedException
public void assertValidFieldValue(Object validatedObject,
Field validatedField,
Object fieldValueToValidate)
throws IllegalArgumentException,
ValidationFailedException,
ConstraintsViolatedException
assertValidFieldValue in interface IValidatorvalidatedObject - the object to validate, cannot be nullvalidatedField - the field to validate, cannot be null
IllegalArgumentException - if validatedObject == null or field == null
ValidationFailedException
ConstraintsViolatedException
protected void checkConstraint(List<ConstraintViolation> violations,
Check check,
Object validatedObject,
Object valueToValidate,
OValContext context,
String[] profiles,
boolean isContainerValue)
throws OValException
OValException
protected void checkConstraintAssertConstraintSet(List<ConstraintViolation> violations,
AssertConstraintSetCheck check,
Object validatedObject,
Object valueToValidate,
OValContext context,
String[] profiles)
throws OValException
OValException
protected void checkConstraintAssertFieldConstraints(List<ConstraintViolation> violations,
AssertFieldConstraintsCheck check,
Object validatedObject,
Object valueToValidate,
OValContext context,
String[] profiles)
throws OValException
OValException
protected void checkConstraintAssertValid(List<ConstraintViolation> violations,
AssertValidCheck check,
Object validatedObject,
Object valueToValidate,
OValContext context,
String[] profiles)
throws OValException
OValExceptionpublic void disableAllProfiles()
public void disableProfile(String profile)
profile - the id of the profilepublic void enableAllProfiles()
public void enableProfile(String profile)
profile - the id of the profile
public Check[] getChecks(Class<?> clazz)
throws IllegalArgumentException
clazz - the class to get the checks for
IllegalArgumentException - if clazz == null
public Check[] getChecks(Field field)
throws IllegalArgumentException
field - the field to get the checks for
IllegalArgumentException - if field == null
public Check[] getChecks(Method method)
throws IllegalArgumentException
method - the method to get the checks for
IllegalArgumentException - if getter == null
protected ClassChecks getClassChecks(Class<?> clazz)
throws IllegalArgumentException,
InvalidConfigurationException,
ReflectionException
clazz - cannot be null
IllegalArgumentException - if clazz == null
InvalidConfigurationException
ReflectionExceptionpublic Set<Configurer> getConfigurers()
public ConstraintSet getConstraintSet(String constraintSetId)
throws InvalidConfigurationException,
IllegalArgumentException
constraintSetId - the id of the constraint set to retrieve
InvalidConfigurationException
IllegalArgumentException - if constraintSetId is nullpublic ExceptionTranslator getExceptionTranslator()
public ExpressionLanguageRegistry getExpressionLanguageRegistry()
public ObjectGraphNavigatorRegistry getObjectGraphNavigatorRegistry()
protected boolean isAnyProfileEnabled(String[] profilesOfCheck,
String[] enabledProfiles)
profilesOfCheck - enabledProfiles - optional array of profiles (can be null)
protected boolean isCurrentlyValidated(Object object)
object -
public boolean isProfileEnabled(String profileId)
profileId -
public void reconfigureChecks()
public void removeChecks(Class<?> clazz,
Check... checks)
throws IllegalArgumentException
clazz - checks -
IllegalArgumentException - if clazz == null or checks == null or checks is empty
public void removeChecks(Field field,
Check... checks)
throws IllegalArgumentException
field - checks -
IllegalArgumentException - if field == null or checks == null or checks is empty
public void removeChecks(Method getter,
Check... checks)
throws IllegalArgumentException
getter - a JavaBean Getter style methodchecks -
IllegalArgumentException - if getter == null or checks == null
public ConstraintSet removeConstraintSet(String id)
throws IllegalArgumentException
id - the id of the constraint set to remove, cannot be null
IllegalArgumentException - if id == null
protected String renderMessage(OValContext context,
Object value,
String messageKey,
Map<String,?> messageValues)
public void reportConstraintViolation(ConstraintViolation constraintViolation)
constraintViolation - the constraint violation
protected Object resolveValue(FieldContext ctx,
Object validatedObject)
validatedObject - may be null for static fields
protected Object resolveValue(MethodReturnValueContext ctx,
Object validatedObject)
validatedObject - may be null for static methodspublic void setExceptionTranslator(ExceptionTranslator exceptionTranslator)
exceptionTranslator - the exceptionTranslator to setprotected RuntimeException translateException(OValException ex)
public List<ConstraintViolation> validate(Object validatedObject)
throws IllegalArgumentException,
ValidationFailedException
validate in interface IValidatorvalidatedObject - the object to validate, cannot be null
IllegalArgumentException - if validatedObject == null
ValidationFailedException
public List<ConstraintViolation> validate(Object validatedObject,
String... profiles)
throws IllegalArgumentException,
ValidationFailedException
validate in interface IValidatorvalidatedObject - the object to validate, cannot be nullprofiles - constraint profiles to validate against, by default the globally enabled profiles are used that.
IllegalArgumentException - if validatedObject == null
ValidationFailedException
public List<ConstraintViolation> validateFieldValue(Object validatedObject,
Field validatedField,
Object fieldValueToValidate)
throws IllegalArgumentException,
ValidationFailedException
validateFieldValue in interface IValidatorIllegalArgumentException - if validatedObject == null or validatedField == null
ValidationFailedException
protected void validateInvariants(Object validatedObject,
List<ConstraintViolation> violations,
String[] profiles)
throws IllegalArgumentException,
ValidationFailedException
validatedObject - the object to validate, cannot be null
ValidationFailedException
IllegalArgumentException - if validatedObject == null
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||