You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ta...@apache.org on 2020/02/17 17:05:25 UTC

[myfaces] branch master updated: synchronize startup code to avoid duplicate logging on concurrent requests

This is an automated email from the ASF dual-hosted git repository.

tandraschko pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/myfaces.git


The following commit(s) were added to refs/heads/master by this push:
     new 0ded358  synchronize startup code to avoid duplicate logging on concurrent requests
0ded358 is described below

commit 0ded358557b08d244c2576e3b9f5140ba03f3d7d
Author: Thomas Andraschko <ta...@apache.org>
AuthorDate: Mon Feb 17 18:03:28 2020 +0100

    synchronize startup code to avoid duplicate logging on concurrent requests
---
 .../myfaces/application/ApplicationImpl.java       |  59 +++------
 .../myfaces/util/ExternalSpecifications.java       | 144 +++++++++++----------
 ...FacesConfiguratorDefaultValidatorsTestCase.java |   6 +-
 .../tag/jsf/core/ValidateBeanTestCase.java         |   6 +-
 4 files changed, 107 insertions(+), 108 deletions(-)

diff --git a/impl/src/main/java/org/apache/myfaces/application/ApplicationImpl.java b/impl/src/main/java/org/apache/myfaces/application/ApplicationImpl.java
index 36d2e5e..6857562 100755
--- a/impl/src/main/java/org/apache/myfaces/application/ApplicationImpl.java
+++ b/impl/src/main/java/org/apache/myfaces/application/ApplicationImpl.java
@@ -126,6 +126,7 @@ import org.apache.myfaces.lifecycle.LifecycleImpl;
 import org.apache.myfaces.config.MyfacesConfig;
 import org.apache.myfaces.util.lang.Assert;
 import org.apache.myfaces.util.lang.ClassUtils;
+import org.apache.myfaces.util.lang.Lazy;
 import org.apache.myfaces.util.lang.StringUtils;
 import org.apache.myfaces.view.facelets.FaceletCompositionContext;
 import org.apache.myfaces.view.facelets.el.ELText;
@@ -194,7 +195,7 @@ public class ApplicationImpl extends Application
     private final RuntimeConfig _runtimeConfig;
     private final MyfacesConfig _myfacesConfig;
 
-    private ELResolver elResolver;
+    private Lazy<ELResolver> elResolver;
 
     private ELResolverBuilder resolverBuilderForFaces;
 
@@ -279,6 +280,23 @@ public class ApplicationImpl extends Application
         {
             _dateTimeConverterDefaultTimeZoneIsSystemTimeZone = true;
         }
+        
+        elResolver = new Lazy<>(() -> {
+            CompositeELResolver celr;
+            if (_myfacesConfig.isSupportJSP())
+            {
+                celr = new FacesCompositeELResolver(Scope.Faces);
+            }
+            else
+            {
+                celr = new CompositeELResolver();
+            }
+
+            new ELResolverBuilderForFaces(_runtimeConfig, _myfacesConfig)
+                    .build(getFacesContext(), celr);
+
+            return celr;
+        });
     }
 
     // ~ Methods
@@ -340,44 +358,7 @@ public class ApplicationImpl extends Application
     @Override
     public final ELResolver getELResolver()
     {
-        // we don't need synchronization here since it is ok to have multiple
-        // instances of the elresolver
-        if (elResolver == null)
-        {
-            elResolver = createFacesResolver();
-        }
-        return elResolver;
-    }
-
-    private ELResolver createFacesResolver()
-    {
-        FacesContext facesContext = getFacesContext();
-        boolean supportJSP = _myfacesConfig.isSupportJSP();
-        CompositeELResolver resolver;
-        if (supportJSP)
-        {
-            resolver = new FacesCompositeELResolver(Scope.Faces);
-        }
-        else
-        {
-            resolver = new CompositeELResolver();
-        }
-        getResolverBuilderForFaces().build(facesContext, resolver);
-        return resolver;
-    }
-
-    protected final ELResolverBuilder getResolverBuilderForFaces()
-    {
-        if (resolverBuilderForFaces == null)
-        {
-            resolverBuilderForFaces = new ELResolverBuilderForFaces(_runtimeConfig, _myfacesConfig);
-        }
-        return resolverBuilderForFaces;
-    }
-
-    public final void setResolverBuilderForFaces(final ELResolverBuilder factory)
-    {
-        resolverBuilderForFaces = factory;
+        return elResolver.get();
     }
 
     @Override
diff --git a/impl/src/main/java/org/apache/myfaces/util/ExternalSpecifications.java b/impl/src/main/java/org/apache/myfaces/util/ExternalSpecifications.java
index d1cb571..292c755 100644
--- a/impl/src/main/java/org/apache/myfaces/util/ExternalSpecifications.java
+++ b/impl/src/main/java/org/apache/myfaces/util/ExternalSpecifications.java
@@ -25,6 +25,7 @@ import java.util.logging.Logger;
 import javax.faces.context.ExternalContext;
 import javax.servlet.http.HttpServletRequest;
 import javax.validation.Validation;
+import org.apache.myfaces.util.lang.Lazy;
 import org.apache.myfaces.webapp.AbstractFacesInitializer;
 
 /**
@@ -42,58 +43,92 @@ public final class ExternalSpecifications
 {
     private static final Logger log = Logger.getLogger(ExternalSpecifications.class.getName());
 
-    private static volatile Boolean beanValidationAvailable;
-    private static volatile Boolean cdiAvailable;
-    private static volatile Boolean el3Available;
-    private static volatile Boolean sevlet4Available;
-
-    /**
-     * This method determines if Bean Validation is present.
-     *
-     * Eager initialization is used for performance. This means Bean Validation binaries
-     * should not be added at runtime after this variable has been set.
-     * @return true if Bean Validation is available, false otherwise.
-     */
-    public static boolean isBeanValidationAvailable()
+    private static Lazy<Boolean> beanValidationAvailable = new Lazy<>(() ->
     {
-        if (beanValidationAvailable == null)
+        boolean available;
+        try
         {
             try
             {
+                available = (ClassUtils.classForName("javax.validation.Validation") != null);
+            }
+            catch(ClassNotFoundException e)
+            {
+                available = false;
+            }
+
+            if (available)
+            {
                 try
                 {
-                    beanValidationAvailable = (ClassUtils.classForName("javax.validation.Validation") != null);
+                    // Trial-error approach to check for Bean Validation impl existence.
+                    // If any Exception occurs here, we assume that Bean Validation is not available.
+                    // The cause may be anything, i.e. NoClassDef, config error...
+                    Validation.buildDefaultValidatorFactory().getValidator();
                 }
-                catch(ClassNotFoundException e)
+                catch (Throwable t)
                 {
-                    beanValidationAvailable = Boolean.FALSE;
+                    //log.log(Level.FINE, "Error initializing Bean Validation (could be normal)", t);
+                    available = false;
                 }
-
-                if (beanValidationAvailable)
-                {
-                    try
-                    {
-                        // Trial-error approach to check for Bean Validation impl existence.
-                        // If any Exception occurs here, we assume that Bean Validation is not available.
-                        // The cause may be anything, i.e. NoClassDef, config error...
-                        Validation.buildDefaultValidatorFactory().getValidator();
-                    }
-                    catch (Throwable t)
-                    {
-                        //log.log(Level.FINE, "Error initializing Bean Validation (could be normal)", t);
-                        beanValidationAvailable = false;
-                    }
-                }
-            }
-            catch (Throwable t)
-            {
-                log.log(Level.FINE, "Error loading class (could be normal)", t);
-                beanValidationAvailable = false;
             }
+        }
+        catch (Throwable t)
+        {
+            log.log(Level.FINE, "Error loading class (could be normal)", t);
+            available = false;
+        }
+
+        log.info("MyFaces Core Bean Validation support " + (available ? "enabled" : "disabled"));
+
+        return available;
+    });
+    
+    private static volatile Boolean cdiAvailable;
+    
+    private static Lazy<Boolean> el3Available  = new Lazy<>(() ->
+    {
+        boolean available;
+        try
+        {
+            available = ClassUtils.classForName("javax.el.StaticFieldELResolver") != null ;
+        }
+        catch (Throwable t)
+        {
+            available = false;
+        }
+        log.info("MyFaces Core EL 3.0 support " + (available ? "enabled" : "disabled"));
+
+        return available;
+    });
 
-            log.info("MyFaces Core Bean Validation support " + (beanValidationAvailable ? "enabled" : "disabled"));
+    private static Lazy<Boolean> sevlet4Available = new Lazy<>(() ->
+    {
+        boolean available;
+        try
+        {
+            available = ClassUtils.classForName("javax.servlet.http.PushBuilder") != null
+                    && HttpServletRequest.class.getMethod("newPushBuilder", (Class[]) null) != null;
         }
-        return beanValidationAvailable;
+        catch (Throwable t)
+        {
+            available = false;
+        }
+        log.info("MyFaces Core Servlet 4.0 support " + (available ? "enabled" : "disabled"));
+
+        return available;
+    });
+
+    /**
+     * This method determines if Bean Validation is present.
+     *
+     * Eager initialization is used for performance. This means Bean Validation binaries
+     * should not be added at runtime after this variable has been set.
+     * @return true if Bean Validation is available, false otherwise.
+     */
+    public static boolean isBeanValidationAvailable()
+    {
+        return beanValidationAvailable.get();
     }
     
     public static boolean isCDIAvailable(ExternalContext externalContext)
@@ -119,37 +154,12 @@ public final class ExternalSpecifications
     
     public static boolean isEL3Available()
     {
-        if (el3Available == null)
-        {
-            try
-            {
-                el3Available = ClassUtils.classForName("javax.el.StaticFieldELResolver") != null ;
-            }
-            catch (Throwable t)
-            {
-                el3Available = false;
-            }
-            log.info("MyFaces Core EL 3.0 support " + (el3Available ? "enabled" : "disabled"));
-        }
-        return el3Available;
+        return el3Available.get();
     }
 
     public static boolean isServlet4Available()
     {
-        if (sevlet4Available == null)
-        {
-            try
-            {
-                sevlet4Available = ClassUtils.classForName("javax.servlet.http.PushBuilder") != null
-                        && HttpServletRequest.class.getMethod("newPushBuilder", (Class[]) null) != null;
-            }
-            catch (Throwable t)
-            {
-                sevlet4Available = false;
-            }
-            log.info("MyFaces Core Servlet 4.0 support " + (sevlet4Available ? "enabled" : "disabled"));
-        }
-        return sevlet4Available;
+        return sevlet4Available.get();
     }
 
     /**
diff --git a/impl/src/test/java/org/apache/myfaces/config/FacesConfiguratorDefaultValidatorsTestCase.java b/impl/src/test/java/org/apache/myfaces/config/FacesConfiguratorDefaultValidatorsTestCase.java
index 1e5b14f..ac3a568 100644
--- a/impl/src/test/java/org/apache/myfaces/config/FacesConfiguratorDefaultValidatorsTestCase.java
+++ b/impl/src/test/java/org/apache/myfaces/config/FacesConfiguratorDefaultValidatorsTestCase.java
@@ -32,6 +32,7 @@ import org.apache.myfaces.application.ApplicationFactoryImpl;
 import org.apache.myfaces.test.base.junit4.AbstractJsfConfigurableMockTestCase;
 import org.apache.myfaces.test.mock.MockRenderKitFactory;
 import org.apache.myfaces.util.ExternalSpecifications;
+import org.apache.myfaces.util.lang.Lazy;
 import org.apache.myfaces.view.ViewDeclarationLanguageFactoryImpl;
 import org.junit.Assert;
 import org.junit.Test;
@@ -110,7 +111,10 @@ public class FacesConfiguratorDefaultValidatorsTestCase extends AbstractJsfConfi
         {
             Field field = ExternalSpecifications.class.getDeclaredField("beanValidationAvailable");
             field.setAccessible(true);
-            field.set(ExternalSpecifications.class, available);
+            field.set(ExternalSpecifications.class, new Lazy<>(() ->
+            {
+                return available;
+            }));
         }
         catch (Exception e)
         {
diff --git a/impl/src/test/java/org/apache/myfaces/view/facelets/tag/jsf/core/ValidateBeanTestCase.java b/impl/src/test/java/org/apache/myfaces/view/facelets/tag/jsf/core/ValidateBeanTestCase.java
index 5c4512f..890023c 100644
--- a/impl/src/test/java/org/apache/myfaces/view/facelets/tag/jsf/core/ValidateBeanTestCase.java
+++ b/impl/src/test/java/org/apache/myfaces/view/facelets/tag/jsf/core/ValidateBeanTestCase.java
@@ -27,6 +27,7 @@ import javax.faces.validator.BeanValidator;
 import javax.faces.validator.Validator;
 
 import org.apache.myfaces.util.ExternalSpecifications;
+import org.apache.myfaces.util.lang.Lazy;
 import org.apache.myfaces.view.facelets.FaceletTestCase;
 import org.junit.Assert;
 import org.junit.Test;
@@ -61,7 +62,10 @@ public class ValidateBeanTestCase extends FaceletTestCase
         {
             Field field = ExternalSpecifications.class.getDeclaredField("beanValidationAvailable");
             field.setAccessible(true);
-            field.set(ExternalSpecifications.class, available);
+            field.set(ExternalSpecifications.class, new Lazy<>(() ->
+            {
+                return available;
+            }));
         }
         catch (Exception e)
         {