Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package de.zalando.sprocwrapper.proxy.executors;
  
  
  import java.util.Set;
  
  
 
 
 
This Executor wraps stored procedure calls that use advisory locks and / or need different statement timeouts set.

Author(s):
carsten.wolters
 
 public class ValidationExecutorWrapper implements Executor {
 
     private static final ConstraintValidator<NotNullObjectNOT_NULL_VALIDATOR = new NotNullValidator();
 
     private final Executor executor;
 
     private static final Logger LOG = LoggerFactory.getLogger(ValidationExecutorWrapper.class);
     private static ValidatorFactory factory;
 
     static {
         try {
              = Validation.buildDefaultValidatorFactory();
         } catch (final Exception e) {
             .error("Could not build default validator factory"e);
         }
     }
 
     public ValidationExecutorWrapper(final Executor e) {
          = e;
     }
 
     @SuppressWarnings("rawtypes")
     @Override
     public Object executeSProc(final DataSource dsfinal String sqlfinal Object[] argsfinal int[] types,
             final InvocationContext invocationContextfinal Class returnType) {
 
         if ( != null) {
             final Validator validator = .getValidator();
             final Set<ConstraintViolation<?>> constraintViolations = Sets.newHashSet();
 
             if (args != null) {
                 validateParameters(invocationContextvalidatorinvocationContext.getArgs(), constraintViolations);
             }
 
             if (!constraintViolations.isEmpty()) {
                 throw new ConstraintViolationException("SPROC call does not meet all constraints. Aborting.",
                     constraintViolations);
             }
 
             final Object result = .executeSProc(dssqlargstypesinvocationContextreturnType);
 
             if (result != null) {
                 constraintViolations.addAll(validator.validate(result));
 
                 if (!constraintViolations.isEmpty()) {
                     throw new ConstraintViolationException("SPROC return object does not meet all constraints.",
                         constraintViolations);
                 }
             }
 
             return result;
         }
 
         // else do nothing
         return .executeSProc(dssqlargstypesinvocationContextreturnType);
     }
 
     private void validateParameters(final InvocationContext invocationContextfinal Validator validator,
             final Object[] originalArgsfinal Set<ConstraintViolation<?>> constraintViolations) {
 
         if (originalArgs != null) {
            Invokable<?, Objectinvokable = Invokable.from(invocationContext.getMethod());
            for (int i = 0; i < originalArgs.lengthi++) {
                Object arg = originalArgs[i];
                if (!.isValid(argnull)) {
                    // JSR 303 doesn't support method level validation
                    // we should provide at least a dummy implementation to detect @NotNull annotations
                    // we should migrate our implementation to bean validation 1.1 when possible
                    final Parameter parameter = invokable.getParameters().get(i);
                    final NotNull annotation = parameter.getAnnotation(NotNull.class);
                    if (annotation != null) {
                        final String parameterName = "arg" + i;
                        final ConstraintDescriptor<NotNulldescriptor = new SimpleConstraintDescriptor<NotNull>(
                                annotation, ImmutableSet.<Class<?>>of(Default.class),
                                ImmutableList.<Class<? extends ConstraintValidator<NotNull, ?>>>of(
                                    NotNullValidator.class), null);
                        final ConstraintViolation<Objectviolation = new MethodConstraintValidationHolder<Object>(
                                // message
                                "may not be null",
                                // messageTemplate
                                annotation.message(),
                                // rootBean
                                invocationContext.getProxy(),
                                // leafBean (the object the method is executed on)
                                invocationContext.getProxy(),
                                // propertyPath
                                SimplePath.createPathForMethodParameter(invocationContext.getMethod(), parameterName),
                                // invalidValue
                                null,
                                // constraintDescriptor
                                descriptor,
                                // elementType
                                .,
                                // method
                                invocationContext.getMethod(),
                                // parameterIndex
                                i,
                                // parameterName
                                parameterName);
                        constraintViolations.add(violation);
                    }
                } else {
                    constraintViolations.addAll(validator.validate(arg));
                }
            }
        }
    }
New to GrepCode? Check out our FAQ X