You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by cm...@apache.org on 2012/06/22 14:14:24 UTC

git commit: WICKET-4618 Make resource lookup prefixes configurable for 1.5

Updated Branches:
  refs/heads/wicket-1.5.x 6f7236897 -> d274f682b


WICKET-4618 Make resource lookup prefixes configurable for 1.5

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

Branch: refs/heads/wicket-1.5.x
Commit: d274f682b1d8b58460fd950fd982f6f8449ef6b9
Parents: 6f72368
Author: Carl-Eric Menzel <cm...@wicketbuch.de>
Authored: Fri Jun 22 14:10:01 2012 +0200
Committer: Carl-Eric Menzel <cm...@wicketbuch.de>
Committed: Fri Jun 22 14:10:58 2012 +0200

----------------------------------------------------------------------
 .../resource/locator/ResourceStreamLocator.java    |   39 ++++++--
 .../apache/wicket/util/resource/locator/foo.txt    |    1 +
 .../locator/ResourceStreamLocatorTest.java         |   75 +++++++++++++++
 .../apache/wicket/util/resource/locator/foo.txt    |    1 +
 4 files changed, 107 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/d274f682/wicket-core/src/main/java/org/apache/wicket/util/resource/locator/ResourceStreamLocator.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/util/resource/locator/ResourceStreamLocator.java b/wicket-core/src/main/java/org/apache/wicket/util/resource/locator/ResourceStreamLocator.java
index 44d2c48..2e54ff4 100644
--- a/wicket-core/src/main/java/org/apache/wicket/util/resource/locator/ResourceStreamLocator.java
+++ b/wicket-core/src/main/java/org/apache/wicket/util/resource/locator/ResourceStreamLocator.java
@@ -17,6 +17,8 @@
 package org.apache.wicket.util.resource.locator;
 
 import java.net.URL;
+import java.util.LinkedList;
+import java.util.List;
 import java.util.Locale;
 
 import org.apache.wicket.Application;
@@ -64,6 +66,12 @@ public class ResourceStreamLocator implements IResourceStreamLocator
 	/** If null, the application registered finder will be used */
 	private IResourceFinder finder;
 
+	private final List<String> classpathLocationPrefixes = new LinkedList<String>();
+	{
+		classpathLocationPrefixes.add("");
+		classpathLocationPrefixes.add("META-INF/resources");
+	}
+
 	/**
 	 * Constructor
 	 */
@@ -201,18 +209,16 @@ public class ResourceStreamLocator implements IResourceStreamLocator
 				classLoader);
 		}
 
-		// Try loading path using classloader
-		URL url = classLoader.getResource(path);
-		if (url == null)
+		for (String prefix : classpathLocationPrefixes)
 		{
-			// maybe it is in the Servlet 3.0 like directory
-			url = classLoader.getResource("META-INF/resources/" + path);
+			String fullPath = prefix.length() > 0 ? prefix + "/" + path : path;
+			URL url = classLoader.getResource(fullPath);
+			if (url != null)
+			{
+				return new UrlResourceStream(url);
+			}
 		}
 
-		if (url != null)
-		{
-			return new UrlResourceStream(url);
-		}
 		return null;
 	}
 
@@ -270,4 +276,19 @@ public class ResourceStreamLocator implements IResourceStreamLocator
 
 		return new ResourceNameIterator(realPath, style, variation, locale, realExtension, strict);
 	}
+
+	/**
+	 * The list of prefixes that are added to a path when trying to find resources in the classpath.
+	 * By default, these are:
+	 * <ul>
+	 * <li><code>""</code> - no prefix</li>
+	 * <li><code>"META-INF/resources"</code> - for Servlet 3.0 compatibility.</li>
+	 * </ul>
+	 * 
+	 * @return
+	 */
+	public List<String> getClasspathLocationPrefixes()
+	{
+		return classpathLocationPrefixes;
+	}
 }

http://git-wip-us.apache.org/repos/asf/wicket/blob/d274f682/wicket-core/src/test/java/ResourceStreamLocatorTest/some/path/org/apache/wicket/util/resource/locator/foo.txt
----------------------------------------------------------------------
diff --git a/wicket-core/src/test/java/ResourceStreamLocatorTest/some/path/org/apache/wicket/util/resource/locator/foo.txt b/wicket-core/src/test/java/ResourceStreamLocatorTest/some/path/org/apache/wicket/util/resource/locator/foo.txt
new file mode 100644
index 0000000..d3009c3
--- /dev/null
+++ b/wicket-core/src/test/java/ResourceStreamLocatorTest/some/path/org/apache/wicket/util/resource/locator/foo.txt
@@ -0,0 +1 @@
+with prefix

http://git-wip-us.apache.org/repos/asf/wicket/blob/d274f682/wicket-core/src/test/java/org/apache/wicket/util/resource/locator/ResourceStreamLocatorTest.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/test/java/org/apache/wicket/util/resource/locator/ResourceStreamLocatorTest.java b/wicket-core/src/test/java/org/apache/wicket/util/resource/locator/ResourceStreamLocatorTest.java
new file mode 100644
index 0000000..a9c36b4
--- /dev/null
+++ b/wicket-core/src/test/java/org/apache/wicket/util/resource/locator/ResourceStreamLocatorTest.java
@@ -0,0 +1,75 @@
+/*
+ * 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.junit.Assert.*;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+
+import org.apache.wicket.util.file.Path;
+import org.apache.wicket.util.lang.Packages;
+import org.apache.wicket.util.resource.IResourceStream;
+import org.junit.Test;
+
+public class ResourceStreamLocatorTest
+{
+	@Test
+	public void defaultPath() throws Exception
+	{
+		IResourceStream stream = new ResourceStreamLocator(new Path()).locate(
+			ResourceStreamLocatorTest.class,
+			Packages.absolutePath(ResourceStreamLocatorTest.class, "foo.txt"));
+		assertEquals("no prefix",
+			new BufferedReader(new InputStreamReader(stream.getInputStream())).readLine());
+	}
+
+	@Test
+	public void addedPath() throws Exception
+	{
+		final ResourceStreamLocator rsl = new ResourceStreamLocator(new Path());
+		rsl.getClasspathLocationPrefixes().add("some/invalid/path");
+		IResourceStream stream = rsl.locate(ResourceStreamLocatorTest.class,
+			Packages.absolutePath(ResourceStreamLocatorTest.class, "foo.txt"));
+		assertEquals("default path is first and should have had priority!", "no prefix",
+			new BufferedReader(new InputStreamReader(stream.getInputStream())).readLine());
+	}
+
+	@Test
+	public void replacedPathWithTrailingSlash() throws Exception
+	{
+		final ResourceStreamLocator rsl = new ResourceStreamLocator(new Path());
+		rsl.getClasspathLocationPrefixes().clear();
+		rsl.getClasspathLocationPrefixes().add("ResourceStreamLocatorTest/some/path/");
+		IResourceStream stream = rsl.locate(ResourceStreamLocatorTest.class,
+			Packages.absolutePath(ResourceStreamLocatorTest.class, "foo.txt"));
+		assertEquals("should have found the version with prefix!", "with prefix",
+			new BufferedReader(new InputStreamReader(stream.getInputStream())).readLine());
+	}
+
+	@Test
+	public void replacedPathWithoutTrailingSlash() throws Exception
+	{
+		final ResourceStreamLocator rsl = new ResourceStreamLocator(new Path());
+		rsl.getClasspathLocationPrefixes().clear();
+		rsl.getClasspathLocationPrefixes().add("ResourceStreamLocatorTest/some/path");
+		IResourceStream stream = rsl.locate(ResourceStreamLocatorTest.class,
+			Packages.absolutePath(ResourceStreamLocatorTest.class, "foo.txt"));
+		assertEquals("should have found the version with prefix!", "with prefix",
+			new BufferedReader(new InputStreamReader(stream.getInputStream())).readLine());
+	}
+}

http://git-wip-us.apache.org/repos/asf/wicket/blob/d274f682/wicket-core/src/test/java/org/apache/wicket/util/resource/locator/foo.txt
----------------------------------------------------------------------
diff --git a/wicket-core/src/test/java/org/apache/wicket/util/resource/locator/foo.txt b/wicket-core/src/test/java/org/apache/wicket/util/resource/locator/foo.txt
new file mode 100644
index 0000000..824e61a
--- /dev/null
+++ b/wicket-core/src/test/java/org/apache/wicket/util/resource/locator/foo.txt
@@ -0,0 +1 @@
+no prefix