/*
 * $Id: HelloWorldApplication.java 3646 2006-01-04 13:32:14 -0800 (Wed, 04 Jan
 * 2006) ivaynberg $ $Revision: 3646 $ $Date: 2006-01-04 13:32:14 -0800 (Wed, 04
 * Jan 2006) $
 * 
 * ==================================================================== Licensed
 * 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 wicket.examples.customresourceloading;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Locale;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import wicket.WicketRuntimeException;
import wicket.examples.WicketExampleApplication;
import wicket.protocol.http.WebApplication;
import wicket.util.resource.IResourceStream;
import wicket.util.resource.UrlResourceStream;
import wicket.util.resource.locator.ClassLoaderResourceStreamLocator;
import wicket.util.resource.locator.IResourceStreamLocator;
import wicket.util.string.Strings;

/**
 * Application class for the custom resource loading example.
 * 
 * @author Eelco Hillenius
 */
public class CustomResourceLoadingApplication extends WicketExampleApplication
{
	/** log. */
	private static Log log = LogFactory.getLog(CustomResourceLoadingApplication.class);

	/**
	 * Custom implementation of {@link IResourceStreamLocator}.
	 */
	private final class CustomResourceStreamLocator implements IResourceStreamLocator
	{
		/**
		 * Locator to fallback on. Always a good idea to do this, because
		 * packaged custom components etc <strong>will</strong> depend in this
		 * loading.
		 */
		private ClassLoaderResourceStreamLocator classLoaderLocator = new ClassLoaderResourceStreamLocator();

		/**
		 * @see wicket.util.resource.locator.IResourceStreamLocator#locate(java.lang.Class,
		 *      java.lang.String, java.lang.String, java.util.Locale,
		 *      java.lang.String)
		 */
		public IResourceStream locate(Class clazz, String path, String style, Locale locale,
				String extension)
		{
			// Log attempt
			if (log.isDebugEnabled())
			{
				log.debug("Attempting to locate resource '" + path
						+ "' using classloader the servlet context");
			}

			String location;
			if (clazz == AlternativePageFromWebContext.class)
			{
				// this custom case disregards the path and tries it's own
				// scheme
				location = "/WEB-INF/templates/"
						+ Strings.lastPathComponent(AlternativePageFromWebContext.class.getName(),
								'.') + "." + extension;
			}
			else
			{
				// use the normal package to path conversion of the passed in
				// path variable
				location = "/WEB-INF/templates/" + path + "." + extension;
			}
			URL url;
			try
			{
				// try to load the resource from the web context
				url = getWicketServlet().getServletContext().getResource(location);
				if (url != null)
				{
					return new UrlResourceStream(url);
				}
			}
			catch (MalformedURLException e)
			{
				throw new WicketRuntimeException(e);
			}

			// resource not found; fall back on class loading
			return classLoaderLocator.locate(clazz, path, style, locale, extension);
		}

	}

	/**
	 * Constructor.
	 */
	public CustomResourceLoadingApplication()
	{
	}

	/**
	 * @see wicket.Application#getHomePage()
	 */
	public Class getHomePage()
	{
		return Index.class;
	}

	/**
	 * @see WebApplication#init()
	 */
	protected void init()
	{
		// instruct the application to use our custom resource stream locator
		getResourceSettings().setResourceStreamLocator(new CustomResourceStreamLocator());
	}
}

@@ -915,33 +915,8 @@
 URL;
-%0Aimport java.util.Locale;
 %0A%0Aim
@@ -1214,32 +1214,99 @@
 ResourceStream;%0A
+import wicket.util.resource.locator.AbstractResourceStreamLocator;%0A
 import wicket.ut
@@ -1860,20 +1860,24 @@
 tor 
-implements I
+extends Abstract
 Reso
@@ -2206,33 +2206,40 @@
 esource.locator.
-I
+Abstract
 ResourceStreamLo
@@ -2272,72 +2272,8 @@
 ss,%0A
-%09%09 *      java.lang.String, java.lang.String, java.util.Locale,%0A
 %09%09 *
@@ -2305,21 +2305,24 @@
 %09 */%0A%09%09p
-ublic
+rotected
  IResour
@@ -2365,59 +2365,8 @@
 path
-, String style, Locale locale,%0A%09%09%09%09String extension
 )%0A%09%09
@@ -2695,110 +2695,129 @@
 %09%09%09%09
-location = %22/WEB-INF/templates/%22%0A%09%09%09%09%09%09+ Strings.lastPathComponent(AlternativePageFromWebContext.class
+String extension = path.substring(path.lastIndexOf('.') + 1);%0A%09%09%09%09String simpleFileName = Strings.lastPathComponent(clazz
 .get
@@ -2827,21 +2827,68 @@
 e(),
-%0A%09%09%09%09%09%09%09%09'.')
+ '.');%0A%09%09%09%09location = %22/WEB-INF/templates/%22 + simpleFileName
  + %22
@@ -3051,34 +3051,16 @@
 %22 + path
- + %22.%22 + extension
 ;%0A%09%09%09%7D%0A%09
@@ -3476,34 +3476,8 @@
 path
-, style, locale, extension
 );%0A%09

extend from AbstractResourceStreamLocator instead of directly implementing the interface as this would be a better real world use (takes the locale and style etc into account).