diff options
author | Artur Signell <artur@vaadin.com> | 2011-12-19 16:39:04 +0200 |
---|---|---|
committer | Artur Signell <artur@vaadin.com> | 2011-12-19 16:41:11 +0200 |
commit | 0d6357abf3631545ddcd3556430da6ffda8e76fe (patch) | |
tree | 7b5df1dbe3bda426b4f87e5a11365efc54a8dfce /src/com/vaadin/data/validator | |
parent | 4253a856db41e1eaf4c78f18422b00ea2d15344f (diff) | |
download | vaadin-framework-0d6357abf3631545ddcd3556430da6ffda8e76fe.tar.gz vaadin-framework-0d6357abf3631545ddcd3556430da6ffda8e76fe.zip |
#8095 Only use BeanValidation if a JSR-303 implementation is available
Diffstat (limited to 'src/com/vaadin/data/validator')
-rw-r--r-- | src/com/vaadin/data/validator/BeanValidationValidator.java | 62 |
1 files changed, 40 insertions, 22 deletions
diff --git a/src/com/vaadin/data/validator/BeanValidationValidator.java b/src/com/vaadin/data/validator/BeanValidationValidator.java index 527ffac245..e144018698 100644 --- a/src/com/vaadin/data/validator/BeanValidationValidator.java +++ b/src/com/vaadin/data/validator/BeanValidationValidator.java @@ -4,8 +4,6 @@ package com.vaadin.data.validator;
-import java.io.IOException;
-import java.io.ObjectInputStream;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
@@ -46,10 +44,9 @@ import com.vaadin.ui.Field; public class BeanValidationValidator implements Validator {
private static final long serialVersionUID = 1L;
- private static ValidatorFactory factory = Validation
- .buildDefaultValidatorFactory();
+ private static ValidatorFactory factory;
- private transient javax.validation.Validator validator;
+ private transient javax.validation.Validator javaxBeanValidator;
private String propertyName;
private Class<?> beanClass;
private Locale locale;
@@ -89,7 +86,6 @@ public class BeanValidationValidator implements Validator { public BeanValidationValidator(Class<?> beanClass, String propertyName) {
this.beanClass = beanClass;
this.propertyName = propertyName;
- validator = factory.getValidator();
locale = Locale.getDefault();
}
@@ -111,12 +107,13 @@ public class BeanValidationValidator implements Validator { * @return the created validator
*/
public static BeanValidationValidator addValidator(Field field,
- Object propertyId, Class<?> beanClass) {
+ Object objectPropertyId, Class<?> beanClass) {
+ String propertyId = (String) objectPropertyId;
BeanValidationValidator validator = new BeanValidationValidator(
- beanClass, String.valueOf(propertyId));
- PropertyDescriptor constraintsForProperty = validator.validator
- .getConstraintsForClass(beanClass).getConstraintsForProperty(
- propertyId.toString());
+ beanClass, propertyId);
+ PropertyDescriptor constraintsForProperty = validator
+ .getJavaxBeanValidator().getConstraintsForClass(beanClass)
+ .getConstraintsForProperty(propertyId);
if (constraintsForProperty != null) {
int nonNotNullValidators = constraintsForProperty
.getConstraintDescriptors().size();
@@ -148,8 +145,9 @@ public class BeanValidationValidator implements Validator { * @return true if the field is marked as not null
*/
public boolean isRequired() {
- PropertyDescriptor desc = validator.getConstraintsForClass(beanClass)
- .getConstraintsForProperty(propertyName);
+ PropertyDescriptor desc = getJavaxBeanValidator()
+ .getConstraintsForClass(beanClass).getConstraintsForProperty(
+ propertyName);
if (desc != null) {
Iterator<ConstraintDescriptor<?>> it = desc
.getConstraintDescriptors().iterator();
@@ -170,13 +168,13 @@ public class BeanValidationValidator implements Validator { }
public void validate(final Object value) throws InvalidValueException {
- Set<?> violations = validator.validateValue(beanClass, propertyName,
- value);
+ Set<?> violations = getJavaxBeanValidator().validateValue(beanClass,
+ propertyName, value);
if (violations.size() > 0) {
List<String> exceptions = new ArrayList<String>();
for (Object v : violations) {
final ConstraintViolation<?> violation = (ConstraintViolation<?>) v;
- String msg = factory.getMessageInterpolator().interpolate(
+ String msg = getJavaxBeanValidatorFactory().getMessageInterpolator().interpolate(
violation.getMessageTemplate(),
new SimpleContext(value, violation
.getConstraintDescriptor()), locale);
@@ -195,7 +193,8 @@ public class BeanValidationValidator implements Validator { private String getErrorMessage(final Object value,
Class<? extends Annotation>... an) {
- BeanDescriptor beanDesc = validator.getConstraintsForClass(beanClass);
+ BeanDescriptor beanDesc = getJavaxBeanValidator()
+ .getConstraintsForClass(beanClass);
PropertyDescriptor desc = beanDesc
.getConstraintsForProperty(propertyName);
if (desc == null) {
@@ -227,7 +226,7 @@ public class BeanValidationValidator implements Validator { throw new InvalidValueException(
"Annotation must have message attribute");
}
- String msg = factory.getMessageInterpolator().interpolate(
+ String msg = getJavaxBeanValidatorFactory().getMessageInterpolator().interpolate(
messageTemplate, new SimpleContext(value, d), locale);
exceptions.add(msg);
}
@@ -265,9 +264,28 @@ public class BeanValidationValidator implements Validator { return locale;
}
- private void readObject(ObjectInputStream in) throws IOException,
- ClassNotFoundException {
- in.defaultReadObject();
- validator = factory.getValidator();
+ protected static ValidatorFactory getJavaxBeanValidatorFactory() {
+ if (factory == null) {
+ factory = Validation.buildDefaultValidatorFactory();
+ }
+
+ return factory;
+ }
+
+ protected javax.validation.Validator getJavaxBeanValidator() {
+ if (javaxBeanValidator == null) {
+ javaxBeanValidator = getJavaxBeanValidatorFactory().getValidator();
+ }
+
+ return javaxBeanValidator;
+ }
+
+ public static boolean isImplementationAvailable() {
+ try {
+ getJavaxBeanValidatorFactory();
+ return true;
+ } catch (Exception e) {
+ return false;
+ }
}
}
\ No newline at end of file |