Commits

rainerh  committed c7065dc

Cached validators hang on to their old contexts
Issue number: XW-287
Obtained from:
Submitted by: Tom Davies
Reviewed by: Rainer Hermanns

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@876e221344d-f017-0410-9bd5-d282ab1896d7

  • Participants
  • Parent commits d3d0e56

Comments (0)

Files changed (2)

File src/java/com/opensymphony/xwork/validator/DefaultActionValidatorManager.java

         Set shortcircuitedFields = null;
 
         for (Iterator iterator = validators.iterator(); iterator.hasNext();) {
-            Validator validator = (Validator) iterator.next();
-            validator.setValidatorContext(validatorContext);
+            final Validator validator = (Validator) iterator.next();
+            try {
+                validator.setValidatorContext(validatorContext);
 
-            if (LOG.isDebugEnabled()) {
-                LOG.debug("Running validator: " + validator + " for object " + object);
-            }
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Running validator: " + validator + " for object " + object);
+                }
 
-            FieldValidator fValidator = null;
-            String fullFieldName = null;
+                FieldValidator fValidator = null;
+                String fullFieldName = null;
 
-            if (validator instanceof FieldValidator) {
-                fValidator = (FieldValidator) validator;
-                fullFieldName = fValidator.getValidatorContext().getFullFieldName(fValidator.getFieldName());
+                if (validator instanceof FieldValidator) {
+                    fValidator = (FieldValidator) validator;
+                    fullFieldName = fValidator.getValidatorContext().getFullFieldName(fValidator.getFieldName());
 
-                if ((shortcircuitedFields != null) && shortcircuitedFields.contains(fullFieldName)) {
-                    if (LOG.isDebugEnabled()) {
-                        LOG.debug("Short-circuited, skipping");
-                    }
+                    if ((shortcircuitedFields != null) && shortcircuitedFields.contains(fullFieldName)) {
+                        if (LOG.isDebugEnabled()) {
+                            LOG.debug("Short-circuited, skipping");
+                        }
 
-                    continue;
+                        continue;
+                    }
                 }
-            }
 
-            if (validator instanceof ShortCircuitableValidator && ((ShortCircuitableValidator) validator).isShortCircuit()) {
-                // get number of existing errors
-                List errs = null;
+                if (validator instanceof ShortCircuitableValidator && ((ShortCircuitableValidator) validator).isShortCircuit()) {
+                    // get number of existing errors
+                    List errs = null;
 
-                if (fValidator != null) {
-                    if (validatorContext.hasFieldErrors()) {
-                        Collection fieldErrors = (Collection) validatorContext.getFieldErrors().get(fullFieldName);
+                    if (fValidator != null) {
+                        if (validatorContext.hasFieldErrors()) {
+                            Collection fieldErrors = (Collection) validatorContext.getFieldErrors().get(fullFieldName);
 
-                        if (fieldErrors != null) {
-                            errs = new ArrayList(fieldErrors);
+                            if (fieldErrors != null) {
+                                errs = new ArrayList(fieldErrors);
+                            }
                         }
-                    }
-                } else if (validatorContext.hasActionErrors()) {
-                    Collection actionErrors = validatorContext.getActionErrors();
+                    } else if (validatorContext.hasActionErrors()) {
+                        Collection actionErrors = validatorContext.getActionErrors();
 
-                    if (actionErrors != null) {
-                        errs = new ArrayList(actionErrors);
+                        if (actionErrors != null) {
+                            errs = new ArrayList(actionErrors);
+                        }
                     }
-                }
 
-                validator.validate(object);
+                    validator.validate(object);
 
-                if (fValidator != null) {
-                    if (validatorContext.hasFieldErrors()) {
-                        Collection errCol = (Collection) validatorContext.getFieldErrors().get(fullFieldName);
+                    if (fValidator != null) {
+                        if (validatorContext.hasFieldErrors()) {
+                            Collection errCol = (Collection) validatorContext.getFieldErrors().get(fullFieldName);
 
-                        if ((errCol != null) && !errCol.equals(errs)) {
-                            if (LOG.isDebugEnabled()) {
-                                LOG.debug("Short-circuiting on field validation");
+                            if ((errCol != null) && !errCol.equals(errs)) {
+                                if (LOG.isDebugEnabled()) {
+                                    LOG.debug("Short-circuiting on field validation");
+                                }
+
+                                if (shortcircuitedFields == null) {
+                                    shortcircuitedFields = new TreeSet();
+                                }
+
+                                shortcircuitedFields.add(fullFieldName);
                             }
+                        }
+                    } else if (validatorContext.hasActionErrors()) {
+                        Collection errCol = validatorContext.getActionErrors();
 
-                            if (shortcircuitedFields == null) {
-                                shortcircuitedFields = new TreeSet();
+                        if ((errCol != null) && !errCol.equals(errs)) {
+                            if (LOG.isDebugEnabled()) {
+                                LOG.debug("Short-circuiting");
                             }
 
-                            shortcircuitedFields.add(fullFieldName);
+                            break;
                         }
                     }
-                } else if (validatorContext.hasActionErrors()) {
-                    Collection errCol = validatorContext.getActionErrors();
 
-                    if ((errCol != null) && !errCol.equals(errs)) {
-                        if (LOG.isDebugEnabled()) {
-                            LOG.debug("Short-circuiting");
-                        }
-
-                        break;
-                    }
+                    continue;
                 }
 
-                continue;
+                validator.validate(object);
+            }
+            finally {
+                validator.setValidatorContext(null);
             }
-
-            validator.validate(object);
         }
     }
 

File tiger/src/java/com/opensymphony/xwork/validator/AnnotationActionValidatorManager.java

         List<Validator> validators = getValidators(object.getClass(), context);
         Set<String> shortcircuitedFields = null;
 
-        for (Validator validator: validators) {
-            validator.setValidatorContext(validatorContext);
+        for (final Validator validator: validators) {
+            try {
+                validator.setValidatorContext(validatorContext);
 
-            if (LOG.isDebugEnabled()) {
-                LOG.debug("Running validator: " + validator + " for object " + object);
-            }
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Running validator: " + validator + " for object " + object);
+                }
 
-            FieldValidator fValidator = null;
-            String fullFieldName = null;
+                FieldValidator fValidator = null;
+                String fullFieldName = null;
 
-            if (validator instanceof FieldValidator) {
-                fValidator = (FieldValidator) validator;
-                fullFieldName = fValidator.getValidatorContext().getFullFieldName(fValidator.getFieldName());
+                if (validator instanceof FieldValidator) {
+                    fValidator = (FieldValidator) validator;
+                    fullFieldName = fValidator.getValidatorContext().getFullFieldName(fValidator.getFieldName());
 
-                if ((shortcircuitedFields != null) && shortcircuitedFields.contains(fullFieldName)) {
-                    if (LOG.isDebugEnabled()) {
-                        LOG.debug("Short-circuited, skipping");
-                    }
+                    if ((shortcircuitedFields != null) && shortcircuitedFields.contains(fullFieldName)) {
+                        if (LOG.isDebugEnabled()) {
+                            LOG.debug("Short-circuited, skipping");
+                        }
 
-                    continue;
+                        continue;
+                    }
                 }
-            }
 
-            if (validator instanceof ShortCircuitableValidator && ((ShortCircuitableValidator) validator).isShortCircuit())
-            {
-                // get number of existing errors
-                List<String> errs = null;
+                if (validator instanceof ShortCircuitableValidator && ((ShortCircuitableValidator) validator).isShortCircuit())
+                {
+                    // get number of existing errors
+                    List<String> errs = null;
 
-                if (fValidator != null) {
-                    if (validatorContext.hasFieldErrors()) {
-                        Collection<String> fieldErrors = (Collection<String>) validatorContext.getFieldErrors().get(fullFieldName);
+                    if (fValidator != null) {
+                        if (validatorContext.hasFieldErrors()) {
+                            Collection<String> fieldErrors = (Collection<String>) validatorContext.getFieldErrors().get(fullFieldName);
 
-                        if (fieldErrors != null) {
-                            errs = new ArrayList<String>(fieldErrors);
+                            if (fieldErrors != null) {
+                                errs = new ArrayList<String>(fieldErrors);
+                            }
                         }
-                    }
-                } else if (validatorContext.hasActionErrors()) {
-                    Collection<String> actionErrors = validatorContext.getActionErrors();
+                    } else if (validatorContext.hasActionErrors()) {
+                        Collection<String> actionErrors = validatorContext.getActionErrors();
 
-                    if (actionErrors != null) {
-                        errs = new ArrayList<String>(actionErrors);
+                        if (actionErrors != null) {
+                            errs = new ArrayList<String>(actionErrors);
+                        }
                     }
-                }
 
-                validator.validate(object);
+                    validator.validate(object);
 
-                if (fValidator != null) {
-                    if (validatorContext.hasFieldErrors()) {
-                        Collection<String> errCol = (Collection<String>) validatorContext.getFieldErrors().get(fullFieldName);
+                    if (fValidator != null) {
+                        if (validatorContext.hasFieldErrors()) {
+                            Collection<String> errCol = (Collection<String>) validatorContext.getFieldErrors().get(fullFieldName);
 
-                        if ((errCol != null) && !errCol.equals(errs)) {
-                            if (LOG.isDebugEnabled()) {
-                                LOG.debug("Short-circuiting on field validation");
+                            if ((errCol != null) && !errCol.equals(errs)) {
+                                if (LOG.isDebugEnabled()) {
+                                    LOG.debug("Short-circuiting on field validation");
+                                }
+
+                                if (shortcircuitedFields == null) {
+                                    shortcircuitedFields = new TreeSet<String>();
+                                }
+
+                                shortcircuitedFields.add(fullFieldName);
                             }
+                        }
+                    } else if (validatorContext.hasActionErrors()) {
+                        Collection<String> errCol = validatorContext.getActionErrors();
 
-                            if (shortcircuitedFields == null) {
-                                shortcircuitedFields = new TreeSet<String>();
+                        if ((errCol != null) && !errCol.equals(errs)) {
+                            if (LOG.isDebugEnabled()) {
+                                LOG.debug("Short-circuiting");
                             }
 
-                            shortcircuitedFields.add(fullFieldName);
+                            break;
                         }
                     }
-                } else if (validatorContext.hasActionErrors()) {
-                    Collection<String> errCol = validatorContext.getActionErrors();
-
-                    if ((errCol != null) && !errCol.equals(errs)) {
-                        if (LOG.isDebugEnabled()) {
-                            LOG.debug("Short-circuiting");
-                        }
 
-                        break;
-                    }
+                    continue;
                 }
 
-                continue;
+                validator.validate(object);
+            } finally {
+                validator.setValidatorContext( null );
             }
 
-            validator.validate(object);
         }
     }