Updated Branches:
  refs/heads/master 3744105da -> 92357f2c2

WICKET-4422 Minimize Wicket's Ajax JavaScript files at build time

Cache the calculated minified name to avoid repetitive calls to re-calculate it 
again and again.


Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/92357f2c
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/92357f2c
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/92357f2c

Branch: refs/heads/master
Commit: 92357f2c2f19a34b77e61d1d4fd4b6a8cdf33e53
Parents: 3744105
Author: Martin Tzvetanov Grigorov <[email protected]>
Authored: Thu Feb 23 11:26:08 2012 +0100
Committer: Martin Tzvetanov Grigorov <[email protected]>
Committed: Thu Feb 23 11:26:08 2012 +0100

----------------------------------------------------------------------
 .../request/resource/PackageResourceReference.java |   55 +++++++++++----
 1 files changed, 42 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/92357f2c/wicket-core/src/main/java/org/apache/wicket/request/resource/PackageResourceReference.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/request/resource/PackageResourceReference.java
 
b/wicket-core/src/main/java/org/apache/wicket/request/resource/PackageResourceReference.java
index cb77241..9d60aab 100644
--- 
a/wicket-core/src/main/java/org/apache/wicket/request/resource/PackageResourceReference.java
+++ 
b/wicket-core/src/main/java/org/apache/wicket/request/resource/PackageResourceReference.java
@@ -29,7 +29,8 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * TODO javadoc
+ * This is a ResourceReference that knows how to find and serve resources 
located in the
+ * Java package (i.e. next to the class files).
  */
 public class PackageResourceReference extends ResourceReference
 {
@@ -43,9 +44,18 @@ public class PackageResourceReference extends 
ResourceReference
        private transient ConcurrentMap<UrlAttributes, UrlAttributes> 
urlAttributesCacheMap;
 
        /**
-        * Cache for existence check of minified file
+        * Cache for existence of minified version of the resource to avoid 
repetitive calls
+        * to 
org.apache.wicket.util.resource.locator.IResourceStreamLocator#locate() and
+        * #getMinifiedName().
         */
-       private Boolean minifiedExists = null;
+       private static final ConcurrentMap<PackageResourceReference, String> 
MINIFIED_NAMES_CACHE
+                       = Generics.newConcurrentHashMap();
+
+       /**
+        * A constant used to indicate that there is no minified version of the 
resource.
+        */
+       // Any file system wont allow a file with this name, so there wont be 
clashes.
+       private static final String NO_MINIFIED_NAME = "~!@#$%^&*()_+<>?|}";
 
        /**
         * Construct.
@@ -119,7 +129,8 @@ public class PackageResourceReference extends 
ResourceReference
                                getVariation());
                }
 
-               if (minifiedExists)
+               String minifiedName = MINIFIED_NAMES_CACHE.get(this);
+               if (minifiedName != null && minifiedName != NO_MINIFIED_NAME)
                {
                        resource.setCompress(false);
                }
@@ -156,12 +167,15 @@ public class PackageResourceReference extends 
ResourceReference
 
        /**
         * Initializes the cache for the existence of the minified resource.
+        * @return the name of the minified resource or the special constant 
{@value #NO_MINIFIED_NAME}
+        * if there is no minified version
         */
-       private void initMinifiedExists()
+       private String internalGetMinifiedName()
        {
-               if (minifiedExists != null)
+               String minifiedExists = MINIFIED_NAMES_CACHE.get(this);
+               if (minifiedExists != null && minifiedExists != 
NO_MINIFIED_NAME)
                {
-                       return;
+                       return minifiedExists;
                }
 
                String name = getMinifiedName();
@@ -171,12 +185,15 @@ public class PackageResourceReference extends 
ResourceReference
                String absolutePath = Packages.absolutePath(getScope(), name);
                IResourceStream stream = locator.locate(getScope(), 
absolutePath, getStyle(),
                                getVariation(), getLocale(), null, true);
-               minifiedExists = stream != null;
-               if (!minifiedExists && log.isDebugEnabled())
+
+               minifiedExists = stream != null ? name : NO_MINIFIED_NAME;
+               MINIFIED_NAMES_CACHE.put(this, minifiedExists);
+               if (minifiedExists == NO_MINIFIED_NAME && log.isDebugEnabled())
                {
                        log.debug("No minified version of '" + super.getName() +
                                        "' found, expected a file with the name 
'" + name + "', using full version");
                }
+               return minifiedExists;
        }
 
        /**
@@ -207,10 +224,22 @@ public class PackageResourceReference extends 
ResourceReference
        @Override
        public String getName()
        {
-               initMinifiedExists();
-               if (minifiedExists && 
Application.get().getResourceSettings().getUseMinifiedResources())
-                       return getMinifiedName();
-               return super.getName();
+               String name = null;
+
+               if (Application.exists() && 
Application.get().getResourceSettings().getUseMinifiedResources())
+               {
+                       String minifiedName = internalGetMinifiedName();
+                       if (minifiedName != NO_MINIFIED_NAME)
+                       {
+                               name = minifiedName;
+                       }
+               }
+
+               if (name == null)
+               {
+                       name = super.getName();
+               }
+               return name;
        }
 
        @Override

Reply via email to