Package javax.validation

Examples of javax.validation.Configuration


    * Create a validator factory
    * @return The factory
    */
   public static ValidatorFactory createValidatorFactory()
   {
      Configuration configuration = Validation.byDefaultProvider().configure();
      Configuration<?> conf = configuration.traversableResolver(new JCATraversableResolver());

      return conf.buildValidatorFactory();
   }


   /**
    * Constructor
    */
   public BeanValidation()
   {
      Configuration configuration = Validation.byDefaultProvider().configure();
      Configuration<?> conf = configuration.traversableResolver(new JCATraversableResolver());

      validatorFactory = conf.buildValidatorFactory();
      validator = validatorFactory.getValidator();
   }

    return false;
  }

  @Override
  public ValidatorFactory create(CreationalContext<ValidatorFactory> ctx) {
    Configuration config = Validation.byProvider( org.hibernate.validator.HibernateValidator.class )
        .configure();

    config.constraintValidatorFactory( createConstraintValidatorFactory( config ) );
    config.messageInterpolator( createMessageInterpolator( config ) );
    config.traversableResolver( createTraversableResolver( config ) );
    config.parameterNameProvider( createParameterNameProvider( config ) );

    return config.buildValidatorFactory();
  }

        MessageInterpolator messageInterpolator = resolveAndRemoveReferenceParameter(parameters, "messageInterpolator", MessageInterpolator.class);
        TraversableResolver traversableResolver = resolveAndRemoveReferenceParameter(parameters, "traversableResolver", TraversableResolver.class);
        ConstraintValidatorFactory constraintValidatorFactory = resolveAndRemoveReferenceParameter(parameters, "constraintValidatorFactory", ConstraintValidatorFactory.class);
        String group = getAndRemoveParameter(parameters, "group", String.class);
       
        Configuration configuration = Validation.byDefaultProvider().configure();
       
        if (messageInterpolator != null) {
            configuration.messageInterpolator(messageInterpolator);
        }
       
        if (traversableResolver != null) {
            configuration.traversableResolver(traversableResolver);
        }
       
        if (constraintValidatorFactory != null) {
            configuration.constraintValidatorFactory(constraintValidatorFactory);           
        }
       
        ValidatorFactory validatorFactory = configuration.buildValidatorFactory();
        beanValidator.setValidatorFactory(validatorFactory);
       
        if (group != null) {
            beanValidator.setGroup(getCamelContext().getClassResolver().resolveMandatoryClass(group));
        }

  }


  @SuppressWarnings("unchecked")
  public void afterPropertiesSet() {
    @SuppressWarnings("rawtypes")
    Configuration configuration = (this.providerClass != null ?
        Validation.byProvider(this.providerClass).configure() :
        Validation.byDefaultProvider().configure());

    MessageInterpolator targetInterpolator = this.messageInterpolator;
    if (targetInterpolator == null) {
      targetInterpolator = configuration.getDefaultMessageInterpolator();
    }
    configuration.messageInterpolator(new LocaleContextMessageInterpolator(targetInterpolator));

    if (this.traversableResolver != null) {
      configuration.traversableResolver(this.traversableResolver);
    }

    ConstraintValidatorFactory targetConstraintValidatorFactory = this.constraintValidatorFactory;
    if (targetConstraintValidatorFactory == null && this.applicationContext != null) {
      targetConstraintValidatorFactory =
          new SpringConstraintValidatorFactory(this.applicationContext.getAutowireCapableBeanFactory());
    }
    if (targetConstraintValidatorFactory != null) {
      configuration.constraintValidatorFactory(targetConstraintValidatorFactory);
    }

    if (this.mappingLocations != null) {
      for (Resource location : this.mappingLocations) {
        try {
          configuration.addMapping(location.getInputStream());
        }
        catch (IOException ex) {
          throw new IllegalStateException("Cannot read mapping resource: " + location);
        }
      }
    }

    for (Map.Entry<String, String> entry : this.validationPropertyMap.entrySet()) {
      configuration.addProperty(entry.getKey(), entry.getValue());
    }

    this.validatorFactory = configuration.buildValidatorFactory();
    setTargetValidator(this.validatorFactory.getValidator());
  }

  }


  @SuppressWarnings("unchecked")
  public void afterPropertiesSet() {
    @SuppressWarnings("rawtypes")
    Configuration configuration = (this.providerClass != null ?
        Validation.byProvider(this.providerClass).configure() :
        Validation.byDefaultProvider().configure());

    MessageInterpolator targetInterpolator = this.messageInterpolator;
    if (targetInterpolator == null) {
      targetInterpolator = configuration.getDefaultMessageInterpolator();
    }
    configuration.messageInterpolator(new LocaleContextMessageInterpolator(targetInterpolator));

    if (this.traversableResolver != null) {
      configuration.traversableResolver(this.traversableResolver);
    }

    ConstraintValidatorFactory targetConstraintValidatorFactory = this.constraintValidatorFactory;
    if (targetConstraintValidatorFactory == null && this.applicationContext != null) {
      targetConstraintValidatorFactory =
          new SpringConstraintValidatorFactory(this.applicationContext.getAutowireCapableBeanFactory());
    }
    if (targetConstraintValidatorFactory != null) {
      configuration.constraintValidatorFactory(targetConstraintValidatorFactory);
    }

    if (this.mappingLocations != null) {
      for (Resource location : this.mappingLocations) {
        try {
          configuration.addMapping(location.getInputStream());
        }
        catch (IOException ex) {
          throw new IllegalStateException("Cannot read mapping resource: " + location);
        }
      }
    }

    for (Map.Entry<String, String> entry : this.validationPropertyMap.entrySet()) {
      configuration.addProperty(entry.getKey(), entry.getValue());
    }

    this.validatorFactory = configuration.buildValidatorFactory();
    setTargetValidator(this.validatorFactory.getValidator());
  }

public class CustomMessageInterpolatorTest extends AbstractTest {

  @Test
  @SpecAssertion(section = "4.3.2", id = "b")
  public void testCustomMessageInterpolatorViaConfiguration() {
    Configuration config = Validation.byDefaultProvider().configure();
    config = config.messageInterpolator( new DummyMessageInterpolator() );
    Validator validator = config.buildValidatorFactory().getValidator();

    assertCustomMessageInterpolatorUsed( validator );
  }

  /* (non-Javadoc)
   * @see javax.validation.spi.ValidationProvider#createGenericConfiguration(javax.validation.spi.BootstrapState)
   */
  public Configuration<?> createGenericConfiguration(BootstrapState state) {
    // TODO Auto-generated method stub
    return new Configuration(){

      public Configuration<?> addMapping(
          InputStream stream) {
        // TODO Auto-generated method stub
        return this;

  }


  @SuppressWarnings("unchecked")
  public void afterPropertiesSet() {
    Configuration configuration = (this.providerClass != null ?
        Validation.byProvider(this.providerClass).configure() :
        Validation.byDefaultProvider().configure());

    configuration.messageInterpolator(new LocaleContextMessageInterpolator(
        this.messageInterpolator, configuration.getDefaultMessageInterpolator()));

    if (this.traversableResolver != null) {
      configuration.traversableResolver(this.traversableResolver);
    }

    ConstraintValidatorFactory targetConstraintValidatorFactory = this.constraintValidatorFactory;
    if (targetConstraintValidatorFactory == null && this.applicationContext != null) {
      targetConstraintValidatorFactory =
          new SpringConstraintValidatorFactory(this.applicationContext.getAutowireCapableBeanFactory());
    }
    if (targetConstraintValidatorFactory != null) {
      configuration.constraintValidatorFactory(targetConstraintValidatorFactory);
    }

    if (this.mappingLocations != null) {
      for (Resource location : this.mappingLocations) {
        try {
          configuration.addMapping(location.getInputStream());
        }
        catch (IOException ex) {
          throw new IllegalStateException("Cannot read mapping resource: " + location);
        }
      }
    }

    for (Map.Entry<String, String> entry : this.validationPropertyMap.entrySet()) {
      configuration.addProperty(entry.getKey(), entry.getValue());
    }

    this.validatorFactory = configuration.buildValidatorFactory();
    setTargetValidator(this.validatorFactory.getValidator());
  }

    * Create a validator factory
    * @return The factory
    */
   public static ValidatorFactory createValidatorFactory()
   {
      Configuration configuration = Validation.byDefaultProvider().configure();
      Configuration<?> conf = configuration.traversableResolver(new JCATraversableResolver());

      return conf.buildValidatorFactory();
   }

TOP

Related Classes of javax.validation.Configuration

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.