myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tandrasc...@apache.org
Subject [myfaces] branch master updated: unified cache
Date Mon, 03 Dec 2018 15:08:23 GMT
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 44a95e3  unified cache
44a95e3 is described below

commit 44a95e343bdc1e096bef27821959d45f47443f5c
Author: Thomas Andraschko <tandraschko@apache.org>
AuthorDate: Mon Dec 3 16:08:19 2018 +0100

    unified cache
---
 .../application/DefaultViewHandlerSupport.java     | 141 +++++----------------
 .../myfaces/lifecycle/CheckedViewIdsCache.java     | 111 ++++++++++++++++
 .../lifecycle/DefaultRestoreViewSupport.java       | 127 ++++---------------
 3 files changed, 165 insertions(+), 214 deletions(-)

diff --git a/impl/src/main/java/org/apache/myfaces/application/DefaultViewHandlerSupport.java
b/impl/src/main/java/org/apache/myfaces/application/DefaultViewHandlerSupport.java
index 65a8a8b..80f5c5c 100644
--- a/impl/src/main/java/org/apache/myfaces/application/DefaultViewHandlerSupport.java
+++ b/impl/src/main/java/org/apache/myfaces/application/DefaultViewHandlerSupport.java
@@ -23,20 +23,17 @@ import java.util.Map;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
-import javax.faces.application.ProjectStage;
 import javax.faces.application.ViewHandler;
 import javax.faces.context.ExternalContext;
 import javax.faces.context.FacesContext;
 import javax.faces.render.ResponseStateManager;
 import javax.faces.view.ViewDeclarationLanguage;
 
-import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam;
+import org.apache.myfaces.lifecycle.CheckedViewIdsCache;
 import org.apache.myfaces.util.SharedStringBuilder;
-import org.apache.myfaces.util.ConcurrentLRUCache;
 import org.apache.myfaces.util.ExternalContextUtils;
 import org.apache.myfaces.util.StringUtils;
 import org.apache.myfaces.util.ViewProtectionUtils;
-import org.apache.myfaces.util.WebConfigParamUtils;
 
 /**
  * A ViewHandlerSupport implementation for use with standard Java Servlet engines,
@@ -51,37 +48,14 @@ public class DefaultViewHandlerSupport implements ViewHandlerSupport
         DefaultViewHandlerSupport.class.getName() + ".CACHED_SERVLET_MAPPING";
 
     private static final Logger log = Logger.getLogger(DefaultViewHandlerSupport.class.getName());
-
-    /**
-     * Controls the size of the cache used to "remember" if a view exists or not.
-     */
-    @JSFWebConfigParam(defaultValue = "500", since = "2.0.2", group="viewhandler", tags="performance",

-            classType="java.lang.Integer",
-            desc="Controls the size of the cache used to 'remember' if a view exists or not.")
-    private static final String CHECKED_VIEWID_CACHE_SIZE_ATTRIBUTE = "org.apache.myfaces.CHECKED_VIEWID_CACHE_SIZE";
-    private static final int CHECKED_VIEWID_CACHE_DEFAULT_SIZE = 500;
-
-    /**
-     * Enable or disable a cache used to "remember" if a view exists or not and reduce the
impact of
-     * sucesive calls to ExternalContext.getResource().
-     */
-    @JSFWebConfigParam(defaultValue = "true", since = "2.0.2", expectedValues="true, false",
group="viewhandler", 
-            tags="performance",
-            desc="Enable or disable a cache used to 'remember' if a view exists or not and
reduce the impact " +
-                 "of sucesive calls to ExternalContext.getResource().")
-    private static final String CHECKED_VIEWID_CACHE_ENABLED_ATTRIBUTE = 
-        "org.apache.myfaces.CHECKED_VIEWID_CACHE_ENABLED";
-    private static final boolean CHECKED_VIEWID_CACHE_ENABLED_DEFAULT = true;
     
     private static final String VIEW_HANDLER_SUPPORT_SB = "oam.viewhandler.SUPPORT_SB";
-
-    private volatile ConcurrentLRUCache<String, Boolean> _checkedViewIdMap = null;
-    private Boolean _checkedViewIdCacheEnabled = null;
     
     private final String[] _faceletsViewMappings;
     private final String[] _contextSuffixes;
     private final String _faceletsContextSufix;
     private final boolean _initialized;
+    private CheckedViewIdsCache checkedViewIdsCache = null;
     
     public DefaultViewHandlerSupport()
     {
@@ -194,7 +168,7 @@ public class DefaultViewHandlerSupport implements ViewHandlerSupport
                     viewId = handleSuffixMapping(context, viewId+".jsf");
                 }
 
-                return (checkResourceExists(context,viewId) ? viewId : null);
+                return (checkViewExists(context,viewId) ? viewId : null);
             }
         }
         else if (mapping.getUrlPattern().startsWith(viewId))
@@ -205,7 +179,7 @@ public class DefaultViewHandlerSupport implements ViewHandlerSupport
         {
             if(viewId != null)
             {
-                return (checkResourceExists(context,viewId) ? viewId : null);
+                return (checkViewExists(context,viewId) ? viewId : null);
             }
         }
 
@@ -527,7 +501,7 @@ public class DefaultViewHandlerSupport implements ViewHandlerSupport
                         builder.append(candidateViewId); 
                         builder.replace(candidateViewId.lastIndexOf('.'), candidateViewId.length(),
mapping);
                         String tempViewId = builder.toString();
-                        if(checkResourceExists(context,tempViewId))
+                        if (checkViewExists(context, tempViewId))
                         {
                             return tempViewId;
                         }
@@ -536,7 +510,7 @@ public class DefaultViewHandlerSupport implements ViewHandlerSupport
             }
 
             // forced facelets mappings did not match or there were no entries in faceletsViewMappings
array
-            if(checkResourceExists(context,candidateViewId))
+            if (checkViewExists(context,candidateViewId))
             {
                 return candidateViewId;
             }
@@ -572,14 +546,14 @@ public class DefaultViewHandlerSupport implements ViewHandlerSupport
             }
             
             String candidateViewId = builder.toString();
-            if (checkResourceExists(context,candidateViewId))
+            if (checkViewExists(context,candidateViewId))
             {
                 return candidateViewId;
             }
         }
 
         // Otherwise, if a physical resource exists with the name requestViewId let that
value be viewId.
-        if(checkResourceExists(context,requestViewId))
+        if (checkViewExists(context,requestViewId))
         {
             return requestViewId;
         }
@@ -588,101 +562,48 @@ public class DefaultViewHandlerSupport implements ViewHandlerSupport
         return null;
     }
     
-    protected boolean checkResourceExists(FacesContext context, String viewId)
+    protected boolean checkViewExists(FacesContext facesContext, String viewId)
     {
+        if (checkedViewIdsCache == null)
+        {
+            checkedViewIdsCache = CheckedViewIdsCache.getInstance(facesContext);
+        }
+        
         try
         {
-            if (isCheckedViewIdCachingEnabled(context))
+            Boolean resourceExists = null;
+            if (checkedViewIdsCache.isEnabled())
             {
-                Boolean resourceExists = getCheckedViewIDMap(context).get(
-                        viewId);
-                if (resourceExists == null)
-                {
-                    ViewDeclarationLanguage vdl = context.getApplication().getViewHandler()
-                            .getViewDeclarationLanguage(context, viewId);
-                    if (vdl != null)
-                    {
-                        resourceExists = vdl.viewExists(context, viewId);
-                    }
-                    else
-                    {
-                        // Fallback to default strategy
-                        resourceExists = context.getExternalContext().getResource(
-                                viewId) != null;
-                    }
-                    getCheckedViewIDMap(context).put(viewId, resourceExists);
-                }
-                return resourceExists;
+                resourceExists = checkedViewIdsCache.getCache().get(viewId);
             }
-            else
+
+            if (resourceExists == null)
             {
-                ViewDeclarationLanguage vdl = context.getApplication().getViewHandler()
-                            .getViewDeclarationLanguage(context, viewId);
+                ViewDeclarationLanguage vdl = facesContext.getApplication().getViewHandler()
+                        .getViewDeclarationLanguage(facesContext, viewId);
                 if (vdl != null)
                 {
-                    if (vdl.viewExists(context, viewId))
-                    {
-                        return true;
-                    }
+                    resourceExists = vdl.viewExists(facesContext, viewId);
                 }
                 else
                 {
                     // Fallback to default strategy
-                    if (context.getExternalContext().getResource(viewId) != null)
-                    {
-                        return true;
-                    }
+                    resourceExists = facesContext.getExternalContext().getResource(viewId)
!= null;
+                }
+
+                if (checkedViewIdsCache.isEnabled())
+                {
+                    checkedViewIdsCache.getCache().put(viewId, resourceExists);
                 }
             }
+
+            return resourceExists;
         }
-        catch(MalformedURLException e)
+        catch (MalformedURLException e)
         {
             //ignore and move on
         }     
         return false;
     }
 
-    private ConcurrentLRUCache<String, Boolean> getCheckedViewIDMap(FacesContext context)
-    {
-        if (_checkedViewIdMap == null)
-        {
-            int maxSize = getViewIDCacheMaxSize(context);
-            _checkedViewIdMap = new ConcurrentLRUCache<String, Boolean>((maxSize *
4 + 3) / 3, maxSize);
-        }
-        return _checkedViewIdMap;
-    }
-
-    private boolean isCheckedViewIdCachingEnabled(FacesContext context)
-    {
-        if (_checkedViewIdCacheEnabled == null)
-        {
-            // first, check if the ProjectStage is development and skip caching in this case
-            if (context.isProjectStage(ProjectStage.Development))
-            {
-                _checkedViewIdCacheEnabled = Boolean.FALSE;
-            }
-            else
-            {
-                // in all ohter cases, make sure that the cache is not explicitly disabled
via context param
-                _checkedViewIdCacheEnabled = WebConfigParamUtils.getBooleanInitParameter(context.getExternalContext(),
-                        CHECKED_VIEWID_CACHE_ENABLED_ATTRIBUTE,
-                        CHECKED_VIEWID_CACHE_ENABLED_DEFAULT);
-            }
-
-            if (log.isLoggable(Level.FINE))
-            {
-                log.log(Level.FINE, "MyFaces ViewID Caching Enabled="
-                        + _checkedViewIdCacheEnabled);
-            }
-        }
-        return _checkedViewIdCacheEnabled;
-    }
-
-    private int getViewIDCacheMaxSize(FacesContext context)
-    {
-        ExternalContext externalContext = context.getExternalContext();
-
-        return WebConfigParamUtils.getIntegerInitParameter(externalContext,
-                CHECKED_VIEWID_CACHE_SIZE_ATTRIBUTE, CHECKED_VIEWID_CACHE_DEFAULT_SIZE);
-    }
 }
diff --git a/impl/src/main/java/org/apache/myfaces/lifecycle/CheckedViewIdsCache.java b/impl/src/main/java/org/apache/myfaces/lifecycle/CheckedViewIdsCache.java
new file mode 100644
index 0000000..00e966d
--- /dev/null
+++ b/impl/src/main/java/org/apache/myfaces/lifecycle/CheckedViewIdsCache.java
@@ -0,0 +1,111 @@
+/*
+ * 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.lifecycle;
+
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import javax.faces.application.ProjectStage;
+import javax.faces.context.FacesContext;
+import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam;
+import org.apache.myfaces.util.ConcurrentLRUCache;
+import org.apache.myfaces.util.WebConfigParamUtils;
+
+public class CheckedViewIdsCache
+{
+    private static final Logger LOG = Logger.getLogger(CheckedViewIdsCache.class.getName());
+    private static final String INSTANCE_KEY = CheckedViewIdsCache.class.getName();
+    
+    /**
+     * Controls the size of the cache used to "remember" if a view exists or not.
+     */
+    @JSFWebConfigParam(defaultValue = "500", since = "2.0.2", group="viewhandler", tags="performance",

+            classType="java.lang.Integer",
+            desc="Controls the size of the cache used to 'remember' if a view exists or not.")
+    private static final String CHECKED_VIEWID_CACHE_SIZE_ATTRIBUTE = "org.apache.myfaces.CHECKED_VIEWID_CACHE_SIZE";
+
+    /**
+     * Enable or disable a cache used to "remember" if a view exists or not and reduce the
impact of
+     * sucesive calls to ExternalContext.getResource().
+     */
+    @JSFWebConfigParam(defaultValue = "true", since = "2.0.2", expectedValues="true, false",
group="viewhandler", 
+            tags="performance",
+            desc="Enable or disable a cache used to 'remember' if a view exists or not and
reduce the impact " +
+                 "of sucesive calls to ExternalContext.getResource().")
+    private static final String CHECKED_VIEWID_CACHE_ENABLED_ATTRIBUTE = 
+        "org.apache.myfaces.CHECKED_VIEWID_CACHE_ENABLED";
+
+    private volatile ConcurrentLRUCache<String, Boolean> cache = null;
+    private boolean enabled;
+    private int size;
+    
+    private CheckedViewIdsCache()
+    {
+    }
+    
+    public void init(FacesContext facesContext)
+    {
+        // first, check if the ProjectStage is development and skip caching in this case
+        if (facesContext.isProjectStage(ProjectStage.Development))
+        {
+            enabled = false;
+        }
+        else
+        {
+            // in all ohter cases, make sure that the cache is not explicitly disabled via
context param
+            enabled = WebConfigParamUtils.getBooleanInitParameter(facesContext.getExternalContext(),
+                    CHECKED_VIEWID_CACHE_ENABLED_ATTRIBUTE,
+                    true);
+        }
+        
+        size = WebConfigParamUtils.getIntegerInitParameter(facesContext.getExternalContext(),
+                CHECKED_VIEWID_CACHE_SIZE_ATTRIBUTE,
+                500);
+
+        cache = new ConcurrentLRUCache<>((size * 4 + 3) / 3, size);
+        
+        if (LOG.isLoggable(Level.FINE))
+        {
+            LOG.log(Level.FINE, "MyFaces CheckedViewIdsCache enabled=" + enabled + ", size="
+ size);
+        }
+    }
+    
+    public boolean isEnabled()
+    {
+        return enabled;
+    }
+    
+    public ConcurrentLRUCache<String, Boolean> getCache()
+    {
+        return cache;
+    }
+    
+    public static CheckedViewIdsCache getInstance(FacesContext facesContext)
+    {
+        CheckedViewIdsCache instance = (CheckedViewIdsCache)
+                facesContext.getExternalContext().getApplicationMap().get(INSTANCE_KEY);
+        if (instance == null)
+        {
+            instance = new CheckedViewIdsCache();
+            instance.init(facesContext);
+            facesContext.getExternalContext().getApplicationMap().put(INSTANCE_KEY, instance);
+        }
+        
+        return instance;
+    }
+}
diff --git a/impl/src/main/java/org/apache/myfaces/lifecycle/DefaultRestoreViewSupport.java
b/impl/src/main/java/org/apache/myfaces/lifecycle/DefaultRestoreViewSupport.java
index 0469ed7..88b119a 100644
--- a/impl/src/main/java/org/apache/myfaces/lifecycle/DefaultRestoreViewSupport.java
+++ b/impl/src/main/java/org/apache/myfaces/lifecycle/DefaultRestoreViewSupport.java
@@ -28,7 +28,6 @@ import java.util.logging.Logger;
 
 import javax.faces.FacesException;
 import javax.faces.FactoryFinder;
-import javax.faces.application.ProjectStage;
 import javax.faces.application.ViewHandler;
 import javax.faces.component.UIComponent;
 import javax.faces.component.visit.VisitCallback;
@@ -43,10 +42,7 @@ import javax.faces.render.RenderKitFactory;
 import javax.faces.render.ResponseStateManager;
 import javax.faces.view.ViewDeclarationLanguage;
 
-import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam;
 import org.apache.myfaces.util.Assert;
-import org.apache.myfaces.util.ConcurrentLRUCache;
-import org.apache.myfaces.util.WebConfigParamUtils;
 
 /**
  * @author Mathias Broekelmann (latest modification by $Author$)
@@ -66,38 +62,14 @@ public class DefaultRestoreViewSupport implements RestoreViewSupport
 
     private final Logger log = Logger.getLogger(DefaultRestoreViewSupport.class.getName());
 
-    /**
-     * Controls the size of the cache used to "remember" if a view exists or not.
-     */
-    @JSFWebConfigParam(defaultValue = "500", since = "2.0.2", group="viewhandler", tags="performance",

-            classType="java.lang.Integer",
-            desc="Controls the size of the cache used to 'remember' if a view exists or not.")
-    private static final String CHECKED_VIEWID_CACHE_SIZE_ATTRIBUTE = "org.apache.myfaces.CHECKED_VIEWID_CACHE_SIZE";
-    private static final int CHECKED_VIEWID_CACHE_DEFAULT_SIZE = 500;
-
-    /**
-     * Enable or disable a cache used to "remember" if a view exists or not and reduce the
impact of
-     * sucesive calls to ExternalContext.getResource().
-     */
-    @JSFWebConfigParam(defaultValue = "true", since = "2.0.2", expectedValues="true, false",
group="viewhandler", 
-            tags="performance",
-            desc="Enable or disable a cache used to 'remember' if a view exists or not and
reduce the impact " +
-                 "of sucesive calls to ExternalContext.getResource().")
-    private static final String CHECKED_VIEWID_CACHE_ENABLED_ATTRIBUTE = 
-        "org.apache.myfaces.CHECKED_VIEWID_CACHE_ENABLED";
-    private static final boolean CHECKED_VIEWID_CACHE_ENABLED_DEFAULT = true;
-    
     private static final String SKIP_ITERATION_HINT = "javax.faces.visit.SKIP_ITERATION";
     
     private static final Set<VisitHint> VISIT_HINTS = Collections.unmodifiableSet(

             EnumSet.of(VisitHint.SKIP_ITERATION));
 
-    private volatile ConcurrentLRUCache<String, Boolean> _checkedViewIdMap = null;
-    private Boolean _checkedViewIdCacheEnabled = null;
-    
     private RenderKitFactory _renderKitFactory = null;
     private VisitContextFactory _visitContextFactory = null;
-
+    private CheckedViewIdsCache checkedViewIdsCache = null;
     
     public DefaultRestoreViewSupport()
     {
@@ -221,7 +193,7 @@ public class DefaultRestoreViewSupport implements RestoreViewSupport
         }
         return _visitContextFactory;
     }
-        
+
     private static class RestoreStateCallback implements VisitCallback
     {
         private PostRestoreStateEvent event;
@@ -250,48 +222,40 @@ public class DefaultRestoreViewSupport implements RestoreViewSupport
     @Override
     public boolean checkViewExists(FacesContext facesContext, String viewId)
     {
-      try
+        if (checkedViewIdsCache == null)
         {
-            if (isCheckedViewIdCachingEnabled(facesContext))
+            checkedViewIdsCache = CheckedViewIdsCache.getInstance(facesContext);
+        }
+        
+        try
+        {
+            Boolean resourceExists = null;
+            if (checkedViewIdsCache.isEnabled())
             {
-                Boolean resourceExists = getCheckedViewIDMap(facesContext).get(viewId);
-                if (resourceExists == null)
-                {
-                    ViewDeclarationLanguage vdl = facesContext.getApplication().getViewHandler()
-                            .getViewDeclarationLanguage(facesContext, viewId);
-                    if (vdl != null)
-                    {
-                        resourceExists = vdl.viewExists(facesContext, viewId);
-                    }
-                    else
-                    {
-                        // Fallback to default strategy
-                        resourceExists = facesContext.getExternalContext().getResource(viewId)
!= null;
-                    }
-                    getCheckedViewIDMap(facesContext).put(viewId, resourceExists);
-                }
-                return resourceExists;
+                resourceExists = checkedViewIdsCache.getCache().get(viewId);
             }
-            else
+
+            if (resourceExists == null)
             {
                 ViewDeclarationLanguage vdl = facesContext.getApplication().getViewHandler()
-                            .getViewDeclarationLanguage(facesContext, viewId);
+                        .getViewDeclarationLanguage(facesContext, viewId);
                 if (vdl != null)
                 {
-                    if (vdl.viewExists(facesContext, viewId))
-                    {
-                        return true;
-                    }
+                    resourceExists = vdl.viewExists(facesContext, viewId);
                 }
                 else
                 {
                     // Fallback to default strategy
-                    if (facesContext.getExternalContext().getResource(viewId) != null)
-                    {
-                        return true;
-                    }
+                    resourceExists = facesContext.getExternalContext().getResource(viewId)
!= null;
+                }
+
+                if (checkedViewIdsCache.isEnabled())
+                {
+                    checkedViewIdsCache.getCache().put(viewId, resourceExists);
                 }
             }
+
+            return resourceExists;
         }
         catch (MalformedURLException e)
         {
@@ -299,49 +263,4 @@ public class DefaultRestoreViewSupport implements RestoreViewSupport
         }     
         return false;
     }
-
-    
-    private ConcurrentLRUCache<String, Boolean> getCheckedViewIDMap(FacesContext context)
-    {
-        if (_checkedViewIdMap == null)
-        {
-            int maxSize = getViewIDCacheMaxSize(context);
-            _checkedViewIdMap = new ConcurrentLRUCache<String, Boolean>((maxSize *
4 + 3) / 3, maxSize);
-        }
-        return _checkedViewIdMap;
-    }
-
-    private boolean isCheckedViewIdCachingEnabled(FacesContext context)
-    {
-        if (_checkedViewIdCacheEnabled == null)
-        {
-            // first, check if the ProjectStage is development and skip caching in this case
-            if (context.isProjectStage(ProjectStage.Development))
-            {
-                _checkedViewIdCacheEnabled = Boolean.FALSE;
-            }
-            else
-            {
-                // in all ohter cases, make sure that the cache is not explicitly disabled
via context param
-                _checkedViewIdCacheEnabled = WebConfigParamUtils.getBooleanInitParameter(context.getExternalContext(),
-                        CHECKED_VIEWID_CACHE_ENABLED_ATTRIBUTE,
-                        CHECKED_VIEWID_CACHE_ENABLED_DEFAULT);
-            }
-
-            if (log.isLoggable(Level.FINE))
-            {
-                log.log(Level.FINE, "MyFaces ViewID Caching Enabled="
-                        + _checkedViewIdCacheEnabled);
-            }
-        }
-        return _checkedViewIdCacheEnabled;
-    }
-
-    private int getViewIDCacheMaxSize(FacesContext context)
-    {
-        ExternalContext externalContext = context.getExternalContext();
-
-        return WebConfigParamUtils.getIntegerInitParameter(externalContext,
-                CHECKED_VIEWID_CACHE_SIZE_ATTRIBUTE, CHECKED_VIEWID_CACHE_DEFAULT_SIZE);
-    }
 }


Mime
View raw message