myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lu4...@apache.org
Subject svn commit: r1212537 - /myfaces/core/branches/2.0.x/api/src/main/java/javax/faces/validator/BeanValidator.java
Date Fri, 09 Dec 2011 17:41:07 GMT
Author: lu4242
Date: Fri Dec  9 17:41:06 2011
New Revision: 1212537

URL: http://svn.apache.org/viewvc?rev=1212537&view=rev
Log:
MYFACES-3416 Default ValidatorFactory is not stored on application map

Modified:
    myfaces/core/branches/2.0.x/api/src/main/java/javax/faces/validator/BeanValidator.java

Modified: myfaces/core/branches/2.0.x/api/src/main/java/javax/faces/validator/BeanValidator.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.0.x/api/src/main/java/javax/faces/validator/BeanValidator.java?rev=1212537&r1=1212536&r2=1212537&view=diff
==============================================================================
--- myfaces/core/branches/2.0.x/api/src/main/java/javax/faces/validator/BeanValidator.java
(original)
+++ myfaces/core/branches/2.0.x/api/src/main/java/javax/faces/validator/BeanValidator.java
Fri Dec  9 17:41:06 2011
@@ -94,7 +94,8 @@ public class BeanValidator implements Va
      * Explicitly adding a BeanValidator to an UIInput is possible though.
      */
     @JSFWebConfigParam(defaultValue="true", expectedValues="true, false", since="2.0", group="validation")
-    public static final String DISABLE_DEFAULT_BEAN_VALIDATOR_PARAM_NAME = "javax.faces.validator.DISABLE_DEFAULT_BEAN_VALIDATOR";
+    public static final String DISABLE_DEFAULT_BEAN_VALIDATOR_PARAM_NAME
+            = "javax.faces.validator.DISABLE_DEFAULT_BEAN_VALIDATOR";
 
     /**
      * The key in the ServletContext where the Bean Validation Factory can be found.
@@ -115,6 +116,8 @@ public class BeanValidator implements Va
      * Currently, a string containing only whitespace is classified as empty.
      */
     public static final String EMPTY_VALIDATION_GROUPS_PATTERN = "^[\\W,]*$";
+    
+    private static final Class<?>[] DEFAULT_VALIDATION_GROUPS_ARRAY = new Class<?>[]
{ Default.class };
 
     private String validationGroups;
 
@@ -127,10 +130,17 @@ public class BeanValidator implements Va
     /**
      * {@inheritDoc}
      */
-    public void validate(final FacesContext context, final UIComponent component, final Object
value) throws ValidatorException
+    public void validate(final FacesContext context, final UIComponent component, final Object
value)
+            throws ValidatorException
     {
-        if (context == null) throw new NullPointerException("context");
-        if (component == null) throw new NullPointerException("component");
+        if (context == null)
+        {
+            throw new NullPointerException("context");
+        }
+        if (component == null)
+        {
+            throw new NullPointerException("component");
+        }
 
         ValueExpression valueExpression = component.getValueExpression("value");
         if (valueExpression == null)
@@ -141,18 +151,18 @@ public class BeanValidator implements Va
         }
 
         // Obtain a reference to the to-be-validated object and the property name.
-        final _ValueReferenceWrapper reference = getValueReference(valueExpression, context);
+        _ValueReferenceWrapper reference = getValueReference(valueExpression, context);
         if (reference == null)
         {
             return;
         }
-        final Object base = reference.getBase();
+        Object base = reference.getBase();
         if (base == null)
         {
             return;
         }
         
-        final Class<?> valueBaseClass = base.getClass();
+        Class<?> valueBaseClass = base.getClass();
         if (valueBaseClass == null)
         {
             return;
@@ -166,38 +176,38 @@ public class BeanValidator implements Va
             // can exit bean validation here
             return;
         }
-        final String valueProperty = (String) referenceProperty;
+        String valueProperty = (String) referenceProperty;
 
         // Initialize Bean Validation.
-        final ValidatorFactory validatorFactory = createValidatorFactory(context);
-        final javax.validation.Validator validator = createValidator(validatorFactory);
-        final BeanDescriptor beanDescriptor = validator.getConstraintsForClass(valueBaseClass);
+        ValidatorFactory validatorFactory = createValidatorFactory(context);
+        javax.validation.Validator validator = createValidator(validatorFactory, context);
+        BeanDescriptor beanDescriptor = validator.getConstraintsForClass(valueBaseClass);
         if (!beanDescriptor.isBeanConstrained())
         {
             return;
         }
         
         // Note that validationGroupsArray was initialized when createValidator was called
-        final Class[] validationGroupsArray = this.validationGroupsArray;
+        Class[] validationGroupsArray = this.validationGroupsArray;
 
         // Delegate to Bean Validation.
-        final Set constraintViolations = validator.validateValue(valueBaseClass, valueProperty,
value, validationGroupsArray);
+        Set constraintViolations = validator.validateValue(valueBaseClass, valueProperty,
value, validationGroupsArray);
         if (!constraintViolations.isEmpty())
         {
-            final Set<FacesMessage> messages = new LinkedHashSet<FacesMessage>(constraintViolations.size());
+            Set<FacesMessage> messages = new LinkedHashSet<FacesMessage>(constraintViolations.size());
             for (Object violation: constraintViolations)
             {
-                final ConstraintViolation constraintViolation = (ConstraintViolation) violation;
-                final String message = constraintViolation.getMessage();
-                final Object[] args = new Object[]{ message, _MessageUtils.getLabel(context,
component) };
-                final FacesMessage msg = _MessageUtils.getErrorMessage(context, MESSAGE_ID,
args);
+                ConstraintViolation constraintViolation = (ConstraintViolation) violation;
+                String message = constraintViolation.getMessage();
+                Object[] args = new Object[]{ message, _MessageUtils.getLabel(context, component)
};
+                FacesMessage msg = _MessageUtils.getErrorMessage(context, MESSAGE_ID, args);
                 messages.add(msg);
             }
             throw new ValidatorException(messages);
         }
     }
 
-    private javax.validation.Validator createValidator(final ValidatorFactory validatorFactory)
+    private javax.validation.Validator createValidator(final ValidatorFactory validatorFactory,
FacesContext context)
     {
         // Set default validation group when setValidationGroups has not been called.
         // The null check is there to prevent it from happening twice.
@@ -208,7 +218,7 @@ public class BeanValidator implements Va
 
         return validatorFactory //
                 .usingContext() //
-                .messageInterpolator(_FacesMessageInterpolatorHolder.get(validatorFactory))
//
+                .messageInterpolator(new FacesMessageInterpolator(validatorFactory.getMessageInterpolator(),
context)) //
                 .getValidator();
 
     }
@@ -226,13 +236,13 @@ public class BeanValidator implements Va
     private _ValueReferenceWrapper getValueReference(
             final ValueExpression valueExpression, final FacesContext context)
     {
-        final ELContext elCtx = context.getELContext();
+        ELContext elCtx = context.getELContext();
         if (_ExternalSpecifications.isUnifiedELAvailable())
         {
             // unified el 2.2 is available --> we can use ValueExpression.getValueReference()
             // we can't access ValueExpression.getValueReference() directly here, because
             // Class loading would fail in applications with el-api versions prior to 2.2
-            final _ValueReferenceWrapper wrapper = _BeanValidatorUELUtils.getUELValueReferenceWrapper(valueExpression,
elCtx);
+            _ValueReferenceWrapper wrapper = _BeanValidatorUELUtils.getUELValueReferenceWrapper(valueExpression,
elCtx);
             if (wrapper != null)
             {
                 if (wrapper.getProperty() == null)
@@ -285,8 +295,8 @@ public class BeanValidator implements Va
             {
                 if (_ExternalSpecifications.isBeanValidationAvailable())
                 {
-                    final ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
-                    applicationMap.put(VALIDATOR_FACTORY_KEY, attr);
+                    ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
+                    applicationMap.put(VALIDATOR_FACTORY_KEY, factory);
                     return factory;
                 }
                 else
@@ -305,12 +315,12 @@ public class BeanValidator implements Va
     {
         if (this.validationGroups == null || this.validationGroups.matches(EMPTY_VALIDATION_GROUPS_PATTERN))
         {
-            this.validationGroupsArray = new Class<?>[] { Default.class };
+            this.validationGroupsArray = DEFAULT_VALIDATION_GROUPS_ARRAY;
         }
         else
         {
-            final String[] classes = this.validationGroups.split(VALIDATION_GROUPS_DELIMITER);
-            final List<Class<?>> validationGroupsList = new ArrayList<Class<?>>(classes.length);
+            String[] classes = this.validationGroups.split(VALIDATION_GROUPS_DELIMITER);
+            List<Class<?>> validationGroupsList = new ArrayList<Class<?>>(classes.length);
 
             for (String clazz : classes)
             {
@@ -444,54 +454,30 @@ public class BeanValidator implements Va
     {
         _initialStateMarked = true;
     }
-}
-
-/**
- * Holder class to prevent NoClassDefFoundError in environments without Bean Validation.
- *
- * This is needed, because holder classes are loaded lazily. This means that when it's not
- * used, it will not be loaded, parsed and initialized. The BeanValidator class is used always,
- * so the MessageInterpolator references need to be in this separate class.
- */
-final class _FacesMessageInterpolatorHolder
-{
-    // Needs to be volatile.
-    private static volatile FacesMessageInterpolator instance;
-
-    /**
-     * Helper method for initializing the FacesMessageInterpolator.
-     *
-     * It uses the "Single Check Idiom" as described in Joshua Bloch's Effective Java 2nd
Edition.
-     *
-     * @param validatorFactory Used to obtain the MessageInterpolator.
-     * @return The instantiated MessageInterpolator for BeanValidator.
-     */
-    static MessageInterpolator get(final ValidatorFactory validatorFactory)
-    {
-        _FacesMessageInterpolatorHolder.FacesMessageInterpolator ret = instance;
-        if (ret == null)
-        {
-            final MessageInterpolator interpolator = validatorFactory.getMessageInterpolator();
-            instance = ret = new FacesMessageInterpolator(interpolator);
-        }
-        return ret;
-    }
-
+    
     /**
+     * Note: Before 2.1.5/2.0.11 there was another strategy for this point to minimize
+     * the instances used, but after checking this with a profiler, it is more expensive
to
+     * call FacesContext.getCurrentInstance() than create this object for bean validation.
+     * 
      * Standard MessageInterpolator, as described in the JSR-314 spec.
+     * 
+     * @author Leonardo Uribe
      */
     private static class FacesMessageInterpolator implements MessageInterpolator
     {
+        private final FacesContext facesContext;
         private final MessageInterpolator interpolator;
 
-        FacesMessageInterpolator(final MessageInterpolator interpolator)
+        public FacesMessageInterpolator(final MessageInterpolator interpolator, final FacesContext
facesContext)
         {
             this.interpolator = interpolator;
+            this.facesContext = facesContext;
         }
 
         public String interpolate(final String s, final Context context)
         {
-            final Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
+            Locale locale = facesContext.getViewRoot().getLocale();
             return interpolator.interpolate(s, context, locale);
         }
 
@@ -509,7 +495,8 @@ final class _FacesMessageInterpolatorHol
  */
 final class _ValueReferenceWrapper
 {
-    private final Object base, property;
+    private final Object base;
+    private final Object property;
 
     /**
      * Full constructor.
@@ -579,8 +566,8 @@ final class _ValueReferenceResolver exte
      */
     public static _ValueReferenceWrapper resolve(ValueExpression valueExpression, final ELContext
elCtx)
     {
-        final _ValueReferenceResolver resolver = new _ValueReferenceResolver(elCtx.getELResolver());
-        final ELContext elCtxDecorator = new _ELContextDecorator(elCtx, resolver);
+        _ValueReferenceResolver resolver = new _ValueReferenceResolver(elCtx.getELResolver());
+        ELContext elCtxDecorator = new _ELContextDecorator(elCtx, resolver);
         
         valueExpression.getValue(elCtxDecorator);
         
@@ -612,11 +599,30 @@ final class _ValueReferenceResolver exte
     }
 
     // ############################ Standard delegating implementations ############################
-    public final Class<?> getType(final ELContext ctx, final Object base, final Object
property){return resolver.getType(ctx, base, property);}
-    public final void setValue(final ELContext ctx, final Object base, final Object property,
final Object value){resolver.setValue(ctx, base, property, value);}
-    public final boolean isReadOnly(final ELContext ctx, final Object base, final Object
property){return resolver.isReadOnly(ctx, base, property);}
-    public final Iterator<FeatureDescriptor> getFeatureDescriptors(final ELContext
ctx, final Object base){return resolver.getFeatureDescriptors(ctx, base);}
-    public final Class<?> getCommonPropertyType(final ELContext ctx, final Object base){return
resolver.getCommonPropertyType(ctx, base);}
+    public final Class<?> getType(final ELContext ctx, final Object base, final Object
property)
+    {
+        return resolver.getType(ctx, base, property);
+    }
+
+    public final void setValue(final ELContext ctx, final Object base, final Object property,
final Object value)
+    {
+        resolver.setValue(ctx, base, property, value);
+    }
+
+    public final boolean isReadOnly(final ELContext ctx, final Object base, final Object
property)
+    {
+        return resolver.isReadOnly(ctx, base, property);
+    }
+
+    public final Iterator<FeatureDescriptor> getFeatureDescriptors(final ELContext
ctx, final Object base)
+    {
+        return resolver.getFeatureDescriptors(ctx, base);
+    }
+
+    public final Class<?> getCommonPropertyType(final ELContext ctx, final Object base)
+    {
+        return resolver.getCommonPropertyType(ctx, base);
+    }
 }
 
 /**
@@ -651,12 +657,43 @@ final class _ELContextDecorator extends 
     }
 
     // ############################ Standard delegating implementations ############################
-    public final FunctionMapper getFunctionMapper(){return ctx.getFunctionMapper();}
-    public final VariableMapper getVariableMapper(){return ctx.getVariableMapper();}
-    public final void setPropertyResolved(final boolean resolved){ctx.setPropertyResolved(resolved);}
-    public final boolean isPropertyResolved(){return ctx.isPropertyResolved();}
-    public final void putContext(final Class key, Object contextObject){ctx.putContext(key,
contextObject);}
-    public final Object getContext(final Class key){return ctx.getContext(key);}
-    public final Locale getLocale(){return ctx.getLocale();}
-    public final void setLocale(final Locale locale){ctx.setLocale(locale);}
+    public final FunctionMapper getFunctionMapper()
+    {
+        return ctx.getFunctionMapper();
+    }
+
+    public final VariableMapper getVariableMapper()
+    {
+        return ctx.getVariableMapper();
+    }
+
+    public final void setPropertyResolved(final boolean resolved)
+    {
+        ctx.setPropertyResolved(resolved);
+    }
+
+    public final boolean isPropertyResolved()
+    {
+        return ctx.isPropertyResolved();
+    }
+
+    public final void putContext(final Class key, Object contextObject)
+    {
+        ctx.putContext(key, contextObject);
+    }
+
+    public final Object getContext(final Class key)
+    {
+        return ctx.getContext(key);
+    }
+
+    public final Locale getLocale()
+    {
+        return ctx.getLocale();
+    }
+
+    public final void setLocale(final Locale locale)
+    {
+        ctx.setLocale(locale);
+    }
 }



Mime
View raw message