Author: mgrigorov
Date: Thu Mar 10 19:22:23 2011
New Revision: 1080326
URL: http://svn.apache.org/viewvc?rev=1080326&view=rev
Log:
WICKET-3511 Mapping ResourceReferences to Urls is slow
Introduce IResourceStreamLocator which can wrap another one and cache its
lookups.
It is used only in DEPLOYMENT mode.
Added:
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/util/resource/locator/CachingResourceStreamLocator.java
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/util/resource/locator/
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/util/resource/locator/CachingResourceStreamLocatorTest.java
Modified:
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/settings/def/ResourceSettings.java
Modified:
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/settings/def/ResourceSettings.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/settings/def/ResourceSettings.java?rev=1080326&r1=1080325&r2=1080326&view=diff
==============================================================================
---
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/settings/def/ResourceSettings.java
(original)
+++
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/settings/def/ResourceSettings.java
Thu Mar 10 19:22:23 2011
@@ -22,6 +22,7 @@ import java.util.Map;
import org.apache.wicket.Application;
import org.apache.wicket.IResourceFactory;
import org.apache.wicket.Localizer;
+import org.apache.wicket.RuntimeConfigurationType;
import org.apache.wicket.javascript.IJavaScriptCompressor;
import org.apache.wicket.markup.html.IPackageResourceGuard;
import org.apache.wicket.markup.html.PackageResourceGuard;
@@ -41,6 +42,8 @@ import org.apache.wicket.util.file.IReso
import org.apache.wicket.util.file.Path;
import org.apache.wicket.util.lang.Args;
import org.apache.wicket.util.lang.Generics;
+import org.apache.wicket.util.resource.IResourceStream;
+import org.apache.wicket.util.resource.locator.CachingResourceStreamLocator;
import org.apache.wicket.util.resource.locator.IResourceStreamLocator;
import org.apache.wicket.util.resource.locator.ResourceStreamLocator;
import org.apache.wicket.util.time.Duration;
@@ -229,6 +232,10 @@ public class ResourceSettings implements
// Create compound resource locator using source path
from
// application settings
resourceStreamLocator = new
ResourceStreamLocator(getResourceFinder());
+ if (Application.get().getConfigurationType() ==
RuntimeConfigurationType.DEPLOYMENT)
+ {
+ resourceStreamLocator = new
CachingResourceStreamLocator(resourceStreamLocator);
+ }
}
return resourceStreamLocator;
}
@@ -347,7 +354,13 @@ public class ResourceSettings implements
}
/**
- * @see
org.apache.wicket.settings.IResourceSettings#setResourceStreamLocator(org.apache.wicket.util.resource.locator.IResourceStreamLocator)
+ * {@inheritDoc}
+ *
+ * Consider wrapping <code>resourceStreamLocator</code> in {@link
CachingResourceStreamLocator}.
+ * This way the locator will not be asked more than once for {@link
IResourceStream}s which do
+ * not exist.
+ *
+ * @see #getResourceStreamLocator()
*/
public void setResourceStreamLocator(IResourceStreamLocator
resourceStreamLocator)
{
@@ -440,6 +453,6 @@ public class ResourceSettings implements
"It is not allowed to set the resource caching
strategy to value NULL. " +
"Please use " +
NoOpResourceCachingStrategy.class.getName() + " instead.");
}
- this.resourceCachingStrategy = strategy;
+ resourceCachingStrategy = strategy;
}
}
Added:
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/util/resource/locator/CachingResourceStreamLocator.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/util/resource/locator/CachingResourceStreamLocator.java?rev=1080326&view=auto
==============================================================================
---
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/util/resource/locator/CachingResourceStreamLocator.java
(added)
+++
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/util/resource/locator/CachingResourceStreamLocator.java
Thu Mar 10 19:22:23 2011
@@ -0,0 +1,229 @@
+/*
+ * 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.wicket.util.resource.locator;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Locale;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+import org.apache.wicket.request.resource.ResourceReference.Key;
+import org.apache.wicket.util.file.File;
+import org.apache.wicket.util.lang.Args;
+import org.apache.wicket.util.resource.FileResourceStream;
+import org.apache.wicket.util.resource.IResourceStream;
+import org.apache.wicket.util.resource.UrlResourceStream;
+
+
+/**
+ * Locating resources can take a significant amount of time, especially since
there are often
+ * several CSS, JavaScript and image resources on any given page. To
facilitate localization and
+ * styling, Wicket will usually make several attempts at locating each
resource (i.e. first with an
+ * "en_US" suffix, then "en", and so on); multiply these attempts by the
number of resources on the
+ * page and this starts to add up.
+ * <p>
+ * This locator mitigates this problem by caching (indefinitely) references to
+ * {@link UrlResourceStream} and {@link FileResourceStream} objects as they
are found, and
+ * {@link NullResourceStreamReference} for all which are missing so they are
not looked up again and
+ * again.
+ */
+public class CachingResourceStreamLocator implements IResourceStreamLocator
+{
+ /**
+ * Lightweight reference to the cached {@link IResourceStream}
+ */
+ private static interface IResourceStreamReference
+ {
+ String getReference();
+ }
+
+ /**
+ * A singleton reference that is used for resource streams which do not
exists. I.e. if there is
+ * a key in the cache which value is
NullResourceStreamReference.INSTANCE then there is no need
+ * to lookup again for this key anymore.
+ */
+ private static class NullResourceStreamReference implements
IResourceStreamReference
+ {
+ private final static NullResourceStreamReference INSTANCE = new
NullResourceStreamReference();
+
+ public String getReference()
+ {
+ return null;
+ }
+ }
+
+ /**
+ * A reference which can be used to recreate {@link FileResourceStream}
+ */
+ private static class FileResourceStreamReference implements
IResourceStreamReference
+ {
+ private final String fileName;
+
+ private FileResourceStreamReference(final String fileName)
+ {
+ this.fileName = fileName;
+ }
+
+ public String getReference()
+ {
+ return fileName;
+ }
+ }
+
+ /**
+ * A reference which may be used to recreate {@link UrlResourceStream}
+ */
+ private static class UrlResourceStreamReference implements
IResourceStreamReference
+ {
+ private final String url;
+
+ private UrlResourceStreamReference(final String url)
+ {
+ this.url = url;
+ }
+
+ public String getReference()
+ {
+ return url;
+ }
+ }
+
+ private final ConcurrentMap<Key, IResourceStreamReference> cache;
+
+ private final IResourceStreamLocator delegate;
+
+ /**
+ * Construct.
+ *
+ * @param resourceStreamLocator
+ * the delegate
+ */
+ public CachingResourceStreamLocator(final IResourceStreamLocator
resourceStreamLocator)
+ {
+ Args.notNull(resourceStreamLocator, "resourceStreamLocator");
+
+ delegate = resourceStreamLocator;
+
+ cache = new ConcurrentHashMap<Key, IResourceStreamReference>();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * Checks for {@link IResourceStreamReference} in the cache and returns
<code>null</code> if the
+ * result is {@link NullResourceStreamReference#INSTANCE}, or {@link
FileResourceStream} /
+ * {@link UrlResourceStream} if there is an entry in the cache.
Otherwise asks the delegate to
+ * find one and puts it in the cache.
+ */
+ public IResourceStream locate(Class<?> clazz, String path)
+ {
+ Key key = new Key(clazz.getName(), path, null, null, null);
+ IResourceStream resourceStream = getCopyFromCache(key);
+
+ if (resourceStream == null)
+ {
+ resourceStream = delegate.locate(clazz, path);
+
+ updateCache(key, resourceStream);
+ }
+
+ return resourceStream;
+ }
+
+ private void updateCache(Key key, IResourceStream stream)
+ {
+ if (null == stream)
+ {
+ cache.put(key, NullResourceStreamReference.INSTANCE);
+ }
+ else if (stream instanceof FileResourceStream)
+ {
+ FileResourceStream fileResourceStream =
(FileResourceStream)stream;
+ String absolutePath =
fileResourceStream.getFile().getAbsolutePath();
+ cache.put(key, new
FileResourceStreamReference(absolutePath));
+ }
+ else if (stream instanceof UrlResourceStream)
+ {
+ UrlResourceStream urlResourceStream =
(UrlResourceStream)stream;
+ String url =
urlResourceStream.getURL().toExternalForm();
+ cache.put(key, new UrlResourceStreamReference(url));
+ }
+ }
+
+ /**
+ * Make a copy before returning an item from the cache as resource
streams are not thread-safe.
+ *
+ * @param key
+ * the cache key
+ * @return the cached File or Url resource stream
+ */
+ private IResourceStream getCopyFromCache(Key key)
+ {
+ final IResourceStreamReference orig = cache.get(key);
+ if (NullResourceStreamReference.INSTANCE == orig)
+ {
+ return null;
+ }
+
+ if (orig instanceof UrlResourceStreamReference)
+ {
+ UrlResourceStreamReference resourceStreamReference =
(UrlResourceStreamReference)orig;
+ String url = resourceStreamReference.getReference();
+ try
+ {
+ return new UrlResourceStream(new URL(url));
+ }
+ catch (MalformedURLException e)
+ {
+ return null;
+ }
+ }
+
+ if (orig instanceof FileResourceStreamReference)
+ {
+ FileResourceStreamReference resourceStreamReference =
(FileResourceStreamReference)orig;
+ String absolutePath =
resourceStreamReference.getReference();
+ return new FileResourceStream(new File(absolutePath));
+ }
+
+ return null;
+ }
+
+ public IResourceStream locate(Class<?> scope, String path, String
style, String variation,
+ Locale locale, String extension, boolean strict)
+ {
+ Key key = new Key(scope.getName(), path, locale, style,
variation);
+ IResourceStream resourceStream = getCopyFromCache(key);
+
+ if (resourceStream == null)
+ {
+ resourceStream = delegate.locate(scope, path, style,
variation, locale, extension,
+ strict);
+
+ updateCache(key, resourceStream);
+ }
+
+ return resourceStream;
+ }
+
+ public ResourceNameIterator newResourceNameIterator(String path, Locale
locale, String style,
+ String variation, String extension, boolean strict)
+ {
+ return delegate.newResourceNameIterator(path, locale, style,
variation, extension, strict);
+ }
+}
Added:
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/util/resource/locator/CachingResourceStreamLocatorTest.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/test/java/org/apache/wicket/util/resource/locator/CachingResourceStreamLocatorTest.java?rev=1080326&view=auto
==============================================================================
---
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/util/resource/locator/CachingResourceStreamLocatorTest.java
(added)
+++
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/util/resource/locator/CachingResourceStreamLocatorTest.java
Thu Mar 10 19:22:23 2011
@@ -0,0 +1,106 @@
+/*
+ * 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.wicket.util.resource.locator;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.io.File;
+import java.net.URL;
+
+import org.apache.wicket.util.resource.FileResourceStream;
+import org.apache.wicket.util.resource.UrlResourceStream;
+import org.junit.Test;
+
+/**
+ * <a href="https://issues.apache.org/jira/browse/WICKET-3511">WICKET-3511</a>
+ *
+ * @author mgrigorov
+ */
+public class CachingResourceStreamLocatorTest
+{
+
+ /**
+ * Tests NullResourceStreamReference
+ */
+ @Test
+ public void testNotExistingResource()
+ {
+
+ IResourceStreamLocator resourceStreamLocator =
mock(IResourceStreamLocator.class);
+
+ CachingResourceStreamLocator cachingLocator = new
CachingResourceStreamLocator(
+ resourceStreamLocator);
+
+ cachingLocator.locate(String.class, "path");
+ cachingLocator.locate(String.class, "path");
+
+ // there is no resource with that Key so expect two calls to
the delegate
+ verify(resourceStreamLocator, times(2)).locate(String.class,
"path");
+ }
+
+ /**
+ * Tests FileResourceStreamReference
+ */
+ @Test
+ public void testFileResource()
+ {
+ IResourceStreamLocator resourceStreamLocator =
mock(IResourceStreamLocator.class);
+
+ FileResourceStream frs = new FileResourceStream(new File("."));
+
+ when(
+ resourceStreamLocator.locate(String.class, "path",
"style", "variation", null,
+ "extension", true)).thenReturn(frs);
+
+ CachingResourceStreamLocator cachingLocator = new
CachingResourceStreamLocator(
+ resourceStreamLocator);
+
+ cachingLocator.locate(String.class, "path", "style",
"variation", null, "extension", true);
+ cachingLocator.locate(String.class, "path", "style",
"variation", null, "extension", true);
+
+ // there is a file resource with that Key so expect just one
call to the delegate
+ verify(resourceStreamLocator, times(1)).locate(String.class,
"path", "style", "variation",
+ null, "extension", true);
+ }
+
+ /**
+ * Tests UrlResourceStreamReference
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testUrlResource() throws Exception
+ {
+ IResourceStreamLocator resourceStreamLocator =
mock(IResourceStreamLocator.class);
+
+ UrlResourceStream urs = new UrlResourceStream(new
URL("file:///"));
+
+ when(resourceStreamLocator.locate(String.class,
"path")).thenReturn(urs);
+
+ CachingResourceStreamLocator cachingLocator = new
CachingResourceStreamLocator(
+ resourceStreamLocator);
+
+ cachingLocator.locate(String.class, "path");
+ cachingLocator.locate(String.class, "path");
+
+ // there is a url resource with that Key so expect just one
call to the delegate
+ verify(resourceStreamLocator, times(1)).locate(String.class,
"path");
+ }
+}