Search in sources :

Example 31 with ValidatorFactory

use of javax.validation.ValidatorFactory in project tomee by apache.

the class ValidatorBuilder method buildFactory.

public static ValidatorFactory buildFactory(final ValidationInfo config, final ClassLoader classLoader) {
    ValidatorFactory factory = null;
    final Thread thread = Thread.currentThread();
    final ClassLoader oldContextLoader = thread.getContextClassLoader();
    try {
        thread.setContextClassLoader(classLoader);
        if (config == null) {
            factory = Validation.buildDefaultValidatorFactory();
        } else {
            final Configuration<?> configuration = getConfig(config);
            try {
                factory = configuration.buildValidatorFactory();
            } catch (final ValidationException ve) {
                thread.setContextClassLoader(ValidatorBuilder.class.getClassLoader());
                factory = Validation.buildDefaultValidatorFactory();
                thread.setContextClassLoader(classLoader);
                logger.warning("Unable create validator factory with config " + config + " (" + ve.getMessage() + ")." + " Default factory will be used.");
            }
        }
    } finally {
        thread.setContextClassLoader(oldContextLoader);
    }
    return factory;
}
Also used : ValidationException(javax.validation.ValidationException) ValidatorFactory(javax.validation.ValidatorFactory) ConstraintValidatorFactory(javax.validation.ConstraintValidatorFactory)

Example 32 with ValidatorFactory

use of javax.validation.ValidatorFactory in project tomee by apache.

the class BuiltInEnvironmentEntries method add.

private void add(final JndiConsumer jndi, final DeploymentModule module, final DeploymentModule app, final boolean defaults) {
    // Standard names
    add(jndi.getEnvEntryMap(), new EnvEntry().name("java:module/ModuleName").value(module.getModuleId()).type(String.class));
    add(jndi.getEnvEntryMap(), new EnvEntry().name("java:app/AppName").value(app.getModuleId()).type(String.class));
    // Standard References to built-in objects
    add(jndi.getResourceEnvRefMap(), new ResourceEnvRef().name("java:comp/BeanManager").type(BeanManager.class));
    add(jndi.getResourceEnvRefMap(), new ResourceEnvRef().name("java:comp/Validator").type(Validator.class));
    add(jndi.getResourceEnvRefMap(), new ResourceEnvRef().name("java:comp/ValidatorFactory").type(ValidatorFactory.class));
    add(jndi.getResourceEnvRefMap(), new ResourceEnvRef().name("java:comp/TransactionManager").type(TransactionManager.class));
    add(jndi.getResourceEnvRefMap(), new ResourceEnvRef().name("java:comp/TransactionSynchronizationRegistry").type(TransactionSynchronizationRegistry.class));
    if (defaults) {
        add(jndi.getResourceEnvRefMap(), new ResourceEnvRef().name("java:comp/DefaultManagedExecutorService").type(ManagedExecutorService.class));
        add(jndi.getResourceEnvRefMap(), new ResourceEnvRef().name("java:comp/DefaultManagedScheduledExecutorService").type(ManagedScheduledExecutorService.class));
        add(jndi.getResourceEnvRefMap(), new ResourceEnvRef().name("java:comp/DefaultManagedThreadFactory").type(ManagedThreadFactory.class));
        add(jndi.getResourceEnvRefMap(), new ResourceEnvRef().name("java:comp/DefaultContextService").type(ContextService.class));
        try {
            final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
            contextClassLoader.loadClass("org.apache.activemq.ActiveMQSslConnectionFactory");
            final ResourceEnvRef ref = new ResourceEnvRef().name("java:comp/DefaultJMSConnectionFactory").type(contextClassLoader.loadClass("javax.jms.ConnectionFactory"));
            add(jndi.getResourceEnvRefMap(), ref);
        } catch (final ClassNotFoundException | NoClassDefFoundError notThere) {
        // no-op
        }
    }
    // OpenEJB specific feature
    add(jndi.getEnvEntryMap(), new EnvEntry().name("java:comp/ComponentName").value(jndi.getJndiConsumerName()).type(String.class));
}
Also used : ContextService(javax.enterprise.concurrent.ContextService) ValidatorFactory(javax.validation.ValidatorFactory) ManagedThreadFactory(javax.enterprise.concurrent.ManagedThreadFactory) ManagedExecutorService(javax.enterprise.concurrent.ManagedExecutorService) TransactionManager(javax.transaction.TransactionManager) TransactionSynchronizationRegistry(javax.transaction.TransactionSynchronizationRegistry) BeanManager(javax.enterprise.inject.spi.BeanManager) ResourceEnvRef(org.apache.openejb.jee.ResourceEnvRef) Validator(javax.validation.Validator) ManagedScheduledExecutorService(javax.enterprise.concurrent.ManagedScheduledExecutorService) EnvEntry(org.apache.openejb.jee.EnvEntry)

Example 33 with ValidatorFactory

use of javax.validation.ValidatorFactory in project tomee by apache.

the class AppComposerAsJUnitRuleWithReusableModulesTest method lookupValidatorFactory.

@Test
public void lookupValidatorFactory() throws Exception {
    final ValidatorFactory validatorFactory = (ValidatorFactory) new InitialContext().lookup("java:comp/ValidatorFactory");
    assertNotNull(validatorFactory);
}
Also used : ValidatorFactory(javax.validation.ValidatorFactory) InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

Example 34 with ValidatorFactory

use of javax.validation.ValidatorFactory in project tomee by apache.

the class BeanValidationTest method injectionValidatorFactory.

@Test
public void injectionValidatorFactory() {
    final ValidatorFactory validatorFactory = persistManager.getValidatorFactory();
    assertNotNull(validatorFactory);
}
Also used : ValidatorFactory(javax.validation.ValidatorFactory) Test(org.junit.Test)

Example 35 with ValidatorFactory

use of javax.validation.ValidatorFactory in project Payara by payara.

the class SimpleBVServlet method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    PrintWriter out = resp.getWriter();
    resp.setContentType("text/html");
    out.print("<html><head><title>SimpleBVServlet</title></head><body>");
    ValidatorFactory validatorFactory = null;
    validatorFactory = Validation.buildDefaultValidatorFactory();
    out.print("<p>");
    out.print("Obtained ValidatorFactory: " + validatorFactory + ".");
    out.print("</p>");
    ValidatorContext validatorContext = validatorFactory.usingContext();
    javax.validation.Validator beanValidator = validatorContext.getValidator();
    out.print("<h1>");
    out.print("Validating person class using validateValue with valid property");
    out.print("</h1>");
    List<String> listOfString = new ArrayList<String>();
    listOfString.add("one");
    listOfString.add("two");
    listOfString.add("three");
    Set<ConstraintViolation<Person>> violations = beanValidator.validateValue(Person.class, "listOfString", listOfString);
    printConstraintViolations(out, violations, "case1");
    out.print("<h1>");
    out.print("Validating person class using validateValue with invalid property");
    out.print("</h1>");
    try {
        violations = beanValidator.validateValue(Person.class, "nonExistentProperty", listOfString);
    } catch (IllegalArgumentException iae) {
        out.print("<p>");
        out.print("case2: caught IllegalArgumentException.  Message: " + iae.getMessage());
        out.print("</p>");
    }
    Person person = new Person();
    out.print("<h1>");
    out.print("Validating invalid person instance using validate.");
    out.print("</h1>");
    violations = beanValidator.validate(person);
    printConstraintViolations(out, violations, "case3");
    out.print("<h1>");
    out.print("Validating valid person.");
    out.print("</h1>");
    person.setFirstName("John");
    person.setLastName("Yaya");
    person.setListOfString(listOfString);
    violations = beanValidator.validate(person);
    printConstraintViolations(out, violations, "case4");
    out.print("</body></html>");
}
Also used : ValidatorFactory(javax.validation.ValidatorFactory) ValidatorContext(javax.validation.ValidatorContext) ConstraintViolation(javax.validation.ConstraintViolation) ArrayList(java.util.ArrayList) PrintWriter(java.io.PrintWriter)

Aggregations

ValidatorFactory (javax.validation.ValidatorFactory)55 Validator (javax.validation.Validator)16 Test (org.junit.Test)12 ConstraintViolation (javax.validation.ConstraintViolation)10 Before (org.junit.Before)8 HashMap (java.util.HashMap)5 BeanManager (javax.enterprise.inject.spi.BeanManager)5 InitialContext (javax.naming.InitialContext)5 NamingException (javax.naming.NamingException)5 ArrayList (java.util.ArrayList)4 ConstraintValidatorFactory (javax.validation.ConstraintValidatorFactory)4 ValidationException (javax.validation.ValidationException)4 ValidatorContext (javax.validation.ValidatorContext)4 HibernateValidator (org.hibernate.validator.HibernateValidator)4 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)4 TransactionManager (javax.transaction.TransactionManager)3 TransactionSynchronizationRegistry (javax.transaction.TransactionSynchronizationRegistry)3 CapabilityServiceSupport (org.jboss.as.controller.capability.CapabilityServiceSupport)3 ProxyBeanManager (org.jboss.as.jpa.beanmanager.ProxyBeanManager)3 PhaseOnePersistenceUnitServiceImpl (org.jboss.as.jpa.service.PhaseOnePersistenceUnitServiceImpl)3