|
Hello @hardy, first I want to apologize for my bad English!
I've attached a fully deployable webapp as test case, that includes an ejb (TestBean) that implements a very simple interface (Test) that has constraints. I don't know if that is what you mean ass relevant code...?
"It works randomly" means, per request while in the same JVM, using a different ValidatorFactory for each request, sometimes the validation works, this is, for a value that does not meet the constraints, sometimes it returns the errors correctly and sometimes it doesn't return errors. Also it fails more than not (fails means, does not return errors). No exceptions, no warnings. Caching the ValidatorFactory causes that if it works once it continue working, or if it fails, it continue failing.
Original classes:
package bval.ejb;
import javax.ejb.Stateless;
import javax.interceptor.Interceptors;
import bval.interceptor.ValidateParameters;
@Stateless
public class TestBean implements Test {
@Override
@Interceptors(ValidateParameters.class)
public String validate(String param) {
return "OK";
}
}
package bval.ejb;
import javax.ejb.Local;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotBlank;
@Local
public interface Test {
String validate(@NotBlank @Size(max = 2) String param);
}
package bval.interceptor;
import java.util.Set;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.executable.ExecutableValidator;
public class ValidateParameters {
@AroundInvoke
public Object checkArguments(InvocationContext context) throws Exception {
validateParameters(context);
return context.proceed();
}
private void validateParameters(InvocationContext context) throws Exception {
ExecutableValidator executableValidator = Validation.buildDefaultValidatorFactory().getValidator().forExecutables();
Set<ConstraintViolation<Object>> errors = executableValidator.validateParameters(
context.getTarget(),
context.getMethod(),
context.getParameters());
if (!errors.isEmpty()) {
throw new IllegalArgumentException();
}
}
}
I suspect the problem is in AnnotationMetaDataProvider and the enhanced interfaces that weblogic creates:
package bval.ejb;
import javax.ejb.EJBContext;
import weblogic.ejb.container.interfaces.WLEnterpriseBean;
public class TestBean_hpp1x8_Impl extends TestBean
implements TestBean_hpp1x8_Intf, WLEnterpriseBean
{
public TestBean_hpp1x8_Impl()
{
}
public int __WL_getMethodState()
{
return __WL_method_state;
}
public void __WL_setMethodState(int i)
{
__WL_method_state = i;
}
public EJBContext __WL_getEJBContext()
{
return __WL_EJBContext;
}
public void __WL_setEJBContext(EJBContext ejbcontext)
{
__WL_EJBContext = ejbcontext;
}
private int __WL_method_state;
private EJBContext __WL_EJBContext;
}
package bval.ejb;
import weblogic.ejb.container.interfaces.WLEnterpriseBean;
public interface TestBean_hpp1x8_Intf
extends WLEnterpriseBean
{
public abstract String validate(String s);
}
I think, depending on the order that *AnnotationMetaDataProvider *gets the metadata into the bean configuration, the validator gets the original constraints or not. This is just an speculation, as I have not fully understand how validation works.
|