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 2019/05/02 14:19:54 UTC

[myfaces] branch master updated: MYFACES-4283 better perf

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 3f570f2  MYFACES-4283 better perf
3f570f2 is described below

commit 3f570f29329bf0538a24ecaa674eee58d6a0490f
Author: Thomas Andraschko <ta...@apache.org>
AuthorDate: Thu May 2 16:19:46 2019 +0200

    MYFACES-4283 better perf
---
 .../application/FacesServletMappingUtils.java      | 167 ++++++++++++++-------
 .../myfaces/webapp/AbstractFacesInitializer.java   |  22 +--
 2 files changed, 126 insertions(+), 63 deletions(-)

diff --git a/impl/src/main/java/org/apache/myfaces/application/FacesServletMappingUtils.java b/impl/src/main/java/org/apache/myfaces/application/FacesServletMappingUtils.java
index 77b93e7..b1bcb61 100644
--- a/impl/src/main/java/org/apache/myfaces/application/FacesServletMappingUtils.java
+++ b/impl/src/main/java/org/apache/myfaces/application/FacesServletMappingUtils.java
@@ -19,7 +19,10 @@
 
 package org.apache.myfaces.application;
 
+import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
 import java.util.Map;
 import javax.faces.context.ExternalContext;
 
@@ -35,10 +38,55 @@ import org.apache.myfaces.webapp.DelegatedFacesServlet;
 public class FacesServletMappingUtils
 {
     private static final String FACES_SERVLET_REGISTRATION = "org.apache.myfaces.FACES_SERVLET_REGISTRATION";
-    private static final String FACES_SERVLET_MAPPINGS = "org.apache.myfaces.FACES_SERVLET_MAPPINGS";
+    private static final String SERVLET_REGISTRATIONS = "org.apache.myfaces.SERVLET_REGISTRATIONS";
     
     private static final String CURRENT_REQUEST_FACES_SERVLET = "org.apache.myfaces.CURRENT_FACES_SERVLET_MAPPING";
     
+    /**
+     * Wrapper for better performance
+     */
+    public static class ServletRegistrationInfo
+    {
+        private String className;
+        private String[] mappings;
+        private boolean facesServlet;
+        private ServletRegistration registration;
+
+        public ServletRegistrationInfo(ServletRegistration registration, boolean facesServlet)
+        {
+            this.className = registration.getClassName();
+            this.facesServlet = facesServlet;
+            this.registration = registration;
+
+            Collection<String> mappingsCollection = registration.getMappings();
+            mappings = mappingsCollection.toArray(new String[mappingsCollection.size()]);
+            if (mappings == null)
+            {
+                mappings = new String[]{ };
+            }
+        }
+
+        public String getClassName()
+        {
+            return className;
+        }
+
+        public String[] getMappings()
+        {
+            return mappings;
+        }
+
+        public boolean isFacesServlet()
+        {
+            return facesServlet;
+        }
+        
+        public ServletRegistration getRegistration()
+        {
+            return registration;
+        }
+    }
+    
     public static FacesServletMapping getCurrentRequestFacesServletMapping(FacesContext context)
     {
         Map<Object, Object> attributes = context.getAttributes();
@@ -58,59 +106,67 @@ public class FacesServletMappingUtils
         }
         return mapping;
     }
-    
-    public static ServletRegistration getFacesServletRegistration(FacesContext facesContext,
-            ServletContext servletContext)
+
+    public static List<ServletRegistrationInfo> getServletRegistrations(FacesContext facesContext,
+            ServletContext servletContext, boolean cache)
     {
         Map<String, Object> applicationMap = facesContext.getExternalContext().getApplicationMap();
         
-        ServletRegistration servletRegistration = (ServletRegistration) applicationMap.get(FACES_SERVLET_REGISTRATION);
-        if (servletRegistration == null)
+        List<ServletRegistrationInfo> infos =
+                (List<ServletRegistrationInfo>) applicationMap.get(SERVLET_REGISTRATIONS);
+        if (infos == null)
         {
+            infos = new ArrayList<>();
+            
             Map<String, ? extends ServletRegistration> registrations = servletContext.getServletRegistrations();
             if (registrations != null)
             {
-                for (Map.Entry<String, ? extends ServletRegistration> entry : registrations.entrySet())
+                for (ServletRegistration servletRegistration : registrations.values())
                 {
-                    if (isFacesServlet(facesContext, entry.getValue().getClassName()))
-                    {
-                        servletRegistration = entry.getValue();
-                        break;
-                    }
+                    ServletRegistrationInfo info = new ServletRegistrationInfo(servletRegistration,
+                            isFacesServlet(facesContext, servletRegistration.getClassName()));
+
+                    infos.add(info);
                 }
             }
             
-            applicationMap.put(FACES_SERVLET_REGISTRATION, servletRegistration);
+            infos = Collections.unmodifiableList(infos);
+            if (cache)
+            {
+                applicationMap.put(SERVLET_REGISTRATIONS, infos);
+            }
         }
 
-        return servletRegistration;
+        return infos;
     }
     
-    public static String[] getFacesServletMappings(FacesContext facesContext, ServletContext servletContext)
+    public static ServletRegistrationInfo getFacesServletRegistration(FacesContext facesContext,
+            ServletContext servletContext, boolean cache)
     {
         Map<String, Object> applicationMap = facesContext.getExternalContext().getApplicationMap();
         
-        String[] mappings = (String[]) applicationMap.get(FACES_SERVLET_MAPPINGS);
-        if (mappings == null)
+        ServletRegistrationInfo facesServletRegistration = (ServletRegistrationInfo)
+                applicationMap.get(FACES_SERVLET_REGISTRATION);
+        if (facesServletRegistration == null)
         {
-            ServletRegistration servletRegistration = getFacesServletRegistration(facesContext, servletContext);
-            if (servletRegistration != null)
+            for (ServletRegistrationInfo info : getServletRegistrations(facesContext, servletContext, cache))
             {
-                Collection<String> mappingsCollection = servletRegistration.getMappings();
-                mappings = mappingsCollection.toArray(new String[mappingsCollection.size()]);
+                if (info.isFacesServlet())
+                {
+                    facesServletRegistration = info;
+                    break;
+                }
             }
-            
-            if (mappings == null)
+
+            if (facesServletRegistration != null && cache)
             {
-                mappings = new String[]{ };
+                applicationMap.put(FACES_SERVLET_REGISTRATION, facesServletRegistration);
             }
-
-            applicationMap.put(FACES_SERVLET_MAPPINGS, mappings);
         }
-        
-        return mappings;
+
+        return facesServletRegistration;
     }
-    
+
     public static boolean isFacesServlet(FacesContext facesContext, String servletClassName)
     {
         // shortcut to avoid class lookup
@@ -191,25 +247,21 @@ public class FacesServletMappingUtils
     {
         try
         {
-            Map<String, ? extends ServletRegistration> servletRegistrations = servletContext.getServletRegistrations();
+            List<ServletRegistrationInfo> servletRegistrations =
+                    getServletRegistrations(facesContext, servletContext, true);
             if (servletRegistrations  != null)
             {
                 FacesServletMapping facesExtensionMapping = null;
                 FacesServletMapping facesPrefixMapping = null;
                 FacesServletMapping facesExactMapping = null;
 
-                ServletRegistration facesServletRegistration = getFacesServletRegistration(
-                        facesContext, servletContext);
-                
-                for (ServletRegistration servletRegistration : servletRegistrations.values())
+                for (ServletRegistrationInfo servletRegistration : servletRegistrations)
                 {
                     try
                     {
-                        if (facesServletRegistration.getClassName().equals(servletRegistration.getClassName()))
+                        if (servletRegistration.isFacesServlet())
                         {
-                            // get the cached one
-                            String[] mappings = getFacesServletMappings(facesContext, servletContext);
-                            for (String mapping : mappings)
+                            for (String mapping : servletRegistration.getMappings())
                             {
                                 if (isExtensionMapping(mapping))
                                 {
@@ -229,11 +281,10 @@ public class FacesServletMappingUtils
                         }
                         else
                         {
-                            Collection<String> mappings = servletRegistration.getMappings();
                             //This is not a FacesServlet mapping. 
                             //It could be a non-faces request, we need to look for exact mapping to servletPath
                             //this happens with richfaces resources
-                            for (String mapping : mappings)
+                            for (String mapping : servletRegistration.getMappings())
                             {                                
                                 if (mapping.startsWith("/") && mapping.endsWith("/*"))
                                 {
@@ -330,12 +381,16 @@ public class FacesServletMappingUtils
             Object context = facesContext.getExternalContext().getContext();
             if (context instanceof ServletContext)
             {
-                String[] mappings = getFacesServletMappings(facesContext, (ServletContext) context);
-                for (String mapping : mappings)
+                ServletRegistrationInfo facesServletRegistration =
+                        getFacesServletRegistration(facesContext, (ServletContext) context, true);
+                if (facesServletRegistration != null)
                 {
-                    if (!mapping.contains("*") && prefixedExactMappingViewId.equals(mapping))
+                    for (String mapping : facesServletRegistration.getMappings())
                     {
-                        return FacesServletMapping.createExactMapping(prefixedExactMappingViewId);
+                        if (!mapping.contains("*") && prefixedExactMappingViewId.equals(mapping))
+                        {
+                            return FacesServletMapping.createExactMapping(prefixedExactMappingViewId);
+                        }
                     }
                 }
             }
@@ -352,18 +407,22 @@ public class FacesServletMappingUtils
             Object context = facesContext.getExternalContext().getContext();
             if (context instanceof ServletContext)
             {
-                String[] mappings = getFacesServletMappings(facesContext, (ServletContext) context);
-                for (String mapping : mappings)
+                ServletRegistrationInfo facesServletRegistration =
+                        getFacesServletRegistration(facesContext, (ServletContext) context, true);
+                if (facesServletRegistration != null)
                 {
-                    if (isExtensionMapping(mapping))
+                    for (String mapping : facesServletRegistration.getMappings())
                     {
-                        String extension = extractExtension(mapping);
-                        return FacesServletMapping.createExtensionMapping(extension);
-                    }
-                    else if (isPrefixMapping(mapping))
-                    {
-                        String prefix = extractPrefix(mapping);
-                        return FacesServletMapping.createPrefixMapping(prefix);
+                        if (isExtensionMapping(mapping))
+                        {
+                            String extension = extractExtension(mapping);
+                            return FacesServletMapping.createExtensionMapping(extension);
+                        }
+                        else if (isPrefixMapping(mapping))
+                        {
+                            String prefix = extractPrefix(mapping);
+                            return FacesServletMapping.createPrefixMapping(prefix);
+                        }
                     }
                 }
             }
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 6f57524..b71610c 100644
--- a/impl/src/main/java/org/apache/myfaces/webapp/AbstractFacesInitializer.java
+++ b/impl/src/main/java/org/apache/myfaces/webapp/AbstractFacesInitializer.java
@@ -58,7 +58,6 @@ import java.util.logging.Logger;
 import javax.enterprise.inject.spi.BeanManager;
 import javax.faces.application.ViewVisitOption;
 import javax.faces.push.PushContext;
-import javax.servlet.ServletRegistration;
 import javax.websocket.DeploymentException;
 import javax.websocket.server.ServerContainer;
 import javax.websocket.server.ServerEndpointConfig;
@@ -148,9 +147,11 @@ public abstract class AbstractFacesInitializer implements FacesInitializer
             if (!WebConfigParamUtils.getBooleanInitParameter(externalContext,
                     MyfacesConfig.INITIALIZE_ALWAYS_STANDALONE, false))
             {
-
-                String[] mappings = FacesServletMappingUtils.getFacesServletMappings(facesContext, servletContext);
-                if (mappings == null || mappings.length == 0)
+                FacesServletMappingUtils.ServletRegistrationInfo facesServletRegistration =
+                        FacesServletMappingUtils.getFacesServletRegistration(facesContext, servletContext, false);
+                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(
@@ -328,8 +329,11 @@ public abstract class AbstractFacesInitializer implements FacesInitializer
         if (!WebConfigParamUtils.getBooleanInitParameter(facesContext.getExternalContext(),
                                                          MyfacesConfig.INITIALIZE_ALWAYS_STANDALONE, false))
         {
-            String[] mappings = FacesServletMappingUtils.getFacesServletMappings(facesContext, servletContext);
-            if (mappings == null || mappings.length == 0)
+            FacesServletMappingUtils.ServletRegistrationInfo facesServletRegistration =
+                    FacesServletMappingUtils.getFacesServletRegistration(facesContext, servletContext, false);
+            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(
@@ -762,13 +766,13 @@ public abstract class AbstractFacesInitializer implements FacesInitializer
      */
     protected void initAutomaticExtensionlessMapping(FacesContext facesContext, ServletContext servletContext)
     {
-        final ServletRegistration facesServletRegistration = FacesServletMappingUtils.getFacesServletRegistration(
-                facesContext, servletContext); 
+        FacesServletMappingUtils.ServletRegistrationInfo facesServletRegistration =
+                FacesServletMappingUtils.getFacesServletRegistration(facesContext, servletContext, false);
         if (facesServletRegistration != null)
         {
             facesContext.getApplication().getViewHandler().getViews(facesContext, "/", 
                     ViewVisitOption.RETURN_AS_MINIMAL_IMPLICIT_OUTCOME).forEach(s -> {
-                        facesServletRegistration.addMapping(s);
+                        facesServletRegistration.getRegistration().addMapping(s);
                     });
         }
     }