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/04/29 08:12:22 UTC

[myfaces] branch 2.3.x updated: MYFACES-4334 backported 2.3-next changes

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

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


The following commit(s) were added to refs/heads/2.3.x by this push:
     new 973cdb7  MYFACES-4334 backported 2.3-next changes
973cdb7 is described below

commit 973cdb712eb4c86cb687f2257b41bfafc3a46573
Author: Thomas Andraschko <ta...@apache.org>
AuthorDate: Wed Apr 29 10:11:59 2020 +0200

    MYFACES-4334 backported 2.3-next changes
---
 .../myfaces/util/ExternalSpecifications.java       | 173 +++++++++++----------
 .../main/java/org/apache/myfaces/util/Lazy.java    |  77 +++++++++
 ...FacesConfiguratorDefaultValidatorsTestCase.java |   3 +-
 .../tag/jsf/core/ValidateBeanTestCase.java         |   3 +-
 4 files changed, 173 insertions(+), 83 deletions(-)

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 8e86121..978fb7a 100644
--- a/impl/src/main/java/org/apache/myfaces/util/ExternalSpecifications.java
+++ b/impl/src/main/java/org/apache/myfaces/util/ExternalSpecifications.java
@@ -23,6 +23,7 @@ import java.util.logging.Logger;
 
 import javax.faces.context.ExternalContext;
 import javax.servlet.http.HttpServletRequest;
+import org.apache.myfaces.shared.util.ClassUtils;
 import org.apache.myfaces.webapp.AbstractFacesInitializer;
 
 /**
@@ -40,114 +41,124 @@ 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 = (Class.forName("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...
+                    _ValidationUtils.tryBuildDefaultValidatorFactory();
                 }
-                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...
-                        _ValidationUtils.tryBuildDefaultValidatorFactory();
-                    }
-                    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"));
 
-            log.info("MyFaces Bean Validation support " + (beanValidationAvailable ? "enabled" : "disabled"));
+        return available;
+    });
+    
+    private static Lazy<Boolean> cdiAvailable = new Lazy<>(() ->
+    {
+        boolean available;
+        try
+        {
+            available = ClassUtils.classForName("javax.enterprise.inject.spi.BeanManager") != null;
         }
-        return beanValidationAvailable;
-    }
+        catch (Throwable t)
+        {
+            //log.log(Level.FINE, "Error loading class (could be normal)", t);
+            available = false;
+        }
+
+        log.info("MyFaces Core CDI support " + (available ? "enabled" : "disabled"));
+ 
+        return available;
+    });
     
-    public static boolean isCDIAvailable(ExternalContext externalContext)
+    private static Lazy<Boolean> el3Available = new Lazy<>(() ->
     {
-        if (cdiAvailable == null)
+        boolean available;
+        try
         {
-            try
-            {
-                cdiAvailable = Class.forName("javax.enterprise.inject.spi.BeanManager") != null;
-            }
-            catch (Throwable t)
-            {
-                //log.log(Level.FINE, "Error loading class (could be normal)", t);
-                cdiAvailable = false;
-            }
+            available = ClassUtils.classForName("javax.el.StaticFieldELResolver") != null ;
+        }
+        catch (Throwable t)
+        {
+            available = false;
+        }
+        log.info("MyFaces Core EL 3.0 support " + (available ? "enabled" : "disabled"));
 
-            log.info("MyFaces CDI support " + (cdiAvailable ? "enabled" : "disabled"));
+        return available;
+    });
+
+    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;
+        }
+        catch (Throwable t)
+        {
+            available = false;
         }
+        log.info("MyFaces Core Servlet 4.0 support " + (available ? "enabled" : "disabled"));
+
+        return available;
+    });
 
-        return cdiAvailable && 
+    /**
+     * 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)
+    {
+        return cdiAvailable.get() && 
                 externalContext.getApplicationMap().containsKey(AbstractFacesInitializer.CDI_BEAN_MANAGER_INSTANCE);
     }
     
     public static boolean isEL3Available()
     {
-        if (el3Available == null)
-        {
-            try
-            {
-                el3Available = Class.forName("javax.el.StaticFieldELResolver") != null ;
-            }
-            catch (Throwable t)
-            {
-                el3Available = false;
-            }
-            log.info("MyFaces EL 3.0 support " + (el3Available ? "enabled" : "disabled"));
-        }
-        return el3Available;
+        return el3Available.get();
     }
 
     public static boolean isServlet4Available()
     {
-        if (sevlet4Available == null)
-        {
-            try
-            {
-                sevlet4Available = Class.forName("javax.servlet.http.PushBuilder") != null
-                        && HttpServletRequest.class.getMethod("newPushBuilder", (Class[]) null) != null;
-            }
-            catch (Throwable t)
-            {
-                sevlet4Available = false;
-            }
-            log.info("MyFaces Servlet 4.0 support " + (sevlet4Available ? "enabled" : "disabled"));
-        }
-        return sevlet4Available;
+        return sevlet4Available.get();
     }
 
     /**
diff --git a/impl/src/main/java/org/apache/myfaces/util/Lazy.java b/impl/src/main/java/org/apache/myfaces/util/Lazy.java
new file mode 100644
index 0000000..6502a24
--- /dev/null
+++ b/impl/src/main/java/org/apache/myfaces/util/Lazy.java
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.util;
+
+import java.util.function.Supplier;
+
+/**
+ * Inspired by commons-lang LazyInitializer.
+ *
+ * @param <T> The type to be lazy initialized.
+ */
+public class Lazy<T>
+{
+
+    private static final Object NOT_INITIALIZED = new Object();
+
+    @SuppressWarnings("unchecked")
+    private volatile T value = (T) NOT_INITIALIZED;
+    private volatile Supplier<T> init;
+
+    public Lazy(Supplier<T> init)
+    {
+        this.init = init;
+    }
+    
+    public synchronized void reset(Supplier<T> init)
+    {
+        this.init = init;
+        this.value = (T) NOT_INITIALIZED;
+    }
+
+    public synchronized void reset(T value)
+    {
+        this.value = value;
+    }
+    
+    public T get()
+    {
+        T result = value;
+
+        if (result == NOT_INITIALIZED)
+        {
+            synchronized (this)
+            {
+                result = value;
+                if (result == NOT_INITIALIZED)
+                {
+                    value = init.get();
+                    result = value;
+                }
+            }
+        }
+
+        return result;
+    }
+
+    public boolean isInitialized()
+    {
+        return value != NOT_INITIALIZED;
+    }
+}
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..a4e2750 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.Lazy;
 import org.apache.myfaces.view.ViewDeclarationLanguageFactoryImpl;
 import org.junit.Assert;
 import org.junit.Test;
@@ -110,7 +111,7 @@ 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<>(() -> 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..3dded62 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.Lazy;
 import org.apache.myfaces.view.facelets.FaceletTestCase;
 import org.junit.Assert;
 import org.junit.Test;
@@ -61,7 +62,7 @@ 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<>(() -> available));
         }
         catch (Exception e)
         {