Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Add Validator producer component.
Browse files Browse the repository at this point in the history
This is done to decouple the validators from the components that use them. They can now be injected.
This revision also makes the raw VersionRestTest and GlossaryRestTest work under the new framework.
Still need to find a way to run DBUnit change sets less intrusively.
  • Loading branch information
Carlos Munoz committed Mar 15, 2013
1 parent 69058a5 commit d5a2e0b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
14 changes: 1 addition & 13 deletions zanata-war/src/main/java/org/zanata/rest/service/RestUtils.java
Expand Up @@ -3,10 +3,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
Expand All @@ -26,16 +24,7 @@ public class RestUtils
private static final Logger log = LoggerFactory.getLogger(RestUtils.class);

@In
ValidatorFactory validatorFactory;

public RestUtils()
{
}

public RestUtils(ValidatorFactory validatorFactory)
{
this.validatorFactory = validatorFactory;
}
Validator validator;

/**
* Validate Hibernate Validator based constraints.
Expand All @@ -49,7 +38,6 @@ public RestUtils(ValidatorFactory validatorFactory)
@SuppressWarnings("unchecked")
public <T> void validateEntity(T entity)
{
Validator validator = validatorFactory.getValidator();
validator.getConstraintsForClass(entity.getClass());
Set<ConstraintViolation<T>> violations = validator.validate(entity);
if (!violations.isEmpty())
Expand Down
Expand Up @@ -134,8 +134,6 @@
<web:multipart-filter create-temp-files="true"
max-request-size="10000000" url-pattern="*.seam"/>

<component name="validatorFactory" jndi-name="java:comp/ValidatorFactory" />

<component name="essentialDataCreator" class="org.zanata.util.EssentialDataCreator" installed="true">
<property name="username">admin</property>
<property name="password">admin</property>
Expand Down
@@ -0,0 +1,51 @@
/*
* Copyright 2010, Red Hat, Inc. and individual contributors as indicated by the
* @author tags. See the copyright.txt file in the distribution for a full
* listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this software; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
* site: http://www.fsf.org.
*/
package org.zanata.validation;

import javax.validation.Validation;
import javax.validation.Validator;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

/**
* Producer for Bean Validators.
*
* @author Carlos Munoz <a href="mailto:camunoz@redhat.com">camunoz@redhat.com</a>
*/
@Name("validatorFactory")
@Scope(ScopeType.APPLICATION)
public class ValidatorFactory
{
private javax.validation.ValidatorFactory beanValidatorFactory;

@Factory(scope = ScopeType.EVENT, autoCreate = true)
public Validator getValidator()
{
if( beanValidatorFactory == null )
{
beanValidatorFactory = Validation.buildDefaultValidatorFactory();
}
return beanValidatorFactory.getValidator();
}
}

0 comments on commit d5a2e0b

Please sign in to comment.