Author: nbubna
Date: Tue Aug 7 16:00:38 2007
New Revision: 563694
URL: http://svn.apache.org/viewvc?view=rev&rev=563694
Log:
fix some classpath resource loading issues
Modified:
velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/ClassUtils.java
Modified:
velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/ClassUtils.java
URL:
http://svn.apache.org/viewvc/velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/ClassUtils.java?view=diff&rev=563694&r1=563693&r2=563694
==============================================================================
---
velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/ClassUtils.java
(original)
+++
velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/ClassUtils.java
Tue Aug 7 16:00:38 2007
@@ -54,6 +54,18 @@
return ClassUtils.class.getClassLoader();
}
+ private static final ClassLoader getCallerLoader(Object caller)
+ {
+ if (caller instanceof Class)
+ {
+ return ((Class)caller).getClassLoader();
+ }
+ else
+ {
+ return caller.getClass().getClassLoader();
+ }
+ }
+
/**
* Load a class with a given name.
* <p/>
@@ -98,43 +110,37 @@
* Load all resources with the specified name. If none are found, we
* prepend the name with '/' and try again.
*
- * This will attempt to load the resources from (in this order):
+ * This will attempt to load the resources from the following methods (in
order):
* <ul>
- * <li>the result Thread.currentThread().getContextClassLoader()</li>
- * <li>the result of ClassUtils.class.getClassLoader()</li>
+ *
<li>Thread.currentThread().getContextClassLoader().getResources(name)</li>
+ * <li>[EMAIL PROTECTED]
ClassUtils}.class.getClassLoader().getResources(name)</li>
+ * <li>[EMAIL PROTECTED] ClassUtils}.class.getResource(name)</li>
+ * <li>[EMAIL PROTECTED] #getCallerLoader(Object
caller)}.getResources(name)</li>
+ * <li>caller.getClass().getResource(name)</li>
* </ul>
*
* @param name The name of the resources to load
+ * @param caller The instance or [EMAIL PROTECTED] Class} calling this
method
*/
- public static List<URL> getResources(String name)
+ public static List<URL> getResources(String name, Object caller)
{
Set<URL> urls = new LinkedHashSet<URL>();
- Enumeration<URL> e;
- try
- {
- e = getThreadContextLoader().getResources(name);
- while (e.hasMoreElements())
- {
- urls.add(e.nextElement());
- }
- }
- catch (IOException ioe)
- {
- // ignore
- }
+ // try to load all from the current thread context classloader
+ addResources(name, urls, getThreadContextLoader());
- try
+ // try to load all from this class' classloader
+ if (!addResources(name, urls, getClassLoader()))
{
- e = getClassLoader().getResources(name);
- while (e.hasMoreElements())
- {
- urls.add(e.nextElement());
- }
+ // ok, try to load one directly from this class
+ addResource(name, urls, ClassUtils.class);
}
- catch (IOException ioe)
+
+ // try to load all from the classloader of the calling class
+ if (!addResources(name, urls, getCallerLoader(caller)))
{
- // ignore
+ // try to load one directly from the calling class
+ addResource(name, urls, caller.getClass());
}
if (!urls.isEmpty())
@@ -146,7 +152,7 @@
else if (!name.startsWith("/"))
{
// try again with a / in front of the name
- return getResources("/"+name);
+ return getResources("/"+name, caller);
}
else
{
@@ -154,23 +160,63 @@
}
}
+ private static final void addResource(String name, Set<URL> urls, Class c)
+ {
+ URL url = c.getResource(name);
+ if (url != null)
+ {
+ urls.add(url);
+ }
+ }
+
+ private static final boolean addResources(String name, Set<URL> urls,
+ ClassLoader loader)
+ {
+ boolean foundSome = false;
+ try
+ {
+ Enumeration<URL> e = loader.getResources(name);
+ while (e.hasMoreElements())
+ {
+ urls.add(e.nextElement());
+ foundSome = true;
+ }
+ }
+ catch (IOException ioe)
+ {
+ // ignore
+ }
+ return foundSome;
+ }
+
/**
* Load a given resource.
* <p/>
* This method will try to load the resource using the following methods
(in order):
* <ul>
- * <li>From [EMAIL PROTECTED]
Thread.currentThread().getContextClassLoader()}
- * <li>From [EMAIL PROTECTED] ClassUtils.class.getClassLoader()}
+ *
<li>Thread.currentThread().getContextClassLoader().getResource(name)</li>
+ * <li>[EMAIL PROTECTED]
ClassUtils}.class.getClassLoader().getResource(name)</li>
+ * <li>[EMAIL PROTECTED] ClassUtils}.class.getResource(name)</li>
+ * <li>caller.getClass().getResource(name)</li>
* </ul>
*
* @param name The name of the resource to load
+ * @param caller The instance or [EMAIL PROTECTED] Class} calling this
method
*/
- public static URL getResource(String name)
+ public static URL getResource(String name, Object caller)
{
URL url = getThreadContextLoader().getResource(name);
if (url == null)
{
url = getClassLoader().getResource(name);
+ if (url == null)
+ {
+ url = ClassUtils.class.getResource(name);
+ if (url == null)
+ {
+ url = caller.getClass().getResource(name);
+ }
+ }
}
return url;
}
@@ -181,10 +227,11 @@
* The algorithm used to find the resource is given in getResource()
*
* @param name The name of the resource to load
+ * @param caller The instance or [EMAIL PROTECTED] Class} calling this
method
*/
- public static InputStream getResourceAsStream(String name)
+ public static InputStream getResourceAsStream(String name, Object caller)
{
- URL url = getResource(name);
+ URL url = getResource(name, caller);
try
{
return (url != null) ? url.openStream() : null;