You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by eh...@apache.org on 2006/10/12 01:37:45 UTC
svn commit: r463068 -
/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/markup/html/PackageResource.java
Author: ehillenius
Date: Wed Oct 11 16:37:45 2006
New Revision: 463068
URL: http://svn.apache.org/viewvc?view=rev&rev=463068
Log:
don't throw a common exception (result in a stack trace and error page) for shared resources that are not found, but rather log a warning and abort the request, sending a 404 in case this is a web request
Modified:
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/markup/html/PackageResource.java
Modified: incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/markup/html/PackageResource.java
URL: http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/markup/html/PackageResource.java?view=diff&rev=463068&r1=463067&r2=463068
==============================================================================
--- incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/markup/html/PackageResource.java (original)
+++ incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/markup/html/PackageResource.java Wed Oct 11 16:37:45 2006
@@ -33,12 +33,17 @@
import java.util.jar.JarFile;
import java.util.regex.Pattern;
+import javax.servlet.http.HttpServletResponse;
+
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import wicket.Application;
+import wicket.RequestCycle;
import wicket.SharedResources;
import wicket.WicketRuntimeException;
+import wicket.protocol.http.WebRequestCycle;
+import wicket.protocol.http.servlet.AbortWithWebErrorCodeException;
import wicket.util.lang.PackageName;
import wicket.util.lang.Packages;
import wicket.util.resource.IResourceStream;
@@ -64,148 +69,6 @@
{
/**
- * Gets non-localized resources for a given set of criteria. Multiple
- * resource can be loaded for the same criteria if they match the pattern.
- * If no resources were found, this method returns null.
- *
- * @param scope
- * This argument will be used to get the class loader for loading
- * the package resource, and to determine what package it is in.
- * Typically this is the calling class/ the class in which you
- * call this method
- * @param pattern
- * Regexp pattern to match resources
- * @return The resources, never null but may be empty
- * @deprecated Will be removed in 2.0; contribute resources one by one
- * instead
- */
- public static PackageResource[] get(Class scope, Pattern pattern)
- {
- return get(scope, pattern, false);
- }
-
- /**
- * Gets non-localized resources for a given set of criteria. Multiple
- * resource can be loaded for the same criteria if they match the pattern.
- * If no resources were found, this method returns null.
- *
- * @param scope
- * This argument will be used to get the class loader for loading
- * the package resource, and to determine what package it is in.
- * Typically this is the calling class/ the class in which you
- * call this method
- * @param pattern
- * Regexp pattern to match resources
- * @param recurse
- * Whether this method should recurse into sub packages
- * @return The resources, never null but may be empty
- * @deprecated Will be removed in 2.0; contribute resources one by one
- * instead
- */
- public static PackageResource[] get(Class scope, Pattern pattern, boolean recurse)
- {
- final List resources = new ArrayList();
- String packageRef = Strings.replaceAll(PackageName.forClass(scope).getName(), ".", "/")
- .toString();
- ClassLoader loader = scope.getClassLoader();
- try
- {
- // loop through the resources of the package
- Enumeration packageResources = loader.getResources(packageRef);
- while (packageResources.hasMoreElements())
- {
- URL resource = (URL)packageResources.nextElement();
- URLConnection connection = resource.openConnection();
- if (connection instanceof JarURLConnection)
- {
- JarFile jf = ((JarURLConnection)connection).getJarFile();
- scanJarFile(scope, pattern, recurse, resources, packageRef, jf);
- }
- else
- {
- String absolutePath = scope.getResource("").toExternalForm();
- File basedir;
- URI uri;
- try
- {
- uri = new URI(absolutePath);
- }
- catch (URISyntaxException e)
- {
- throw new RuntimeException(e);
- }
- try
- {
- basedir = new File(uri);
- }
- catch (IllegalArgumentException e)
- {
- log.debug("Can't construct the uri as a file: " + absolutePath);
- // if this is throwen then the path is not really a
- // file. but could be a zip.
- String jarZipPart = uri.getSchemeSpecificPart();
- // lowercased for testing if jar/zip, but leave the real
- // filespec unchanged
- String lowerJarZipPart = jarZipPart.toLowerCase();
- int index = lowerJarZipPart.indexOf(".zip");
- if (index == -1)
- index = lowerJarZipPart.indexOf(".jar");
- if (index == -1)
- throw e;
-
- String filename = jarZipPart.substring(0, index + 4); // 4 =
- // len
- // of
- // ".jar"
- // or
- // ".zip"
- log.debug("trying the filename: " + filename + " to load as a zip/jar.");
- JarFile jarFile = new JarFile(filename, false);
- scanJarFile(scope, pattern, recurse, resources, packageRef, jarFile);
- return (PackageResource[])resources.toArray(new PackageResource[resources
- .size()]);
- }
- if (!basedir.isDirectory())
- {
- throw new IllegalStateException("unable to read resources from directory "
- + basedir);
- }
- // should not be necessary anymore
- // addResources(scope, pattern, resources, new
- // StringBuffer(""), basedir, recurse);
- }
- }
- }
- catch (IOException e)
- {
- throw new WicketRuntimeException(e);
- }
-
- return (PackageResource[])resources.toArray(new PackageResource[resources.size()]);
- }
-
- /* removed in 2.0 */
- private static void scanJarFile(Class scope, Pattern pattern, boolean recurse,
- final List resources, String packageRef, JarFile jf)
- {
- Enumeration enumeration = jf.entries();
- while (enumeration.hasMoreElements())
- {
- JarEntry je = (JarEntry)enumeration.nextElement();
- String name = je.getName();
- if (name.startsWith(packageRef))
- {
- name = name.substring(packageRef.length() + 1);
- if (pattern.matcher(name).matches() && (recurse || (name.indexOf('/') == -1)))
- {
- // we add the entry as a package resource
- resources.add(get(scope, name, null, null));
- }
- }
- }
- }
-
- /**
* Exception thrown when the creation of a package resource is not allowed.
*/
public static final class PackageResourceBlockedException extends WicketRuntimeException
@@ -403,6 +266,127 @@
}
/**
+ * Gets non-localized resources for a given set of criteria. Multiple
+ * resource can be loaded for the same criteria if they match the pattern.
+ * If no resources were found, this method returns null.
+ *
+ * @param scope
+ * This argument will be used to get the class loader for loading
+ * the package resource, and to determine what package it is in.
+ * Typically this is the calling class/ the class in which you
+ * call this method
+ * @param pattern
+ * Regexp pattern to match resources
+ * @return The resources, never null but may be empty
+ * @deprecated Will be removed in 2.0; contribute resources one by one
+ * instead
+ */
+ public static PackageResource[] get(Class scope, Pattern pattern)
+ {
+ return get(scope, pattern, false);
+ }
+
+ /**
+ * Gets non-localized resources for a given set of criteria. Multiple
+ * resource can be loaded for the same criteria if they match the pattern.
+ * If no resources were found, this method returns null.
+ *
+ * @param scope
+ * This argument will be used to get the class loader for loading
+ * the package resource, and to determine what package it is in.
+ * Typically this is the calling class/ the class in which you
+ * call this method
+ * @param pattern
+ * Regexp pattern to match resources
+ * @param recurse
+ * Whether this method should recurse into sub packages
+ * @return The resources, never null but may be empty
+ * @deprecated Will be removed in 2.0; contribute resources one by one
+ * instead
+ */
+ public static PackageResource[] get(Class scope, Pattern pattern, boolean recurse)
+ {
+ final List resources = new ArrayList();
+ String packageRef = Strings.replaceAll(PackageName.forClass(scope).getName(), ".", "/")
+ .toString();
+ ClassLoader loader = scope.getClassLoader();
+ try
+ {
+ // loop through the resources of the package
+ Enumeration packageResources = loader.getResources(packageRef);
+ while (packageResources.hasMoreElements())
+ {
+ URL resource = (URL)packageResources.nextElement();
+ URLConnection connection = resource.openConnection();
+ if (connection instanceof JarURLConnection)
+ {
+ JarFile jf = ((JarURLConnection)connection).getJarFile();
+ scanJarFile(scope, pattern, recurse, resources, packageRef, jf);
+ }
+ else
+ {
+ String absolutePath = scope.getResource("").toExternalForm();
+ File basedir;
+ URI uri;
+ try
+ {
+ uri = new URI(absolutePath);
+ }
+ catch (URISyntaxException e)
+ {
+ throw new RuntimeException(e);
+ }
+ try
+ {
+ basedir = new File(uri);
+ }
+ catch (IllegalArgumentException e)
+ {
+ log.debug("Can't construct the uri as a file: " + absolutePath);
+ // if this is throwen then the path is not really a
+ // file. but could be a zip.
+ String jarZipPart = uri.getSchemeSpecificPart();
+ // lowercased for testing if jar/zip, but leave the real
+ // filespec unchanged
+ String lowerJarZipPart = jarZipPart.toLowerCase();
+ int index = lowerJarZipPart.indexOf(".zip");
+ if (index == -1)
+ index = lowerJarZipPart.indexOf(".jar");
+ if (index == -1)
+ throw e;
+
+ String filename = jarZipPart.substring(0, index + 4); // 4 =
+ // len
+ // of
+ // ".jar"
+ // or
+ // ".zip"
+ log.debug("trying the filename: " + filename + " to load as a zip/jar.");
+ JarFile jarFile = new JarFile(filename, false);
+ scanJarFile(scope, pattern, recurse, resources, packageRef, jarFile);
+ return (PackageResource[])resources.toArray(new PackageResource[resources
+ .size()]);
+ }
+ if (!basedir.isDirectory())
+ {
+ throw new IllegalStateException("unable to read resources from directory "
+ + basedir);
+ }
+ // should not be necessary anymore
+ // addResources(scope, pattern, resources, new
+ // StringBuffer(""), basedir, recurse);
+ }
+ }
+ }
+ catch (IOException e)
+ {
+ throw new WicketRuntimeException(e);
+ }
+
+ return (PackageResource[])resources.toArray(new PackageResource[resources.size()]);
+ }
+
+ /**
* Gets a non-localized resource for a given set of criteria. Only one
* resource will be loaded for the same criteria.
*
@@ -450,6 +434,27 @@
return resource;
}
+ /* removed in 2.0 */
+ private static void scanJarFile(Class scope, Pattern pattern, boolean recurse,
+ final List resources, String packageRef, JarFile jf)
+ {
+ Enumeration enumeration = jf.entries();
+ while (enumeration.hasMoreElements())
+ {
+ JarEntry je = (JarEntry)enumeration.nextElement();
+ String name = je.getName();
+ if (name.startsWith(packageRef))
+ {
+ name = name.substring(packageRef.length() + 1);
+ if (pattern.matcher(name).matches() && (recurse || (name.indexOf('/') == -1)))
+ {
+ // we add the entry as a package resource
+ resources.add(get(scope, name, null, null));
+ }
+ }
+ }
+ }
+
/** The path to the resource */
private final String absolutePath;
@@ -550,8 +555,17 @@
// Check that resource was found
if (resourceStream == null)
{
- throw new WicketRuntimeException("Unable to find package resource [path = "
- + absolutePath + ", style = " + style + ", locale = " + locale + "]");
+ String msg = "Unable to find package resource [path = " + absolutePath
+ + ", style = " + style + ", locale = " + locale + "]";
+ log.warn(msg);
+ if (RequestCycle.get() instanceof WebRequestCycle)
+ {
+ throw new AbortWithWebErrorCodeException(HttpServletResponse.SC_NOT_FOUND, msg);
+ }
+ else
+ {
+ throw new WicketRuntimeException(msg);
+ }
}
this.locale = resourceStream.getLocale();
return resourceStream;