commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dgra...@apache.org
Subject cvs commit: jakarta-commons/validator/src/share/org/apache/commons/validator Validator.java
Date Sat, 17 Jan 2004 21:37:20 GMT
dgraham     2004/01/17 13:37:20

  Modified:    validator/src/share/org/apache/commons/validator
                        Validator.java
  Log:
  Changed exception handling for PR# 24369.  The framework will convert 
  checked exceptions into ValidatorExceptions so any ValidatorException 
  thrown out of the framework indicates a "system" exception that stops
  validation processing.  
  
  If a pluggable validation method throws a ValidatorException it will 
  be rethrown and passed out of the framework.  Any other exception 
  from a pluggable validation method is still considered a validation 
  failure rather than a system exception to maintain backwards
  compatibility.  This should be changed for Validator 2.0.
  
  Revision  Changes    Path
  1.32      +75 -42    jakarta-commons/validator/src/share/org/apache/commons/validator/Validator.java
  
  Index: Validator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/Validator.java,v
  retrieving revision 1.31
  retrieving revision 1.32
  diff -u -r1.31 -r1.32
  --- Validator.java	11 Jan 2004 23:30:20 -0000	1.31
  +++ Validator.java	17 Jan 2004 21:37:20 -0000	1.32
  @@ -396,7 +396,8 @@
       }
   
       /**
  -     * Executes the given ValidatorAction and all ValidatorActions that it depends on.
  +     * Executes the given ValidatorAction and all ValidatorActions that it 
  +     * depends on.
        * @return true if the validation succeeded.
        */
       private boolean validateFieldForRule(
  @@ -465,8 +466,8 @@
       }
   
       /**
  -     * Dynamically runs the validation method for this validator and returns true if the
  -     * data is valid.
  +     * Dynamically runs the validation method for this validator and returns 
  +     * true if the data is valid.
        * @param field
        * @param va
        * @param results
  @@ -474,27 +475,36 @@
        * @throws ValidatorException
        */
       private boolean executeValidationMethod(
  -            Field field,
  -            ValidatorAction va,
  -            ValidatorResults results,
  -            int pos)
  -            throws ValidatorException {
  +        Field field,
  +        ValidatorAction va,
  +        ValidatorResults results,
  +        int pos)
  +        throws ValidatorException {
  +
  +        // Add these two Objects to the resources since they reference
  +        // the current validator action and field
  +        this.setParameter(VALIDATOR_ACTION_PARAM, va);
  +        this.setParameter(FIELD_PARAM, field);
   
           try {
  -            // Add these two Objects to the resources since they reference
  -            // the current validator action and field
  -            this.setParameter(VALIDATOR_ACTION_PARAM, va);
  -            this.setParameter(FIELD_PARAM, field);
  -
  -            Class validationClass = getClassLoader().loadClass(va.getClassname());
  +            Class validationClass;
  +            try {
  +                validationClass = getClassLoader().loadClass(va.getClassname());
  +            } catch (ClassNotFoundException e) {
  +                throw new ValidatorException(e.getMessage());
  +            }
   
               List params = va.getMethodParamsList();
   
               Class[] paramClass = this.getParameterClasses(params);
               Object[] paramValue = this.getParameterValues(params);
   
  -            Method validationMethod =
  -                    validationClass.getMethod(va.getMethod(), paramClass);
  +            Method validationMethod;
  +            try {
  +                validationMethod = validationClass.getMethod(va.getMethod(), paramClass);
  +            } catch (NoSuchMethodException e) {
  +                throw new ValidatorException(e.getMessage());
  +            }
   
               // If the method is static, we don't need an instance of the class
               // to call the method.
  @@ -506,8 +516,22 @@
                   this.handleIndexedField(field, pos, params, paramValue);
               }
   
  -            Object result =
  -                    validationMethod.invoke(va.getClassnameInstance(), paramValue);
  +            Object result = null;
  +            try {
  +                result = validationMethod.invoke(va.getClassnameInstance(), paramValue);
  +            } catch (IllegalArgumentException e) {
  +                throw new ValidatorException(e.getMessage());
  +            } catch (IllegalAccessException e) {
  +                throw new ValidatorException(e.getMessage());
  +            } catch (InvocationTargetException e) {
  +                
  +                if (e.getTargetException() instanceof Exception) {
  +                    throw (Exception) e.getTargetException();
  +                
  +                } else if (e.getTargetException() instanceof Error) {
  +                    throw (Error) e.getTargetException();
  +                }
  +            }
   
               boolean valid = this.isValid(result);
               if (!valid || (valid && !this.onlyReturnErrors)) {
  @@ -518,14 +542,18 @@
                   return false;
               }
   
  -        } catch(Exception e) {
  -            log.error("reflection: " + e.getMessage(), e);
  -
  -            results.add(field, va.getName(), false);
  -
  +        // TODO This catch block remains for backward compatibility.  Remove
  +        // this for Validator 2.0 when exception scheme changes.
  +        } catch (Exception e) {
               if (e instanceof ValidatorException) {
                   throw (ValidatorException) e;
               }
  +            
  +            log.error(
  +                "Unhandled exception thrown during validation: " + e.getMessage(),
  +                e);
  +
  +            results.add(field, va.getName(), false);
               return false;
           }
   
  @@ -535,13 +563,13 @@
       /**
        * Converts a List of parameter class names into their Class objects.
        * @param paramNames
  -     * @return An array containing the Class object for each parameter.  This array
  -     * is in the same order as the given List and is suitable for passing to the validation
  -     * method.
  -     * @throws ClassNotFoundException
  +     * @return An array containing the Class object for each parameter.  This 
  +     * array is in the same order as the given List and is suitable for passing 
  +     * to the validation method.
  +     * @throws ValidatorException if a class cannot be loaded.
        */
       private Class[] getParameterClasses(List paramNames)
  -            throws ClassNotFoundException {
  +        throws ValidatorException {
   
           Class[] paramClass = new Class[paramNames.size()];
   
  @@ -549,19 +577,23 @@
               String paramClassName = (String) paramNames.get(i);
   
               // There were problems calling getClass on paramValue[]
  -            paramClass[i] = this.getClassLoader().loadClass(paramClassName);
  +            try {
  +                paramClass[i] = this.getClassLoader().loadClass(paramClassName);
  +            } catch (ClassNotFoundException e) {
  +                throw new ValidatorException(e.getMessage());
  +            }
           }
   
           return paramClass;
       }
   
       /**
  -     * Converts a List of parameter class names into their values contained in the
  -     * parameters Map.
  +     * Converts a List of parameter class names into their values contained in 
  +     * the parameters Map.
        * @param paramNames
  -     * @return An array containing the value object for each parameter.  This array
  -     * is in the same order as the given List and is suitable for passing to the validation
  -     * method.
  +     * @return An array containing the value object for each parameter.  This 
  +     * array is in the same order as the given List and is suitable for passing 
  +     * to the validation method.
        */
       private Object[] getParameterValues(List paramNames) {
   
  @@ -576,8 +608,8 @@
       }
   
       /**
  -     * If the given action doesn't already have an instance of the class, store a new
  -     * instance in the action.
  +     * If the given action doesn't already have an instance of the class, 
  +     * store a new instance in the action.
        * @param validationClass The pluggable validation class to store.
        * @param va The ValidatorAction to store the object in.
        */
  @@ -633,11 +665,12 @@
   
       /**
        * Run the validations on a given field, modifying the passed
  -     * ValidatorResults to add in any new errors found.  Run all the validations in
  -     * the depends clause over each item in turn, returning when the first one fails.
  +     * ValidatorResults to add in any new errors found.  Run all the 
  +     * validations in the depends clause over each item in turn, returning 
  +     * when the first one fails.
        */
       private void validateField(Field field, ValidatorResults allResults)
  -            throws ValidatorException {
  +        throws ValidatorException {
   
           int numberOfFieldsToValidate =
                   field.isIndexed() ? this.getIndexedProperty(field).length : 1;
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message