Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.makeandbuild.validation.proxy;
  
  import java.util.List;
  import java.util.Map;
 
 
Creates a dynamic proxy for a DAO that extends BaseDao. That dynamic proxy will in turn validate any beans that passed to any BaseDao methods which the dao itself supports based on it entity class. User: Jeremy Dyer Date: 3/6/14 Time: 4:54 PM
 
 @SuppressWarnings({ "unused""rawtypes" })
 public class BeanValidationProxy
         implements InvocationHandler {
 
     Log logger = LogFactory.getLog(getClass());
 
     private Dao obj;
     private String[] validationTypes;
 
     //Cached Map of Spring validator(s)
     private Map<Class<?>, List<Validator>> cachedValidatorsMap = null;
 
     public static Object newInstance(Dao objMap<Class<?>, List<Validator>> validatorCacheMap) {
         return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(),
                 new BeanValidationProxy(objvalidatorCacheMap));
     }
 
     private BeanValidationProxy(Dao objMap<Class<?>, List<Validator>> validatorCacheMapString... validationTypes) {
         this. = obj;
         this. = validatorCacheMap;
         this. = validationTypes;
     }
     public static Object newInstance(Dao objMap<Class<?>, List<Validator>> validatorCacheMapString... validationTypes) {
         return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(),
                 new BeanValidationProxy(objvalidatorCacheMapvalidationTypes));
     }
     private boolean skip(Validator validator){
         if ( == null || . == 0){
             return false;
         } else {
             String type = "data";
             if (validator.getClass().isAnnotationPresent(ValidationType.class)){
                 type = validator.getClass().getAnnotation(ValidationType.class).value();
             }
             for (String filter : ){
                 if (filter.equals(type)){
                     return false;
                 }
             }
             return true;
         }
 
     }
     @SuppressWarnings("unchecked")
     public Object invoke(Object proxyMethod mObject[] argsthrows Throwable {
         Object result;
         try {
 
             //Check method that is being invoked actually even contains the Class we are interested in.
             if (args != null) {
                 List<ObjectparameterBeansToValidate = null;
                 for (int i = 0; i < args.lengthi++) {
                     if (.getEntityClass().isAssignableFrom(args[i].getClass())) {
                         if (parameterBeansToValidate == null)
                             parameterBeansToValidate = new ArrayList<Object>();
                         parameterBeansToValidate.add(args[i]);
                     }
                 }
 
                 if (parameterBeansToValidate != null && parameterBeansToValidate.size() > 0) {
 
                     //Runs the validator against each bean that was in the parameters.
                     for (Object paramBean : parameterBeansToValidate) {
 
                         if (.containsKey(paramBean.getClass())) {
 
                             List<Validatorvalidators = .get(paramBean.getClass());
 
                            for (Validator validator : validators) {
                                if (skip(validator))
                                    continue;
                                List<ObjectErrorvalidationErrors = null;
                                if (validator.supports(paramBean.getClass())) {
                                    BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult
                                            (paramBeanparamBean.getClass().getName());
                                    ValidationUtils.invokeValidator(validatorparamBeanbindingResult);
                                    if (bindingResult.hasErrors()) {
                                        if (validationErrors == null)
                                            validationErrors = new ArrayList<ObjectError>();
                                        validationErrors.addAll(bindingResult.getAllErrors());
                                    }
                                }
                                if (validationErrors != null && validationErrors.size() > 0) {
                                    throw new BeanValidationException(paramBean.getClass().getName() + " " +
                                            "validation failed"validationErrorsparamBean);
                                }
                            }
                        } else {
                            .debug("No validators found for Bean - " + paramBean.getClass().getName());
                        }
                    }
                }
            }
            .debug("Invoking method " + m.getName() + " from dynamic proxy " + getClass().getName());
            result = m.invoke(args);
        } catch (InvocationTargetException e) {
            .error("problem with method " + m.getName(), e);
            throw e.getTargetException();
        } finally {
            .debug("after method " + m.getName());
        }
        return result;
    }
New to GrepCode? Check out our FAQ X