Commit a3cbf9c0 authored by Bill Lynch's avatar Bill Lynch Committed by bill

Added getResource.. method.


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@243 b35dd754-fafc-0310-a699-88a17e54d16e
parent 935ada80
...@@ -10,15 +10,17 @@ ...@@ -10,15 +10,17 @@
*/ */
package org.jivesoftware.util; package org.jivesoftware.util;
import java.io.InputStream;
/** /**
* A utility class to assist with loading classes by name. Many application servers use * A utility class to assist with loading classes or resources by name. Many application servers use
* custom classloaders, which will break uses of: * custom classloaders, which will break uses of:
* <pre> * <pre>
* Class.forName(className); * Class.forName(className);
* </pre> * </pre>
* <p/> *
* This utility attempts to load the class using a number of different mechanisms to work * This utility attempts to load the class or resource using a number of different mechanisms to
* around this problem. * work around this problem.
* *
* @author Matt Tucker * @author Matt Tucker
*/ */
...@@ -37,10 +39,21 @@ public class ClassUtils { ...@@ -37,10 +39,21 @@ public class ClassUtils {
return instance.loadClass(className); return instance.loadClass(className);
} }
private ClassUtils() { /**
* Loads the given resource as a stream.
*
* @param name the name of the resource that exists in the classpath.
* @return the resource as an input stream or <tt>null</tt> if the resource was not found.
*/
public static InputStream getResourceAsStream(String name) {
return instance.loadResource(name);
} }
/**
* Not instantiatable.
*/
private ClassUtils() {}
public Class loadClass(String className) throws ClassNotFoundException { public Class loadClass(String className) throws ClassNotFoundException {
Class theClass = null; Class theClass = null;
try { try {
...@@ -56,4 +69,15 @@ public class ClassUtils { ...@@ -56,4 +69,15 @@ public class ClassUtils {
} }
return theClass; return theClass;
} }
private InputStream loadResource(String name) {
InputStream in = getClass().getResourceAsStream(name);
if (in == null) {
in = Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
if (in == null) {
in = getClass().getClassLoader().getResourceAsStream(name);
}
}
return in;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment