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;
}
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));
}
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);
}
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);
}
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>");
}
Aggregations