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 2022/08/31 07:13:05 UTC

[myfaces] branch 2.3-next updated: MYFACES-4442: avoid UnsupportedOperationException (#298)

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

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


The following commit(s) were added to refs/heads/2.3-next by this push:
     new eca3852c5 MYFACES-4442: avoid UnsupportedOperationException (#298)
eca3852c5 is described below

commit eca3852c54d8e6fcd3f2beb72b1d62b447cf0d3a
Author: Paul A. Nicolucci <pn...@gmail.com>
AuthorDate: Wed Aug 31 03:13:00 2022 -0400

    MYFACES-4442: avoid UnsupportedOperationException (#298)
---
 .../myfaces/webapp/AbstractFacesInitializer.java   | 67 +++++++---------
 .../webapp/MyFacesContainerInitializer.java        | 92 ++++++++++++++--------
 2 files changed, 86 insertions(+), 73 deletions(-)

diff --git a/impl/src/main/java/org/apache/myfaces/webapp/AbstractFacesInitializer.java b/impl/src/main/java/org/apache/myfaces/webapp/AbstractFacesInitializer.java
index 90ae1ed44..196974c20 100644
--- a/impl/src/main/java/org/apache/myfaces/webapp/AbstractFacesInitializer.java
+++ b/impl/src/main/java/org/apache/myfaces/webapp/AbstractFacesInitializer.java
@@ -83,7 +83,7 @@ public abstract class AbstractFacesInitializer implements FacesInitializer
     private static final Logger log = Logger.getLogger(AbstractFacesInitializer.class.getName());
 
     public static final String CDI_BEAN_MANAGER_INSTANCE = "oam.cdi.BEAN_MANAGER_INSTANCE";
-    
+
     private static final String CDI_SERVLET_CONTEXT_BEAN_MANAGER_ATTRIBUTE = 
         "javax.enterprise.inject.spi.BeanManager";
 
@@ -122,43 +122,34 @@ public abstract class AbstractFacesInitializer implements FacesInitializer
             {
                 spf.initKnownServiceProviderMapInfo(externalContext, spfConfig);
             }
-            
-            // Parse and validate the web.xml configuration file
-            
+
             if (!WebConfigParamUtils.getBooleanInitParameter(externalContext,
                     MyfacesConfig.INITIALIZE_ALWAYS_STANDALONE, false))
             {
-                FacesServletMappingUtils.ServletRegistrationInfo facesServletRegistration =
-                        FacesServletMappingUtils.getFacesServletRegistration(facesContext, servletContext);
-                if (facesServletRegistration == null
-                        || facesServletRegistration.getMappings() == null
-                        || facesServletRegistration.getMappings().length == 0)
+                // check to see if the FacesServlet was found by MyFacesContainerInitializer
+                Boolean mappingAdded = (Boolean) servletContext.getAttribute(
+                    MyFacesContainerInitializer.FACES_SERVLET_FOUND);
+
+                if (mappingAdded == null || !mappingAdded)
                 {
-                    // check to see if the FacesServlet was found by MyFacesContainerInitializer
-                    Boolean mappingAdded = (Boolean) servletContext.getAttribute(
-                        MyFacesContainerInitializer.FACES_SERVLET_FOUND);
+                    // check if the FacesServlet has been added dynamically
+                    // in a Servlet 3.0 environment by MyFacesContainerInitializer
+                    mappingAdded = (Boolean) servletContext.getAttribute(
+                        MyFacesContainerInitializer.FACES_SERVLET_ADDED_ATTRIBUTE);
 
                     if (mappingAdded == null || !mappingAdded)
                     {
-                        // check if the FacesServlet has been added dynamically
-                        // in a Servlet 3.0 environment by MyFacesContainerInitializer
-                        mappingAdded = (Boolean) servletContext.getAttribute(
-                            MyFacesContainerInitializer.FACES_SERVLET_ADDED_ATTRIBUTE);
-
-                        if (mappingAdded == null || !mappingAdded)
+                        if (log.isLoggable(Level.WARNING))
                         {
-                            if (log.isLoggable(Level.WARNING))
-                            {
-                                log.warning("No mappings of FacesServlet found. Abort initializing MyFaces.");
-                            }
-                            return;
+                            log.warning("No mappings of FacesServlet found. Abort initializing MyFaces.");
                         }
+                        return;
                     }
                 }
             }
-            
+
             initCDIIntegration(servletContext, externalContext);
-            
+
             initContainerIntegration(servletContext, externalContext);
 
             // log environment integrations
@@ -204,10 +195,10 @@ public abstract class AbstractFacesInitializer implements FacesInitializer
             }
 
             WebConfigParamsLogger.logWebContextParams(facesContext);
-            
+
             //Start ViewPoolProcessor if necessary
             ViewPoolProcessor.initialize(facesContext);
-            
+
             MyfacesConfig config = MyfacesConfig.getCurrentInstance(facesContext.getExternalContext());
             if (config.isAutomaticExtensionlessMapping())
             {
@@ -221,7 +212,7 @@ public abstract class AbstractFacesInitializer implements FacesInitializer
                 facesContext.getExternalContext().getApplicationMap().put(
                         MyfacesConfig.RESOURCE_BUNDLE_CONTROL, resourceBundleControl);
             }
- 
+
             // print out a very prominent log message if the project stage is != Production
             if (!facesContext.isProjectStage(ProjectStage.Production)
                     && !facesContext.isProjectStage(ProjectStage.UnitTest))
@@ -456,33 +447,33 @@ public abstract class AbstractFacesInitializer implements FacesInitializer
         // before Application and RenderKit factories, so we should use different object. 
         return _createFacesContext(servletContext, true);
     }
-        
+
     @Override
     public void destroyStartupFacesContext(FacesContext facesContext)
     {
         _releaseFacesContext(facesContext);
     }
-    
+
     @Override
     public FacesContext initShutdownFacesContext(ServletContext servletContext)
     {
         return _createFacesContext(servletContext, false);
     }
-    
-    @Override    
+
+    @Override
     public void destroyShutdownFacesContext(FacesContext facesContext)
     {
         _releaseFacesContext(facesContext);
     }
-    
+
     private FacesContext _createFacesContext(ServletContext servletContext, boolean startup)
     {
         ExternalContext externalContext = new StartupServletExternalContextImpl(servletContext, startup);
         ExceptionHandler exceptionHandler = new ExceptionHandlerImpl();
-        FacesContext facesContext = new StartupFacesContextImpl(externalContext, 
+        FacesContext facesContext = new StartupFacesContextImpl(externalContext,
                 externalContext, exceptionHandler, startup);
-        
-        // If getViewRoot() is called during application startup or shutdown, 
+
+        // If getViewRoot() is called during application startup or shutdown,
         // it should return a new UIViewRoot with its locale set to Locale.getDefault().
         UIViewRoot startupViewRoot = new UIViewRoot();
         startupViewRoot.setLocale(Locale.getDefault());
@@ -490,7 +481,7 @@ public abstract class AbstractFacesInitializer implements FacesInitializer
         
         return facesContext;
     }
-    
+
     private void _releaseFacesContext(FacesContext facesContext)
     {        
         // make sure that the facesContext gets released.
@@ -498,7 +489,7 @@ public abstract class AbstractFacesInitializer implements FacesInitializer
         if (facesContext != null)
         {
             facesContext.release();
-        }        
+        }
     }
     
     /**
diff --git a/impl/src/main/java/org/apache/myfaces/webapp/MyFacesContainerInitializer.java b/impl/src/main/java/org/apache/myfaces/webapp/MyFacesContainerInitializer.java
index dc9fc0c5e..2d67ef92b 100644
--- a/impl/src/main/java/org/apache/myfaces/webapp/MyFacesContainerInitializer.java
+++ b/impl/src/main/java/org/apache/myfaces/webapp/MyFacesContainerInitializer.java
@@ -60,7 +60,7 @@ import org.apache.myfaces.util.lang.ClassUtils;
 
 /**
  * This class is called by any Java EE 6 complaint container at startup.
- * It checks if the current webapp is a JSF-webapp by checking if some of 
+ * It checks if the current webapp is a JSF-webapp by checking if some of
  * the JSF related annotations are specified in the webapp classpath or if
  * the faces-config.xml file is present. If so, the listener checks if 
  * the FacesServlet has already been defined in web.xml and if not, it adds
@@ -135,6 +135,9 @@ public class MyFacesContainerInitializer implements ServletContainerInitializer
             return;
         }
 
+        // Check for a FacesServlet and store the result so it can be used by the FacesInitializer.
+        boolean isFacesServletPresent = checkForFacesServlet(servletContext);
+
         // Check for one or more of this conditions:
         // 1. A faces-config.xml file is found in WEB-INF
         // 2. A faces-config.xml file is found in the META-INF directory of a jar in the application's classpath.
@@ -145,45 +148,42 @@ public class MyFacesContainerInitializer implements ServletContainerInitializer
         //    implementation is not empty.
         if ((clazzes != null && !clazzes.isEmpty()) || isFacesConfigPresent(servletContext))
         {
-            // look for the FacesServlet
-            Map<String, ? extends ServletRegistration> servlets = servletContext.getServletRegistrations();
-            for (Map.Entry<String, ? extends ServletRegistration> servletEntry : servlets.entrySet())
+            /*
+             * If we get into this code block the application contains some Faces artifacts, either a faces-config.xml
+             * or one ore more classes from @HandlesTypes. However if classes from @HandlesTypes or a faces-config.xml
+             * is available a FacesServlet definition might not be defined.
+             *
+             * If a FacesServet definition was not found then add it dynamically.
+             */
+            if(!isFacesServletPresent)
             {
-                String className = servletEntry.getValue().getClassName();
-                if (FACES_SERVLET_CLASS.getName().equals(className) || isDelegatedFacesServlet(className))
+                // the FacesServlet is not installed yet - install it
+                ServletRegistration.Dynamic servlet =
+                        servletContext.addServlet(FACES_SERVLET_NAME, FACES_SERVLET_CLASS);
+
+                //try to add typical JSF mappings
+                String[] mappings = isAutomaticXhtmlMappingDisabled(servletContext) ?
+                            FACES_SERVLET_MAPPINGS : FACES_SERVLET_FULL_MAPPINGS;
+                Set<String> conflictMappings = servlet.addMapping(mappings);
+                if (conflictMappings != null && !conflictMappings.isEmpty())
                 {
-                    // we found a FacesServlet; set an attribute for use during initialization
-                    servletContext.setAttribute(FACES_SERVLET_FOUND, Boolean.TRUE);                    
-                    return;
+                    //at least one of the attempted mappings is in use, remove and try again
+                    Set<String> newMappings = new HashSet<>(Arrays.asList(mappings));
+                    newMappings.removeAll(conflictMappings);
+                    mappings = newMappings.toArray(new String[newMappings.size()]);
+                    servlet.addMapping(mappings);
                 }
-            }
-
-            // the FacesServlet is not installed yet - install it
-            ServletRegistration.Dynamic servlet = servletContext.addServlet(FACES_SERVLET_NAME, FACES_SERVLET_CLASS);
-
-            //try to add typical JSF mappings
-            String[] mappings = isAutomaticXhtmlMappingDisabled(servletContext) ? 
-                        FACES_SERVLET_MAPPINGS : FACES_SERVLET_FULL_MAPPINGS;
-            Set<String> conflictMappings = servlet.addMapping(mappings);
-            if (conflictMappings != null && !conflictMappings.isEmpty())
-            {
-                //at least one of the attempted mappings is in use, remove and try again
-                Set<String> newMappings = new HashSet<>(Arrays.asList(mappings));
-                newMappings.removeAll(conflictMappings);
-                mappings = newMappings.toArray(new String[newMappings.size()]);
-                servlet.addMapping(mappings);
-            }
 
-            if (mappings != null && mappings.length > 0)
-            {
-                // at least one mapping was added 
-                // now we have to set a field in the ServletContext to indicate that we have
-                // added the mapping dynamically, because MyFaces just parsed the web.xml to
-                // find mappings and thus it would abort initializing
-                servletContext.setAttribute(FACES_SERVLET_ADDED_ATTRIBUTE, Boolean.TRUE);
+                if (mappings != null && mappings.length > 0)
+                {
+                    // at least one mapping was added 
+                    // now we have to set a field in the ServletContext to indicate that we have
+                    // added the mapping dynamically.
+                    servletContext.setAttribute(FACES_SERVLET_ADDED_ATTRIBUTE, Boolean.TRUE);
 
-                // add a log message
-                log.log(Level.INFO, "Added FacesServlet with mappings=" + Arrays.toString(mappings));
+                    // add a log message
+                    log.log(Level.INFO, "Added FacesServlet with mappings=" + Arrays.toString(mappings));
+                }
             }
         }
     }
@@ -307,6 +307,28 @@ public class MyFacesContainerInitializer implements ServletContainerInitializer
         }
     }
 
+    /*
+     * Checks if a FacesServlet has been mapped.
+     */
+    private boolean checkForFacesServlet(ServletContext servletContext)
+    {
+        // look for the FacesServlet
+        Map<String, ? extends ServletRegistration> servlets = servletContext.getServletRegistrations();
+        boolean isFacesServletPresent = false;
+
+        for (Map.Entry<String, ? extends ServletRegistration> servletEntry : servlets.entrySet())
+        {
+            String className = servletEntry.getValue().getClassName();
+            if (FACES_SERVLET_CLASS.getName().equals(className) || isDelegatedFacesServlet(className))
+            {
+                // we found a FacesServlet; set an attribute for use during initialization
+                servletContext.setAttribute(FACES_SERVLET_FOUND, Boolean.TRUE);
+                isFacesServletPresent = true;
+            }
+        }
+        return isFacesServletPresent;
+    }
+
     /**
      * Checks if the class represented by className implements DelegatedFacesServlet.
      * @param className