Tapestry Magic #12 : Tapestry IOC aware JSR-303 Custom Validators

In JSR-303, adding a custom field validator is a two step process.

  1. Create an annotation which will be placed on a field to be validated
  2. Create a validator which implements ConstraintValidator and link it to the above annotation


But there are situations where in you may need to access Tapestry services inside the validator. By default the validators are instantiated by ConstraintValidatorFactory which is by default provided by the ValidatorFactory. So to allow tapestry-ioc to resolve dependencies within the validators, we need to take instantiation into our own hands. Let us create a simple validator which will invoke a service to check if the name of an object is correct. (Stupid but simple)

Create an annotation

We start with an annotation

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Constraint(validatedBy = FooNameConstraintValidator.class)
public @interface FooNameConstraint {

   public abstract String message() default "{com.googlecode.tawus.exports.FooNameConstraint.message}";

   public abstract Class<?>[] groups() default {};

   Class<? extends Payload>[] payload() default {};
}

All the three attributes are required and specify the message to be displayed, groups() to which this annotation is added to and payload() to assign custom payload objects(not used by API itself). Notice that we have linked the validator class using @Constraint attribute.

Create a validator

The validator is very simple.

public class FooNameConstraintValidator implements
      ConstraintValidator<FooNameConstraint, String> {
   
   private FooNameCheckService fooNameCheckService;
   
   public FooNameConstraintValidator(FooNameCheckService fooNameCheckService){
      this.fooNameCheckService = fooNameCheckService;
   }

   public void initialize(FooNameConstraint constraintAnnotation) {
   }

   public boolean isValid(String value, ConstraintValidatorContext context) {
      return fooNameCheckService.isValid(value);
   }

}

This validator calls FooNameCheckService to validate the name. As the validators are created only once, we can use only those services which have default scope. Per-thread services can be accessed by injecting ObjectLocator and the calling getService() method with the required Service interface as argument. FooNameCheckService is defined as

//Interface
public interface FooNameCheckService {
   boolean isValid(String Name);
}


//Implementation
public class FooNameCheckServiceImpl implements FooNameCheckService {

   public boolean isValid(String name) {
      //Check if the length is more than 5
      return name != null && name.length() > 5;
   }

}

Create Tapestry IOC Aware ConstraintValidatorFactory

Now, we have to take control of the instantiation of the validator. This can be done by providing our own implementation of ConstraintValidatorFactory.

public class TapestryConstraintValidatorFactory implements ConstraintValidatorFactory {
   
   private ObjectLocator locator;

   public TapestryConstraintValidatorFactory(ObjectLocator locator){
      this.locator = locator;
   }

   public <T extends ConstraintValidator<?, ?>> T getInstance(Class<T> key) {
      return locator.autobuild(key);
   }

}

Contribute ConstraintValidatorFactory

Finally, we contribute this to BeanValidatorSource in the module class


   @Contribute(BeanValidatorSource.class)
   public static void provideBeanValidatorConfigurer(
         final ObjectLocator locator,
         OrderedConfiguration<BeanValidatorConfigurer> configuration) {
      configuration.add("constraint-validator", new BeanValidatorConfigurer() {
         public void configure(javax.validation.Configuration<?> configuration) {
            configuration.constraintValidatorFactory(new TapestryConstraintValidatorFactory(locator));
         }
      });
   }

We also need to contribute this to hibernate-core, otherwise it will try to use its own implementation of ConstraintValidatorFactory. This can done by turning off the default validation by setting javax.persistence.validation.mode to none in hibernate.cfg.xml

<property name='javax.persistence.validation.mode'>none</property>

and then contributing an event-listener to the hibernate configuration

   public static void contributeHibernateSessionSource(
         final ObjectLocator locator,
         OrderedConfiguration<HibernateConfigurer> config) {
      config.add("hibernateConfiguration", new HibernateConfigurer() {
         public void configure(org.hibernate.cfg.Configuration configuration) {
            Configuration<?> validationConfig = Validation.byDefaultProvider()
                  .configure();
            validationConfig
                  .constraintValidatorFactory(new TapestryConstraintValidatorFactory(locator));
            BeanValidationEventListener listener = new BeanValidationEventListener(
                  validationConfig.buildValidatorFactory(), configuration
                        .getProperties());
            listener.initialize(configuration);
            configuration.setListener("pre-insert", listener);
            configuration.setListener("pre-delete", listener);
            configuration.setListener("pre-update", listener);
         }

      });
   }
Advertisements

Tagged: , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: