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: MYFACES-4225
Date Tue, 06 Oct 2020 14:15:54 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 2533d06  MYFACES-4225
2533d06 is described below

commit 2533d0609819eb583daf90652e703117ec523bee
Author: Thomas Andraschko <tandraschko@apache.org>
AuthorDate: Tue Oct 6 16:15:45 2020 +0200

    MYFACES-4225
---
 .../myfaces/resource/ResourceHandlerCache.java     |  2 +-
 .../apache/myfaces/resource/ResourceLoader.java    | 40 ++++++++++++++--
 .../apache/myfaces/resource/ResourceMetaImpl.java  | 56 ++++++++++++++++++++++
 .../TempDirFileCacheContractResourceLoader.java    |  6 ---
 .../resource/TempDirFileCacheResourceLoader.java   |  6 ---
 .../application/ResourceHandlerImplTest.java       | 13 ++---
 6 files changed, 97 insertions(+), 26 deletions(-)

diff --git a/impl/src/main/java/org/apache/myfaces/resource/ResourceHandlerCache.java b/impl/src/main/java/org/apache/myfaces/resource/ResourceHandlerCache.java
index 453a800..02f2076 100644
--- a/impl/src/main/java/org/apache/myfaces/resource/ResourceHandlerCache.java
+++ b/impl/src/main/java/org/apache/myfaces/resource/ResourceHandlerCache.java
@@ -300,7 +300,7 @@ public class ResourceHandlerCache
         
         if (log.isLoggable(Level.FINE))
         {
-            log.log(Level.FINE, "Attemping to set confirmLibraryExists on cache " + libraryName);
+            log.log(Level.FINE, "Attemping to set confirmLibraryNotExists on cache " + libraryName);
         }
 
         _libraryExistsCacheMap.put(libraryName, Boolean.FALSE);
diff --git a/impl/src/main/java/org/apache/myfaces/resource/ResourceLoader.java b/impl/src/main/java/org/apache/myfaces/resource/ResourceLoader.java
index 1422674..b0c7d51 100644
--- a/impl/src/main/java/org/apache/myfaces/resource/ResourceLoader.java
+++ b/impl/src/main/java/org/apache/myfaces/resource/ResourceLoader.java
@@ -24,6 +24,8 @@ import java.util.Comparator;
 import java.util.Iterator;
 import javax.faces.application.ResourceVisitOption;
 import javax.faces.context.FacesContext;
+import org.apache.myfaces.config.MyfacesConfig;
+import org.apache.myfaces.util.lang.ConcurrentLRUCache;
 
 /**
  * Base class for resource loaders.  Resource loaders can lookup resources 
@@ -34,11 +36,23 @@ public abstract class ResourceLoader
     
     public static final String VERSION_INVALID = "INVALID";
     
-    private String _prefix;
+    private String prefix;
+    private boolean resourceCacheEnabled;
+    private ConcurrentLRUCache<Object, Boolean> resourceExistsCache;
     
     public ResourceLoader(String prefix)
     {
-        _prefix = prefix;
+        this.prefix = prefix;
+        
+        FacesContext facesContext = FacesContext.getCurrentInstance();
+        MyfacesConfig myfacesConfig = MyfacesConfig.getCurrentInstance(facesContext);
+        this.resourceCacheEnabled = myfacesConfig.isResourceHandlerCacheEnabled();
+        
+        if (this.resourceCacheEnabled)
+        {
+            int maxSize = myfacesConfig.getResourceHandlerCacheSize();
+            this.resourceExistsCache =  new ConcurrentLRUCache<>(maxSize * 2, maxSize);
+        }
     }
 
     public abstract String getResourceVersion(String path);
@@ -70,7 +84,23 @@ public abstract class ResourceLoader
     
     public boolean resourceExists(ResourceMeta resourceMeta)
     {
-        return (getResourceURL(resourceMeta) != null);
+        if (resourceMeta == null)
+        {
+            return false;
+        }
+
+        if (resourceCacheEnabled)
+        {
+            Boolean exists = resourceExistsCache.get(resourceMeta);
+            if (exists == null)
+            {
+                exists = getResourceURL(resourceMeta) != null;
+                resourceExistsCache.put(resourceMeta, exists);
+            }
+            return exists;
+        }
+
+        return getResourceURL(resourceMeta) != null;
     }
 
     public Iterator<String> iterator(FacesContext facesContext, 
@@ -162,11 +192,11 @@ public abstract class ResourceLoader
     
     public String getPrefix()
     {
-        return _prefix;
+        return prefix;
     }
 
     public void setPrefix(String prefix)
     {
-        _prefix = prefix;
+        this.prefix = prefix;
     }
 }
diff --git a/impl/src/main/java/org/apache/myfaces/resource/ResourceMetaImpl.java b/impl/src/main/java/org/apache/myfaces/resource/ResourceMetaImpl.java
index d343e75..5dd62a1 100644
--- a/impl/src/main/java/org/apache/myfaces/resource/ResourceMetaImpl.java
+++ b/impl/src/main/java/org/apache/myfaces/resource/ResourceMetaImpl.java
@@ -18,6 +18,8 @@
  */
 package org.apache.myfaces.resource;
 
+import java.util.Objects;
+
 /**
  * Contains the metadata information to reference a resource 
  */
@@ -160,4 +162,58 @@ public class ResourceMetaImpl extends ResourceMeta
         this.lastModified = lastModified;
     }
 
+    @Override
+    public int hashCode()
+    {
+        int hash = 3;
+        hash = 79 * hash + Objects.hashCode(this._libraryName);
+        hash = 79 * hash + Objects.hashCode(this._libraryVersion);
+        hash = 79 * hash + Objects.hashCode(this._resourceName);
+        hash = 79 * hash + Objects.hashCode(this._resourceVersion);
+        hash = 79 * hash + Objects.hashCode(this._contractName);
+        return hash;
+    }
+
+    @Override
+    public boolean equals(Object obj)
+    {
+        if (this == obj)
+        {
+            return true;
+        }
+        if (obj == null)
+        {
+            return false;
+        }
+        if (getClass() != obj.getClass())
+        {
+            return false;
+        }
+        final ResourceMetaImpl other = (ResourceMetaImpl) obj;
+        if (!Objects.equals(this._prefix, other._prefix))
+        {
+            return false;
+        }
+        if (!Objects.equals(this._libraryName, other._libraryName))
+        {
+            return false;
+        }
+        if (!Objects.equals(this._libraryVersion, other._libraryVersion))
+        {
+            return false;
+        }
+        if (!Objects.equals(this._resourceName, other._resourceName))
+        {
+            return false;
+        }
+        if (!Objects.equals(this._resourceVersion, other._resourceVersion))
+        {
+            return false;
+        }
+        if (!Objects.equals(this._contractName, other._contractName))
+        {
+            return false;
+        }
+        return true;
+    }
 }
diff --git a/impl/src/main/java/org/apache/myfaces/resource/TempDirFileCacheContractResourceLoader.java
b/impl/src/main/java/org/apache/myfaces/resource/TempDirFileCacheContractResourceLoader.java
index daa504f..54b69db 100644
--- a/impl/src/main/java/org/apache/myfaces/resource/TempDirFileCacheContractResourceLoader.java
+++ b/impl/src/main/java/org/apache/myfaces/resource/TempDirFileCacheContractResourceLoader.java
@@ -165,12 +165,6 @@ public class TempDirFileCacheContractResourceLoader extends ContractResourceLoad
     {
         return getResourceInputStream(resourceMeta, null);
     }
-    
-    @Override
-    public boolean resourceExists(ResourceMeta resourceMeta)
-    {
-        return super.resourceExists(resourceMeta);
-    }
 
     @SuppressWarnings("unchecked")
     private File createOrGetTempFile(FacesContext facesContext, ResourceMeta resourceMeta)
diff --git a/impl/src/main/java/org/apache/myfaces/resource/TempDirFileCacheResourceLoader.java
b/impl/src/main/java/org/apache/myfaces/resource/TempDirFileCacheResourceLoader.java
index e7d0efc..1e868a0 100644
--- a/impl/src/main/java/org/apache/myfaces/resource/TempDirFileCacheResourceLoader.java
+++ b/impl/src/main/java/org/apache/myfaces/resource/TempDirFileCacheResourceLoader.java
@@ -206,12 +206,6 @@ public class TempDirFileCacheResourceLoader extends ResourceLoaderWrapper
     {
         return getResourceInputStream(resourceMeta, null);
     }
-    
-    @Override
-    public boolean resourceExists(ResourceMeta resourceMeta)
-    {
-        return super.resourceExists(resourceMeta);
-    }
 
     @SuppressWarnings("unchecked")
     private File createOrGetTempFile(FacesContext facesContext, ResourceMeta resourceMeta)
diff --git a/impl/src/test/java/org/apache/myfaces/application/ResourceHandlerImplTest.java
b/impl/src/test/java/org/apache/myfaces/application/ResourceHandlerImplTest.java
index 92e712b..b752ca4 100644
--- a/impl/src/test/java/org/apache/myfaces/application/ResourceHandlerImplTest.java
+++ b/impl/src/test/java/org/apache/myfaces/application/ResourceHandlerImplTest.java
@@ -289,13 +289,11 @@ public class ResourceHandlerImplTest extends AbstractJsfTestCase
         Mockito.verify(loader, Mockito.times(4)).createResourceMeta(
                 Mockito.any(), Mockito.eq("test"), Mockito.any(), Mockito.eq("test.png"),
Mockito.any());
     }
-    
-    @Ignore
+
     @Test
-    public void testNegativeCache()
+    public void testResourceExistsCache()
     {
         ResourceLoader loader = Mockito.spy(new ClassLoaderResourceLoader(null));
-        Mockito.when(loader.resourceExists(Mockito.any())).thenReturn(false);
 
         ResourceHandlerCache cache = Mockito.spy(new ResourceHandlerCache());   
         
@@ -313,9 +311,8 @@ public class ResourceHandlerImplTest extends AbstractJsfTestCase
 
         Mockito.verify(cache, Mockito.times(4)).getResource(
                 Mockito.eq("test.png"), Mockito.eq("test"), Mockito.eq("test"), Mockito.any());
-        Mockito.verify(cache, Mockito.times(1)).putResource(
-                Mockito.eq("test.png"), Mockito.eq("test"), Mockito.eq("test"), Mockito.any(),
Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any());
-        Mockito.verify(loader, Mockito.times(1)).createResourceMeta(
-                Mockito.any(), Mockito.eq("test"), Mockito.any(), Mockito.eq("test.png"),
Mockito.any());
+        Mockito.verify(loader, Mockito.times(1)).getResourceURL(Mockito.any());
+        Mockito.verify(loader, Mockito.never()).getResourceInputStream(Mockito.any());
+        
     }
 }


Mime
View raw message