work in progress
Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/45db0cd5 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/45db0cd5 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/45db0cd5 Branch: refs/heads/sandbox/resourcefinder Commit: 45db0cd5777215e12197d3fd78938aee48010ace Parents: 55303c0 Author: Carl-Eric Menzel <cmen...@wicketbuch.de> Authored: Wed Jun 27 14:20:31 2012 +0200 Committer: Carl-Eric Menzel <cmen...@wicketbuch.de> Committed: Wed Jun 27 14:59:31 2012 +0200 ---------------------------------------------------------------------- .../main/java/org/apache/wicket/Application.java | 4 +- .../util/resource/ClassPathResourceFinder.java | 85 ++++++++++++++ .../wicket/protocol/http/WebApplication.java | 8 ++ .../wicket/util/file/WebApplicationPathTest.java | 5 +- .../apache/wicket/util/resource/ResourceTest.java | 66 ++++++----- .../wicket/util/file/ClasspathResourceFinder.java | 89 --------------- .../java/org/apache/wicket/util/file/Path.java | 13 ++- .../org/apache/wicket/velocity/Initializer.java | 6 +- 8 files changed, 147 insertions(+), 129 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/45db0cd5/wicket-core/src/main/java/org/apache/wicket/Application.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/Application.java b/wicket-core/src/main/java/org/apache/wicket/Application.java index 8164b1a..6a5ba84 100644 --- a/wicket-core/src/main/java/org/apache/wicket/Application.java +++ b/wicket-core/src/main/java/org/apache/wicket/Application.java @@ -37,6 +37,7 @@ import org.apache.wicket.application.IComponentInstantiationListener; import org.apache.wicket.core.request.mapper.IMapperContext; import org.apache.wicket.core.util.lang.PropertyResolver; import org.apache.wicket.core.util.lang.WicketObjects; +import org.apache.wicket.core.util.resource.ClassPathResourceFinder; import org.apache.wicket.event.IEvent; import org.apache.wicket.event.IEventSink; import org.apache.wicket.javascript.DefaultJavaScriptCompressor; @@ -108,7 +109,6 @@ import org.apache.wicket.settings.def.ResourceSettings; import org.apache.wicket.settings.def.SecuritySettings; import org.apache.wicket.settings.def.StoreSettings; import org.apache.wicket.util.IProvider; -import org.apache.wicket.util.file.ClasspathResourceFinder; import org.apache.wicket.util.file.Folder; import org.apache.wicket.util.file.IResourceFinder; import org.apache.wicket.util.file.Path; @@ -697,7 +697,7 @@ public abstract class Application implements UnboundListener, IEventSink pageSettings.addComponentResolver(new WicketMessageTagHandler()); pageSettings.addComponentResolver(new WicketContainerResolver()); - getResourceSettings().getResourceFinders().add(new ClasspathResourceFinder("")); + getResourceSettings().getResourceFinders().add(new ClassPathResourceFinder("")); // Install button image resource factory getResourceSettings().addResourceFactory("buttonFactory", http://git-wip-us.apache.org/repos/asf/wicket/blob/45db0cd5/wicket-core/src/main/java/org/apache/wicket/core/util/resource/ClassPathResourceFinder.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/core/util/resource/ClassPathResourceFinder.java b/wicket-core/src/main/java/org/apache/wicket/core/util/resource/ClassPathResourceFinder.java new file mode 100644 index 0000000..5a71c2b --- /dev/null +++ b/wicket-core/src/main/java/org/apache/wicket/core/util/resource/ClassPathResourceFinder.java @@ -0,0 +1,85 @@ +/* + * 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.core.util.resource; + +import java.net.URL; + +import org.apache.wicket.util.file.IResourceFinder; +import org.apache.wicket.util.resource.IResourceStream; +import org.apache.wicket.util.string.Strings; + +public class ClassPathResourceFinder implements IResourceFinder +{ + private final String prefix; + + public ClassPathResourceFinder(String prefix) + { + if (Strings.isEmpty(prefix)) + { + this.prefix = ""; + } + else + { + this.prefix = prefix + "/"; + } + } + + @Override + public IResourceStream find(Class<?> clazz, String path) + { + String fullPath = prefix + path; + IResourceStream resourceStream; + if (clazz != null) + { + resourceStream = getResourceStream(clazz.getClassLoader(), fullPath); + if (resourceStream != null) + { + return resourceStream; + } + } + + // use context classloader when no specific classloader is set + // (package resources for instance) + resourceStream = getResourceStream(Thread.currentThread().getContextClassLoader(), fullPath); + if (resourceStream != null) + { + return resourceStream; + } + + // use Wicket classloader when no specific classloader is set + resourceStream = getResourceStream(getClass().getClassLoader(), fullPath); + if (resourceStream != null) + { + return resourceStream; + } + + return null; + } + + private IResourceStream getResourceStream(ClassLoader classLoader, String path) + { + if (classLoader != null) + { + URL url = classLoader.getResource(path); + if (url != null) + { + return new UrlResourceStream(url); + } + } + return null; + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/45db0cd5/wicket-core/src/main/java/org/apache/wicket/protocol/http/WebApplication.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/protocol/http/WebApplication.java b/wicket-core/src/main/java/org/apache/wicket/protocol/http/WebApplication.java index f0a8bd2..fcb145e 100644 --- a/wicket-core/src/main/java/org/apache/wicket/protocol/http/WebApplication.java +++ b/wicket-core/src/main/java/org/apache/wicket/protocol/http/WebApplication.java @@ -37,6 +37,7 @@ import org.apache.wicket.core.request.mapper.MountedMapper; import org.apache.wicket.core.request.mapper.PackageMapper; import org.apache.wicket.core.request.mapper.ResourceMapper; import org.apache.wicket.core.util.file.WebApplicationPath; +import org.apache.wicket.core.util.resource.ClassPathResourceFinder; import org.apache.wicket.markup.MarkupType; import org.apache.wicket.markup.head.CssHeaderItem; import org.apache.wicket.markup.head.JavaScriptHeaderItem; @@ -129,6 +130,8 @@ public abstract class WebApplication extends Application /** Log. */ private static final Logger log = LoggerFactory.getLogger(WebApplication.class); + public static final String META_INF_RESOURCES = "META-INF/resources"; + private ServletContext servletContext; private final AjaxRequestTargetListenerCollection ajaxRequestTargetListeners; @@ -614,6 +617,11 @@ public abstract class WebApplication extends Application { super.internalInit(); + getResourceSettings().getResourceFinders().add( + new WebApplicationPath(getServletContext(), "")); + getResourceSettings().getResourceFinders().add( + new ClassPathResourceFinder(META_INF_RESOURCES)); + // Set default error pages for HTML markup getApplicationSettings().setPageExpiredErrorPage(PageExpiredErrorPage.class); getApplicationSettings().setInternalErrorPage(InternalErrorPage.class); http://git-wip-us.apache.org/repos/asf/wicket/blob/45db0cd5/wicket-core/src/test/java/org/apache/wicket/util/file/WebApplicationPathTest.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/java/org/apache/wicket/util/file/WebApplicationPathTest.java b/wicket-core/src/test/java/org/apache/wicket/util/file/WebApplicationPathTest.java index f88d62e..fc71fe4 100644 --- a/wicket-core/src/test/java/org/apache/wicket/util/file/WebApplicationPathTest.java +++ b/wicket-core/src/test/java/org/apache/wicket/util/file/WebApplicationPathTest.java @@ -24,6 +24,7 @@ import org.apache.wicket.core.util.file.WebApplicationPath; import org.apache.wicket.util.resource.IResourceStream; import org.junit.Assert; import org.junit.Test; +import org.mockito.Matchers; import org.mockito.Mockito; /** @@ -37,9 +38,9 @@ public class WebApplicationPathTest extends Assert URL webUrl = new URL("file://dummyFile"); ServletContext context = Mockito.mock(ServletContext.class); - Mockito.when(context.getResource(Mockito.any(String.class))).thenReturn(webUrl); + Mockito.when(context.getResource(Matchers.any(String.class))).thenReturn(webUrl); - WebApplicationPath path = new WebApplicationPath(context); + WebApplicationPath path = new WebApplicationPath(context, ""); IResourceStream resourceStream = path.find(String.class, "WEB-INF/web.xml"); assertNull(resourceStream); http://git-wip-us.apache.org/repos/asf/wicket/blob/45db0cd5/wicket-core/src/test/java/org/apache/wicket/util/resource/ResourceTest.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/java/org/apache/wicket/util/resource/ResourceTest.java b/wicket-core/src/test/java/org/apache/wicket/util/resource/ResourceTest.java index 3737bdc..94d1362 100644 --- a/wicket-core/src/test/java/org/apache/wicket/util/resource/ResourceTest.java +++ b/wicket-core/src/test/java/org/apache/wicket/util/resource/ResourceTest.java @@ -23,10 +23,12 @@ import java.net.URL; import java.util.Locale; import org.apache.wicket.WicketTestCase; +import org.apache.wicket.core.util.resource.ClassPathResourceFinder; import org.apache.wicket.core.util.resource.UrlResourceStream; import org.apache.wicket.core.util.resource.locator.IResourceStreamLocator; import org.apache.wicket.core.util.resource.locator.ResourceStreamLocator; import org.apache.wicket.util.file.Folder; +import org.apache.wicket.util.file.IResourceFinder; import org.apache.wicket.util.file.Path; import org.apache.wicket.util.string.Strings; import org.junit.Test; @@ -57,16 +59,16 @@ public class ResourceTest extends WicketTestCase /** * - * @param sourcePath + * @param finders * @param style * @param variation * @param locale * @param extension */ - public void createAndTestResource(Path sourcePath, String style, String variation, + public void createAndTestResource(IResourceFinder[] finders, String style, String variation, Locale locale, String extension) { - IResourceStreamLocator locator = new ResourceStreamLocator(sourcePath); + IResourceStreamLocator locator = new ResourceStreamLocator(finders); IResourceStream resource = locator.locate(this.getClass(), this.getClass() .getName() .replace('.', '/'), style, variation, locale, "txt", false); @@ -75,36 +77,36 @@ public class ResourceTest extends WicketTestCase /** * - * @param sourcePath + * @param finders */ - public void executeMultiple(Path sourcePath) + public void executeMultiple(IResourceFinder... finders) { - createAndTestResource(sourcePath, null, null, null, ""); - createAndTestResource(sourcePath, "style", null, null, "_style"); - - createAndTestResource(sourcePath, null, null, locale_de, "_de"); - createAndTestResource(sourcePath, null, null, locale_de_DE, "_de_DE"); - createAndTestResource(sourcePath, null, null, locale_de_DE_POSIX, "_de_DE_POSIX"); - createAndTestResource(sourcePath, null, null, locale_de_POSIX, "_de__POSIX"); - createAndTestResource(sourcePath, null, null, locale_de_CH, "_de"); - - createAndTestResource(sourcePath, "style", null, locale_de, "_style_de"); - createAndTestResource(sourcePath, "style", null, locale_de_DE, "_style_de_DE"); - createAndTestResource(sourcePath, "style", null, locale_de_DE_POSIX, "_style_de_DE_POSIX"); - createAndTestResource(sourcePath, "style", null, locale_de_POSIX, "_style_de__POSIX"); - createAndTestResource(sourcePath, "style", null, locale_de_CH, "_style_de"); - - createAndTestResource(sourcePath, null, null, locale_en, ""); - createAndTestResource(sourcePath, null, null, locale_en_US, ""); - createAndTestResource(sourcePath, null, null, locale_en_US_WIN, ""); - createAndTestResource(sourcePath, null, null, locale_en_WIN, ""); - createAndTestResource(sourcePath, "style", null, locale_en_WIN, "_style"); - - createAndTestResource(sourcePath, null, null, locale_fr, "_fr"); - createAndTestResource(sourcePath, null, null, locale_fr_FR, "_fr"); - createAndTestResource(sourcePath, null, null, locale_fr_FR_WIN, "_fr"); - createAndTestResource(sourcePath, null, null, locale_fr_WIN, "_fr"); - createAndTestResource(sourcePath, "style", null, locale_fr_WIN, "_style"); + createAndTestResource(finders, null, null, null, ""); + createAndTestResource(finders, "style", null, null, "_style"); + + createAndTestResource(finders, null, null, locale_de, "_de"); + createAndTestResource(finders, null, null, locale_de_DE, "_de_DE"); + createAndTestResource(finders, null, null, locale_de_DE_POSIX, "_de_DE_POSIX"); + createAndTestResource(finders, null, null, locale_de_POSIX, "_de__POSIX"); + createAndTestResource(finders, null, null, locale_de_CH, "_de"); + + createAndTestResource(finders, "style", null, locale_de, "_style_de"); + createAndTestResource(finders, "style", null, locale_de_DE, "_style_de_DE"); + createAndTestResource(finders, "style", null, locale_de_DE_POSIX, "_style_de_DE_POSIX"); + createAndTestResource(finders, "style", null, locale_de_POSIX, "_style_de__POSIX"); + createAndTestResource(finders, "style", null, locale_de_CH, "_style_de"); + + createAndTestResource(finders, null, null, locale_en, ""); + createAndTestResource(finders, null, null, locale_en_US, ""); + createAndTestResource(finders, null, null, locale_en_US_WIN, ""); + createAndTestResource(finders, null, null, locale_en_WIN, ""); + createAndTestResource(finders, "style", null, locale_en_WIN, "_style"); + + createAndTestResource(finders, null, null, locale_fr, "_fr"); + createAndTestResource(finders, null, null, locale_fr_FR, "_fr"); + createAndTestResource(finders, null, null, locale_fr_FR_WIN, "_fr"); + createAndTestResource(finders, null, null, locale_fr_WIN, "_fr"); + createAndTestResource(finders, "style", null, locale_fr_WIN, "_style"); } /** @@ -114,7 +116,7 @@ public class ResourceTest extends WicketTestCase public void locate() { // Execute without source path - executeMultiple(new Path()); + executeMultiple(new Path("/"), new ClassPathResourceFinder("")); // Determine source path IResourceStreamLocator locator = new ResourceStreamLocator(); http://git-wip-us.apache.org/repos/asf/wicket/blob/45db0cd5/wicket-util/src/main/java/org/apache/wicket/util/file/ClasspathResourceFinder.java ---------------------------------------------------------------------- diff --git a/wicket-util/src/main/java/org/apache/wicket/util/file/ClasspathResourceFinder.java b/wicket-util/src/main/java/org/apache/wicket/util/file/ClasspathResourceFinder.java deleted file mode 100644 index 0fe8e73..0000000 --- a/wicket-util/src/main/java/org/apache/wicket/util/file/ClasspathResourceFinder.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * 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.file; - -import java.net.URL; - -import org.apache.wicket.util.resource.IResourceStream; -import org.apache.wicket.util.string.Strings; - -public class ClasspathResourceFinder implements IResourceFinder -{ - - private final String prefix; - - public ClasspathResourceFinder(String prefix) - { - if (Strings.isEmpty(prefix)) - { - this.prefix = ""; - } - else if (prefix.endsWith("/")) - { - this.prefix = prefix; - } - else - { - this.prefix = prefix + "/"; - } - } - - @Override - public IResourceStream find(Class<?> clazz, String path) - { - String fullPath = prefix + path; - IResourceStream resourceStream; - if (clazz != null) - { - resourceStream = getResourceStream(clazz.getClassLoader(), fullPath); - if (resourceStream != null) - { - return resourceStream; - } - } - - // use context classloader when no specific classloader is set - // (package resources for instance) - resourceStream = getResourceStream(Thread.currentThread().getContextClassLoader(), fullPath); - if (resourceStream != null) - { - return resourceStream; - } - - // use Wicket classloader when no specific classloader is set - resourceStream = getResourceStream(getClass().getClassLoader(), fullPath); - if (resourceStream != null) - { - return resourceStream; - } - - return null; - } - - private IResourceStream getResourceStream(ClassLoader classLoader, String path) - { - URL url = classLoader.getResource(path); - if (url != null) - { - return new UrlResourceStream(url); - } - else - { - return null; - } - } -} http://git-wip-us.apache.org/repos/asf/wicket/blob/45db0cd5/wicket-util/src/main/java/org/apache/wicket/util/file/Path.java ---------------------------------------------------------------------- diff --git a/wicket-util/src/main/java/org/apache/wicket/util/file/Path.java b/wicket-util/src/main/java/org/apache/wicket/util/file/Path.java index 58cbd5b..cd62c98 100644 --- a/wicket-util/src/main/java/org/apache/wicket/util/file/Path.java +++ b/wicket-util/src/main/java/org/apache/wicket/util/file/Path.java @@ -35,7 +35,18 @@ public class Path implements IResourceFinder * Constructor * * @param folder - * A single folder to add to the path + * The folder to look in + */ + public Path(final String folder) + { + this(new Folder(folder)); + } + + /** + * Constructor + * + * @param folder + * The folder to look in */ public Path(final Folder folder) { http://git-wip-us.apache.org/repos/asf/wicket/blob/45db0cd5/wicket-velocity/src/main/java/org/apache/wicket/velocity/Initializer.java ---------------------------------------------------------------------- diff --git a/wicket-velocity/src/main/java/org/apache/wicket/velocity/Initializer.java b/wicket-velocity/src/main/java/org/apache/wicket/velocity/Initializer.java index 9ccfe74..e6f2fe1 100644 --- a/wicket-velocity/src/main/java/org/apache/wicket/velocity/Initializer.java +++ b/wicket-velocity/src/main/java/org/apache/wicket/velocity/Initializer.java @@ -26,8 +26,8 @@ import org.apache.velocity.app.Velocity; import org.apache.wicket.Application; import org.apache.wicket.IInitializer; import org.apache.wicket.WicketRuntimeException; -import org.apache.wicket.protocol.http.WebApplication; import org.apache.wicket.core.util.file.WebApplicationPath; +import org.apache.wicket.protocol.http.WebApplication; import org.apache.wicket.util.io.IOUtils; import org.apache.wicket.util.resource.IResourceStream; import org.apache.wicket.util.resource.ResourceStreamNotFoundException; @@ -89,8 +89,8 @@ public class Initializer implements IInitializer if (null != propertiesFolder) { - WebApplicationPath webPath = new WebApplicationPath(servletContext); - webPath.add(propertiesFolder); + WebApplicationPath webPath = new WebApplicationPath(servletContext, + propertiesFolder); IResourceStream stream = webPath.find(Initializer.class, velocityPropertiesFile); InputStream is = null; try