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/10/27 13:50:28 UTC

[myfaces] branch master updated: refactored

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 46433dd  refactored
46433dd is described below

commit 46433dd9f908a996829c41b513248ab82b0142c2
Author: Thomas Andraschko <ta...@apache.org>
AuthorDate: Tue Oct 27 14:50:19 2020 +0100

    refactored
---
 api/src/main/java/javax/faces/FactoryFinder.java   | 36 ++---------
 .../javax/faces/_FactoryFinderProviderFactory.java | 70 ++--------------------
 .../javax/faces/component/UIViewParameter.java     |  2 +-
 .../java/javax/faces/validator/BeanValidator.java  | 65 +++-----------------
 .../myfaces/core/api/shared/MessageUtils.java      | 20 +------
 .../core/api/shared/ValueReferenceResolver.java    | 16 ++++-
 .../component/validate/WholeBeanValidator.java     | 18 +-----
 .../java/org/apache/myfaces/util/WebXmlParser.java |  2 +-
 8 files changed, 39 insertions(+), 190 deletions(-)

diff --git a/api/src/main/java/javax/faces/FactoryFinder.java b/api/src/main/java/javax/faces/FactoryFinder.java
index 4719049..b14714b 100755
--- a/api/src/main/java/javax/faces/FactoryFinder.java
+++ b/api/src/main/java/javax/faces/FactoryFinder.java
@@ -47,6 +47,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import javax.faces.component.search.SearchExpressionContextFactory;
+import org.apache.myfaces.core.api.shared.lang.ClassUtils;
 
 /**
  * see Javadoc of <a href="http://java.sun.com/javaee/javaserverfaces/1.2/docs/api/index.html">JSF Specification</a>
@@ -258,7 +259,7 @@ public final class FactoryFinder
 
     private static Object _getFactory(String factoryName) throws FacesException
     {
-        ClassLoader classLoader = getClassLoader();
+        ClassLoader classLoader = ClassUtils.getContextClassLoader();
 
         // This code must be synchronized because this could cause a problem when
         // using update feature each time of myfaces (org.apache.myfaces.CONFIG_REFRESH_PERIOD)
@@ -440,7 +441,7 @@ public final class FactoryFinder
     {
         try
         {
-            Class<?> beanEntryClass = _FactoryFinderProviderFactory.classForName(BEAN_ENTRY_CLASS_NAME);
+            Class<?> beanEntryClass = ClassUtils.classForName(BEAN_ENTRY_CLASS_NAME);
             Constructor beanEntryConstructor = beanEntryClass.getDeclaredConstructor(Object.class, Object.class);
 
             Object result = beanEntryConstructor.newInstance(instance, creationMetaData);
@@ -584,7 +585,7 @@ public final class FactoryFinder
     {
         checkFactoryName(factoryName);
 
-        ClassLoader classLoader = getClassLoader();
+        ClassLoader classLoader = ClassUtils.getContextClassLoader();
         Map<String, List<String>> factoryClassNames = null;
         synchronized (registeredFactoryNames)
         {
@@ -656,7 +657,7 @@ public final class FactoryFinder
 
     private static void _releaseFactories() throws FacesException
     {
-        ClassLoader classLoader = getClassLoader();
+        ClassLoader classLoader = ClassUtils.getContextClassLoader();
 
         Map<String, Object> factoryMap;
         // This code must be synchronized
@@ -716,31 +717,4 @@ public final class FactoryFinder
             throw new IllegalArgumentException("factoryName '" + factoryName + '\'');
         }
     }
-
-    private static ClassLoader getClassLoader()
-    {
-        try
-        {
-            ClassLoader classLoader = null;
-            if (System.getSecurityManager() != null)
-            {
-                classLoader = (ClassLoader) AccessController.doPrivileged(
-                        (PrivilegedExceptionAction) () -> Thread.currentThread().getContextClassLoader());
-            }
-            else
-            {
-                classLoader = Thread.currentThread().getContextClassLoader();
-            }
-            
-            if (classLoader == null)
-            {
-                throw new FacesException("web application class loader cannot be identified", null);
-            }
-            return classLoader;
-        }
-        catch (Exception e)
-        {
-            throw new FacesException("web application class loader cannot be identified", e);
-        }
-    }
 }
diff --git a/api/src/main/java/javax/faces/_FactoryFinderProviderFactory.java b/api/src/main/java/javax/faces/_FactoryFinderProviderFactory.java
index 405dbd4..b8071f5 100644
--- a/api/src/main/java/javax/faces/_FactoryFinderProviderFactory.java
+++ b/api/src/main/java/javax/faces/_FactoryFinderProviderFactory.java
@@ -19,12 +19,10 @@
 package javax.faces;
 
 import java.lang.reflect.Method;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
-import java.security.PrivilegedExceptionAction;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import javax.faces.context.ExternalContext;
+import org.apache.myfaces.core.api.shared.lang.ClassUtils;
 
 /**
  * Provide utility methods used by FactoryFinder class to lookup for SPI interface FactoryFinderProvider.
@@ -83,7 +81,7 @@ class _FactoryFinderProviderFactory
 
         try
         {
-            factoryFinderFactoryClass = classForName(FACTORY_FINDER_PROVIDER_FACTORY_CLASS_NAME);
+            factoryFinderFactoryClass = ClassUtils.classForName(FACTORY_FINDER_PROVIDER_FACTORY_CLASS_NAME);
             if (factoryFinderFactoryClass != null)
             {
                 factoryFinderproviderFactoryGetMethod = factoryFinderFactoryClass.getMethod
@@ -92,7 +90,7 @@ class _FactoryFinderProviderFactory
                         .getMethod("getFactoryFinderProvider", null);
             }
 
-            factoryFinderProviderClass = classForName(FACTORY_FINDER_PROVIDER_CLASS_NAME);
+            factoryFinderProviderClass = ClassUtils.classForName(FACTORY_FINDER_PROVIDER_CLASS_NAME);
             if (factoryFinderProviderClass != null)
             {
                 factoryFinderProviderGetFactoryMethod = factoryFinderProviderClass.getMethod("getFactory",
@@ -103,7 +101,7 @@ class _FactoryFinderProviderFactory
                         ("releaseFactories", null);
             }
             
-            injectionProviderFactoryClass = classForName(INJECTION_PROVIDER_FACTORY_CLASS_NAME);
+            injectionProviderFactoryClass = ClassUtils.classForName(INJECTION_PROVIDER_FACTORY_CLASS_NAME);
             
             if (injectionProviderFactoryClass != null)
             {
@@ -113,7 +111,7 @@ class _FactoryFinderProviderFactory
                     getMethod("getInjectionProvider", ExternalContext.class);
             }
             
-            injectionProviderClass = classForName(INJECTION_PROVIDER_CLASS_NAME);
+            injectionProviderClass = ClassUtils.classForName(INJECTION_PROVIDER_CLASS_NAME);
             
             if (injectionProviderClass != null)
             {
@@ -171,62 +169,4 @@ class _FactoryFinderProviderFactory
         return null;
     }
 
-    // ~ Methods Copied from _ClassUtils
-    // ------------------------------------------------------------------------------------
-
-    /**
-     * Tries a Class.loadClass with the context class loader of the current thread first and automatically falls back
-     * to
-     * the ClassUtils class loader (i.e. the loader of the myfaces.jar lib) if necessary.
-     *
-     * @param type fully qualified name of a non-primitive non-array class
-     * @return the corresponding Class
-     * @throws NullPointerException   if type is null
-     * @throws ClassNotFoundException
-     */
-    public static Class<?> classForName(String type) throws ClassNotFoundException
-    {
-        if (type == null)
-        {
-            throw new NullPointerException("type");
-        }
-        try
-        {
-            // Try WebApp ClassLoader first
-            return Class.forName(type, false, // do not initialize for faster startup
-                    getContextClassLoader());
-        }
-        catch (ClassNotFoundException ignore)
-        {
-            // fallback: Try ClassLoader for ClassUtils (i.e. the myfaces.jar lib)
-            return Class.forName(type, false, // do not initialize for faster startup
-                    _FactoryFinderProviderFactory.class.getClassLoader());
-        }
-    }
-
-    /**
-     * Gets the ClassLoader associated with the current thread. Returns the class loader associated with the specified
-     * default object if no context loader is associated with the current thread.
-     *
-     * @return ClassLoader
-     */
-    protected static ClassLoader getContextClassLoader()
-    {
-        if (System.getSecurityManager() != null)
-        {
-            try
-            {
-                return (ClassLoader) AccessController.doPrivileged(
-                        (PrivilegedExceptionAction) () -> Thread.currentThread().getContextClassLoader());
-            }
-            catch (PrivilegedActionException pae)
-            {
-                throw new FacesException(pae);
-            }
-        }
-        else
-        {
-            return Thread.currentThread().getContextClassLoader();
-        }
-    }
 }
diff --git a/api/src/main/java/javax/faces/component/UIViewParameter.java b/api/src/main/java/javax/faces/component/UIViewParameter.java
index 58e58f1..d63fb13 100644
--- a/api/src/main/java/javax/faces/component/UIViewParameter.java
+++ b/api/src/main/java/javax/faces/component/UIViewParameter.java
@@ -273,7 +273,7 @@ public class UIViewParameter extends UIInput
             log.finest("releaseRenderer rendererMap -> " + delegateRendererMap.toString());
         }
 
-        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+        ClassLoader classLoader = ClassUtils.getContextClassLoader();
         
         if (log.isLoggable(Level.FINEST))
         {
diff --git a/api/src/main/java/javax/faces/validator/BeanValidator.java b/api/src/main/java/javax/faces/validator/BeanValidator.java
index 0d0a62b..7910b6a 100644
--- a/api/src/main/java/javax/faces/validator/BeanValidator.java
+++ b/api/src/main/java/javax/faces/validator/BeanValidator.java
@@ -20,9 +20,6 @@ package javax.faces.validator;
 
 import org.apache.myfaces.core.api.shared.MessageUtils;
 import org.apache.myfaces.core.api.shared.ExternalSpecifications;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
-import java.security.PrivilegedExceptionAction;
 import java.util.ArrayList;
 import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
@@ -31,7 +28,6 @@ import java.util.Map;
 import java.util.Set;
 import java.util.logging.Logger;
 
-import javax.el.ELContext;
 import javax.el.ValueExpression;
 import javax.el.ValueReference;
 import javax.faces.FacesException;
@@ -51,6 +47,7 @@ import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFValidat
 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam;
 import org.apache.myfaces.core.api.shared.FacesMessageInterpolator;
 import org.apache.myfaces.core.api.shared.ValueReferenceResolver;
+import org.apache.myfaces.core.api.shared.lang.ClassUtils;
 
 /**
  * <p>
@@ -158,7 +155,7 @@ public class BeanValidator implements Validator, PartialStateHolder
         }
 
         // Obtain a reference to the to-be-validated object and the property name.
-        ValueReference reference = getValueReference(valueExpression, context);
+        ValueReference reference = ValueReferenceResolver.resolve(valueExpression, context);
         if (reference == null)
         {
             return;
@@ -299,20 +296,6 @@ public class BeanValidator implements Validator, PartialStateHolder
     }
 
     /**
-     * Get the ValueReference from the ValueExpression.
-     *
-     * @param valueExpression The ValueExpression for value.
-     * @param context The FacesContext.
-     * @return A ValueReferenceWrapper with the necessary information about the ValueReference.
-     */
-    private ValueReference getValueReference(
-            final ValueExpression valueExpression, final FacesContext context)
-    {
-        ELContext elCtx = context.getELContext();
-        return ValueReferenceResolver.resolve(valueExpression, elCtx);
-    }
-
-    /**
      * This method creates ValidatorFactory instances or retrieves them from the container.
      *
      * Once created, ValidatorFactory instances are stored in the container under the key
@@ -369,44 +352,17 @@ public class BeanValidator implements Validator, PartialStateHolder
                 clazz = clazz.trim();
                 if (!clazz.isEmpty())
                 {
-                    Class<?> theClass = null;
-                    ClassLoader cl = null;
-                    if (System.getSecurityManager() != null) 
-                    {
-                        try 
-                        {
-                            cl = (ClassLoader) AccessController.doPrivileged(
-                                    (PrivilegedExceptionAction) () -> Thread.currentThread().getContextClassLoader());
-                        }
-                        catch (PrivilegedActionException pae)
-                        {
-                            throw new FacesException(pae);
-                        }
-                    }
-                    else
-                    {
-                        cl = Thread.currentThread().getContextClassLoader();
-                    }
-                    
                     try
-                    {                        
-                        // Try WebApp ClassLoader first
-                        theClass = Class.forName(clazz,false,cl);
+                    {
+                        Class<?> theClass = ClassUtils.classForName(clazz);
+
+                        // the class was found
+                        validationGroupsList.add(theClass);
                     }
-                    catch (ClassNotFoundException ignore)
+                    catch (ClassNotFoundException e)
                     {
-                        try
-                        {
-                            // fallback: Try ClassLoader for BeanValidator (i.e. the myfaces.jar lib)
-                            theClass = Class.forName(clazz,false, BeanValidator.class.getClassLoader());
-                        }
-                        catch (ClassNotFoundException e)
-                        {
-                            throw new RuntimeException("Could not load validation group", e);
-                        }                        
+                        throw new RuntimeException("Could not load validation group", e);                     
                     }
-                    // the class was found
-                    validationGroupsList.add(theClass);
                 }
             }
                     
@@ -461,8 +417,6 @@ public class BeanValidator implements Validator, PartialStateHolder
             // validationGroups to javax.validation.groups.Default. 
             this.validationGroups = null;
         }
-        // Only the String is saved, recalculate the Class[] on state restoration.
-        //postSetValidationGroups();
     }
 
     /**
@@ -484,7 +438,6 @@ public class BeanValidator implements Validator, PartialStateHolder
     {
         this.validationGroups = validationGroups;
         this.clearInitialState();
-        //postSetValidationGroups();
     }
 
     @JSFProperty
diff --git a/api/src/main/java/org/apache/myfaces/core/api/shared/MessageUtils.java b/api/src/main/java/org/apache/myfaces/core/api/shared/MessageUtils.java
index 1da0939..e5a9efe 100755
--- a/api/src/main/java/org/apache/myfaces/core/api/shared/MessageUtils.java
+++ b/api/src/main/java/org/apache/myfaces/core/api/shared/MessageUtils.java
@@ -18,18 +18,15 @@
  */
 package org.apache.myfaces.core.api.shared;
 
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
-import java.security.PrivilegedExceptionAction;
 import java.util.Locale;
 import java.util.MissingResourceException;
 import java.util.ResourceBundle;
 
 import javax.el.ValueExpression;
-import javax.faces.FacesException;
 import javax.faces.application.FacesMessage;
 import javax.faces.component.UIComponent;
 import javax.faces.context.FacesContext;
+import org.apache.myfaces.core.api.shared.lang.ClassUtils;
 
 public class MessageUtils
 {
@@ -220,16 +217,7 @@ public class MessageUtils
                 try
                 {
                     //Last resort is the context class loader
-                    ClassLoader cl;
-                    if (System.getSecurityManager() == null)
-                    {
-                        cl = Thread.currentThread().getContextClassLoader();
-                    }
-                    else
-                    {
-                        cl = (ClassLoader) AccessController.doPrivileged(
-                                (PrivilegedExceptionAction) () -> Thread.currentThread().getContextClassLoader());
-                    }
+                    ClassLoader cl = ClassUtils.getContextClassLoader();
 
                     if (bundleControl == null)
                     {
@@ -240,10 +228,6 @@ public class MessageUtils
                         return ResourceBundle.getBundle(bundleName, locale, cl, bundleControl);
                     }
                 }
-                catch(PrivilegedActionException pae)
-                {
-                    throw new FacesException(pae);
-                }
                 catch (MissingResourceException damned)
                 {
                     facesContext.getExternalContext().log("resource bundle " + bundleName + " could not be found");
diff --git a/api/src/main/java/org/apache/myfaces/core/api/shared/ValueReferenceResolver.java b/api/src/main/java/org/apache/myfaces/core/api/shared/ValueReferenceResolver.java
index 1a64156..d0b7a36 100644
--- a/api/src/main/java/org/apache/myfaces/core/api/shared/ValueReferenceResolver.java
+++ b/api/src/main/java/org/apache/myfaces/core/api/shared/ValueReferenceResolver.java
@@ -21,18 +21,32 @@ package org.apache.myfaces.core.api.shared;
 import javax.el.ELContext;
 import javax.el.ValueExpression;
 import javax.el.ValueReference;
+import javax.faces.context.FacesContext;
 import javax.faces.el.CompositeComponentExpressionHolder;
 
 public class ValueReferenceResolver
 {
     /**
+     * Get the ValueReference from the ValueExpression.
+     *
+     * @param valueExpression The ValueExpression for value.
+     * @param context The FacesContext.
+     * @return A ValueReferenceWrapper with the necessary information about the ValueReference.
+     */
+    public static ValueReference resolve(ValueExpression valueExpression, FacesContext context)
+    {
+        ELContext elCtx = context.getELContext();
+        return ValueReferenceResolver.resolve(valueExpression, elCtx);
+    }
+
+    /**
      * This method can be used to extract the ValueReference from the given ValueExpression.
      *
      * @param valueExpression The ValueExpression to resolve.
      * @param elCtx The ELContext, needed to parse and execute the expression.
      * @return The ValueReferenceWrapper.
      */
-    public static ValueReference resolve(ValueExpression valueExpression, final ELContext elCtx)
+    public static ValueReference resolve(ValueExpression valueExpression, ELContext elCtx)
     {
         ValueReference valueReference = valueExpression.getValueReference(elCtx);
         
diff --git a/impl/src/main/java/org/apache/myfaces/component/validate/WholeBeanValidator.java b/impl/src/main/java/org/apache/myfaces/component/validate/WholeBeanValidator.java
index 13d91dc..3571545 100644
--- a/impl/src/main/java/org/apache/myfaces/component/validate/WholeBeanValidator.java
+++ b/impl/src/main/java/org/apache/myfaces/component/validate/WholeBeanValidator.java
@@ -252,22 +252,6 @@ public class WholeBeanValidator implements Validator
 
     }
 
-
-    /**
-     * Get the ValueReference from the ValueExpression.
-     *
-     * @param valueExpression The ValueExpression for value.
-     * @param context The FacesContext.
-     * @return A ValueReferenceWrapper with the necessary information about the ValueReference.
-     */
-    private ValueReference getValueReference(
-            final ValueExpression valueExpression, final FacesContext context)
-    {
-        ELContext elCtx = context.getELContext();
-
-        return ValueReferenceResolver.resolve(valueExpression, elCtx);
-    }
-
     /**
      * This method creates ValidatorFactory instances or retrieves them from the container.
      *
@@ -398,7 +382,7 @@ public class WholeBeanValidator implements Validator
             }
 
             // Obtain a reference to the to-be-validated object and the property name.
-            ValueReference reference = validator.getValueReference(
+            ValueReference reference = ValueReferenceResolver.resolve(
                     valueExpression, context.getFacesContext());
             if (reference == null)
             {
diff --git a/impl/src/main/java/org/apache/myfaces/util/WebXmlParser.java b/impl/src/main/java/org/apache/myfaces/util/WebXmlParser.java
index 30b54d7..f119d93 100755
--- a/impl/src/main/java/org/apache/myfaces/util/WebXmlParser.java
+++ b/impl/src/main/java/org/apache/myfaces/util/WebXmlParser.java
@@ -136,7 +136,7 @@ public class WebXmlParser
 
         try
         {
-            Enumeration<URL> webFragments = Thread.currentThread().getContextClassLoader()
+            Enumeration<URL> webFragments = ClassUtils.getContextClassLoader()
                     .getResources("META-INF/web-fragment.xml");
             while (webFragments.hasMoreElements())
             {