Author: lmeadors
Date: Fri Aug 7 20:49:44 2009
New Revision: 802181
URL: http://svn.apache.org/viewvc?rev=802181&view=rev
Log:
Making the Resources class look for stuff more harder.
Added:
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/io/ClassLoaderWrapper.java
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/io/ClassLoaderWrapperTest.java
Modified:
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/io/Resources.java
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/io/ResourcesTest.java
Added:
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/io/ClassLoaderWrapper.java
URL:
http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/io/ClassLoaderWrapper.java?rev=802181&view=auto
==============================================================================
---
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/io/ClassLoaderWrapper.java
(added)
+++
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/io/ClassLoaderWrapper.java
Fri Aug 7 20:49:44 2009
@@ -0,0 +1,212 @@
+package org.apache.ibatis.io;
+
+import org.apache.log4j.Logger;
+
+import java.io.InputStream;
+import java.net.URL;
+
+/**
+ * A class to wrap access to multiple class loaders making them work as one
+ */
+public class ClassLoaderWrapper {
+ private static final Logger log = Logger.getLogger(ClassLoaderWrapper.class);
+
+ ClassLoader defaultClassLoader;
+
+ ClassLoaderWrapper() {
+ }
+
+ /**
+ * Get a resource as a URL using the current class path
+ *
+ * @param resource - the resource to locate
+ * @return the resource or null
+ */
+ public URL getResourceAsURL(String resource) {
+ return getResourceAsURL(resource, new ClassLoader[]{
+ defaultClassLoader,
+ Thread.currentThread().getContextClassLoader(),
+ getClass().getClassLoader(),
+ ClassLoader.getSystemClassLoader()
+ });
+ }
+
+ /**
+ * Get a resource from the classpath, starting with a specific class loader
+ *
+ * @param resource - the resource to find
+ * @param classLoader - the first classloader to try
+ * @return the stream or null
+ */
+ public URL getResourceAsURL(String resource, ClassLoader classLoader) {
+ return getResourceAsURL(resource, new ClassLoader[]{
+ classLoader,
+ defaultClassLoader,
+ Thread.currentThread().getContextClassLoader(),
+ getClass().getClassLoader(),
+ ClassLoader.getSystemClassLoader()
+ });
+ }
+
+ /**
+ * Get a resource from the classpath
+ *
+ * @param resource - the resource to find
+ * @return the stream or null
+ */
+ public InputStream getResourceAsStream(String resource) {
+ return getResourceAsStream(resource, new ClassLoader[]{
+ defaultClassLoader,
+ Thread.currentThread().getContextClassLoader(),
+ getClass().getClassLoader(),
+ ClassLoader.getSystemClassLoader()
+ });
+ }
+
+ /**
+ * Get a resource from the classpath, starting with a specific class loader
+ *
+ * @param resource - the resource to find
+ * @param classLoader - the first class loader to try
+ * @return the stream or null
+ */
+ public InputStream getResourceAsStream(String resource, ClassLoader
classLoader) {
+ return getResourceAsStream(resource, new ClassLoader[]{
+ classLoader,
+ defaultClassLoader,
+ Thread.currentThread().getContextClassLoader(),
+ getClass().getClassLoader(),
+ ClassLoader.getSystemClassLoader()
+ });
+ }
+
+ /**
+ * Find a class on the classpath (or die trying)
+ *
+ * @param name - the class to look for
+ * @return - the class
+ * @throws ClassNotFoundException Duh.
+ */
+ public Class classForName(String name) throws ClassNotFoundException {
+ return classForName(name, new ClassLoader[]{
+ defaultClassLoader,
+ Thread.currentThread().getContextClassLoader(),
+ getClass().getClassLoader(),
+ ClassLoader.getSystemClassLoader()
+ });
+ }
+
+ /**
+ * Find a class on the classpath, starting with a specific classloader (or
die trying)
+ *
+ * @param name - the class to look for
+ * @param classLoader - the first classloader to try
+ * @return - the class
+ * @throws ClassNotFoundException Duh.
+ */
+ public Class classForName(String name, ClassLoader classLoader) throws
ClassNotFoundException {
+ return classForName(name, new ClassLoader[]{
+ classLoader,
+ defaultClassLoader,
+ Thread.currentThread().getContextClassLoader(),
+ getClass().getClassLoader(),
+ ClassLoader.getSystemClassLoader()
+ });
+ }
+
+ /**
+ * Try to get a resource from a group of classloaders
+ *
+ * @param resource - the resource to get
+ * @param classLoader - the classloaders to examine
+ * @return the resource or null
+ */
+ InputStream getResourceAsStream(String resource, ClassLoader[] classLoader) {
+ InputStream inputStream;
+ for (ClassLoader cl : classLoader) {
+ if (null != cl) {
+
+ // try to find the resource as passed
+ InputStream returnValue = cl.getResourceAsStream(resource);
+
+ // now, some class loaders want this leading "/", so we'll add it and
try again if we didn't find the resource
+ if (null == returnValue) returnValue = cl.getResourceAsStream("/" +
resource);
+
+ inputStream = returnValue;
+ if (null != inputStream) return inputStream;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Get a resource as a URL using the current class path
+ *
+ * @param resource - the resource to locate
+ * @param classLoader - the class loaders to examine
+ * @return the resource or null
+ */
+ URL getResourceAsURL(String resource, ClassLoader[] classLoader) {
+
+ URL url;
+
+ for (ClassLoader cl : classLoader) {
+
+ if (null != cl) {
+
+ // look for the resource as passed in...
+ url = cl.getResource(resource);
+
+ // ...but some class loaders want this leading "/", so we'll add it
+ // and try again if we didn't find the resource
+ if (null == url) url = cl.getResource("/" + resource);
+
+ // "It's always in the last place I look for it!"
+ // ... because only an idiot would keep looking for it after finding
it, so stop looking already.
+ if (null != url) return url;
+
+ }
+
+ }
+
+ // didn't find it anywhere.
+ return null;
+
+ }
+
+ /**
+ * Attempt to load a class from a group of classloaders
+ *
+ * @param name - the class to load
+ * @param classLoader - the group of classloaders to examine
+ * @return the class
+ * @throws ClassNotFoundException - Remember the wisdom of Judge Smails:
Well, the world needs ditch diggers, too.
+ */
+ Class classForName(String name, ClassLoader[] classLoader) throws
ClassNotFoundException {
+
+ for (ClassLoader cl : classLoader) {
+
+ if (null != cl) {
+
+ try {
+
+ log.debug("Looking for " + name + " using " + cl);
+
+ Class c = cl.loadClass(name);
+
+ if (null != c) return c;
+
+ } catch (ClassNotFoundException e) {
+ // we'll ignore this until all classloaders fail to locate the class
+ log.debug("Did not find " + name + " using " + cl);
+ }
+
+ }
+
+ }
+
+ throw new ClassNotFoundException("Cannot find class: " + name);
+
+ }
+
+}
Modified:
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/io/Resources.java
URL:
http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/io/Resources.java?rev=802181&r1=802180&r2=802181&view=diff
==============================================================================
---
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/io/Resources.java
(original)
+++
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/io/Resources.java
Fri Aug 7 20:49:44 2009
@@ -1,7 +1,8 @@
package org.apache.ibatis.io;
import java.io.*;
-import java.net.*;
+import java.net.URL;
+import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.Properties;
@@ -10,7 +11,8 @@
*/
public class Resources {
- private static ClassLoader defaultClassLoader;
+ // private static ClassLoader defaultClassLoader;
+ private static ClassLoaderWrapper classLoaderWrapper = new
ClassLoaderWrapper();
/**
* Charset to use when calling getResourceAsReader.
@@ -18,8 +20,7 @@
*/
private static Charset charset;
- private Resources() {
- }
+ Resources() {}
/**
* Returns the default classloader (may be null).
@@ -27,7 +28,7 @@
* @return The default classloader
*/
public static ClassLoader getDefaultClassLoader() {
- return defaultClassLoader;
+ return classLoaderWrapper.defaultClassLoader;
}
/**
@@ -36,7 +37,7 @@
* @param defaultClassLoader - the new default ClassLoader
*/
public static void setDefaultClassLoader(ClassLoader defaultClassLoader) {
- Resources.defaultClassLoader = defaultClassLoader;
+ classLoaderWrapper.defaultClassLoader = defaultClassLoader;
}
/**
@@ -47,7 +48,7 @@
* @throws java.io.IOException If the resource cannot be found or read
*/
public static URL getResourceURL(String resource) throws IOException {
- return getResourceURL(getClassLoader(), resource);
+ return classLoaderWrapper.getResourceAsURL(resource);
}
/**
@@ -59,11 +60,7 @@
* @throws java.io.IOException If the resource cannot be found or read
*/
public static URL getResourceURL(ClassLoader loader, String resource) throws
IOException {
- URL url = null;
- if (loader != null) url = loader.getResource(resource);
- if (url == null) url = ClassLoader.getSystemResource(resource);
- if (url == null) throw new IOException("Could not find resource " +
resource);
- return url;
+ return classLoaderWrapper.getResourceAsURL(resource, loader);
}
/**
@@ -74,7 +71,7 @@
* @throws java.io.IOException If the resource cannot be found or read
*/
public static InputStream getResourceAsStream(String resource) throws
IOException {
- return getResourceAsStream(getClassLoader(), resource);
+ return classLoaderWrapper.getResourceAsStream(resource);
}
/**
@@ -86,11 +83,7 @@
* @throws java.io.IOException If the resource cannot be found or read
*/
public static InputStream getResourceAsStream(ClassLoader loader, String
resource) throws IOException {
- InputStream in = null;
- if (loader != null) in = loader.getResourceAsStream(resource);
- if (in == null) in = ClassLoader.getSystemResourceAsStream(resource);
- if (in == null) throw new IOException("Could not find resource " +
resource);
- return in;
+ return classLoaderWrapper.getResourceAsStream(resource, loader);
}
/**
@@ -100,8 +93,7 @@
* @return The resource
* @throws java.io.IOException If the resource cannot be found or read
*/
- public static Properties getResourceAsProperties(String resource)
- throws IOException {
+ public static Properties getResourceAsProperties(String resource) throws
IOException {
Properties props = new Properties();
InputStream in = getResourceAsStream(resource);
props.load(in);
@@ -117,8 +109,7 @@
* @return The resource
* @throws java.io.IOException If the resource cannot be found or read
*/
- public static Properties getResourceAsProperties(ClassLoader loader, String
resource)
- throws IOException {
+ public static Properties getResourceAsProperties(ClassLoader loader, String
resource) throws IOException {
Properties props = new Properties();
InputStream in = getResourceAsStream(loader, resource);
props.load(in);
@@ -134,14 +125,11 @@
* @throws java.io.IOException If the resource cannot be found or read
*/
public static Reader getResourceAsReader(String resource) throws IOException
{
- Reader reader;
if (charset == null) {
- reader = new InputStreamReader(getResourceAsStream(resource));
+ return new InputStreamReader(getResourceAsStream(resource));
} else {
- reader = new InputStreamReader(getResourceAsStream(resource), charset);
+ return new InputStreamReader(getResourceAsStream(resource), charset);
}
-
- return reader;
}
/**
@@ -153,14 +141,11 @@
* @throws java.io.IOException If the resource cannot be found or read
*/
public static Reader getResourceAsReader(ClassLoader loader, String
resource) throws IOException {
- Reader reader;
if (charset == null) {
- reader = new InputStreamReader(getResourceAsStream(loader, resource));
+ return new InputStreamReader(getResourceAsStream(loader, resource));
} else {
- reader = new InputStreamReader(getResourceAsStream(loader, resource),
charset);
+ return new InputStreamReader(getResourceAsStream(loader, resource),
charset);
}
-
- return reader;
}
/**
@@ -233,24 +218,7 @@
* @throws ClassNotFoundException If the class cannot be found (duh!)
*/
public static Class classForName(String className) throws
ClassNotFoundException {
- Class clazz = null;
- try {
- clazz = getClassLoader().loadClass(className);
- } catch (Exception e) {
- // Ignore. Failsafe below.
- }
- if (clazz == null) {
- clazz = Class.forName(className);
- }
- return clazz;
- }
-
- private static ClassLoader getClassLoader() {
- if (defaultClassLoader != null) {
- return defaultClassLoader;
- } else {
- return Thread.currentThread().getContextClassLoader();
- }
+ return classLoaderWrapper.classForName(className);
}
public static Charset getCharset() {
Added:
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/io/ClassLoaderWrapperTest.java
URL:
http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/io/ClassLoaderWrapperTest.java?rev=802181&view=auto
==============================================================================
---
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/io/ClassLoaderWrapperTest.java
(added)
+++
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/io/ClassLoaderWrapperTest.java
Fri Aug 7 20:49:44 2009
@@ -0,0 +1,69 @@
+package org.apache.ibatis.io;
+
+import org.junit.Test;
+import org.junit.Before;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import org.apache.ibatis.BaseDataTest;
+
+public class ClassLoaderWrapperTest extends BaseDataTest {
+
+ ClassLoaderWrapper wrapper;
+ ClassLoader loader;
+ private final String RESOURCE_NOT_FOUND =
"some_resource_that_does_not_exist.properties";
+ private final String CLASS_NOT_FOUND =
"some.random.class.that.does.not.Exist";
+ private final String CLASS_FOUND = "java.lang.Object";
+
+
+ @Before
+ public void beforeClassLoaderWrapperTest() {
+ wrapper = new ClassLoaderWrapper();
+ loader = getClass().getClassLoader();
+ }
+
+ @Test
+ public void classForName() throws ClassNotFoundException {
+ assertNotNull(wrapper.classForName(CLASS_FOUND));
+ }
+
+ @Test(expected = ClassNotFoundException.class)
+ public void classForNameNotFound() throws ClassNotFoundException {
+ assertNotNull(wrapper.classForName(CLASS_NOT_FOUND));
+ }
+
+ @Test
+ public void classForNameWithClassLoader() throws ClassNotFoundException {
+ assertNotNull(wrapper.classForName(CLASS_FOUND, loader));
+ }
+
+ @Test
+ public void getResourceAsURL() {
+ assertNotNull(wrapper.getResourceAsURL(JPETSTORE_PROPERTIES));
+ }
+
+ @Test
+ public void getResourceAsURLNotFound() {
+ assertNull(wrapper.getResourceAsURL(RESOURCE_NOT_FOUND));
+ }
+
+ @Test
+ public void getResourceAsURLWithClassLoader() {
+ assertNotNull(wrapper.getResourceAsURL(JPETSTORE_PROPERTIES, loader));
+ }
+
+ @Test
+ public void getResourceAsStream() {
+ assertNotNull(wrapper.getResourceAsStream(JPETSTORE_PROPERTIES));
+ }
+
+ @Test
+ public void getResourceAsStreamNotFound() {
+ assertNull(wrapper.getResourceAsStream(RESOURCE_NOT_FOUND));
+ }
+
+ @Test
+ public void getResourceAsStreamWithClassLoader() {
+ assertNotNull(wrapper.getResourceAsStream(JPETSTORE_PROPERTIES, loader));
+ }
+
+}
Modified:
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/io/ResourcesTest.java
URL:
http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/io/ResourcesTest.java?rev=802181&r1=802180&r2=802181&view=diff
==============================================================================
---
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/io/ResourcesTest.java
(original)
+++
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/io/ResourcesTest.java
Fri Aug 7 20:49:44 2009
@@ -9,6 +9,8 @@
import java.nio.charset.Charset;
import java.util.Properties;
+import sun.nio.cs.US_ASCII;
+
public class ResourcesTest extends BaseDataTest {
private static final ClassLoader CLASS_LOADER =
ResourcesTest.class.getClassLoader();
@@ -65,13 +67,25 @@
@Test
public void shouldGetResourceAsFile() throws Exception {
File file = Resources.getResourceAsFile(JPETSTORE_PROPERTIES);
-
assertTrue(file.toURL().toString().endsWith("jpetstore/jpetstore-hsqldb.properties"));
+
assertTrue(file.getAbsolutePath().endsWith("jpetstore/jpetstore-hsqldb.properties"));
}
@Test
public void shouldGetResourceAsFileWithClassloader() throws Exception {
File file = Resources.getResourceAsFile(CLASS_LOADER,
JPETSTORE_PROPERTIES);
-
assertTrue(file.toURL().toString().endsWith("jpetstore/jpetstore-hsqldb.properties"));
+
assertTrue(file.getAbsolutePath().endsWith("jpetstore/jpetstore-hsqldb.properties"));
+ }
+
+ @Test
+ public void shouldGetResourceAsPropertiesWithOutClassloader() throws
Exception {
+ Properties file = Resources.getResourceAsProperties(JPETSTORE_PROPERTIES);
+ assertNotNull(file);
+ }
+
+ @Test
+ public void shouldGetResourceAsPropertiesWithClassloader() throws Exception {
+ Properties file = Resources.getResourceAsProperties(CLASS_LOADER,
JPETSTORE_PROPERTIES);
+ assertNotNull(file);
}
@Test
@@ -92,5 +106,51 @@
assertNotNull(clazz);
}
+ @Test(expected = ClassNotFoundException.class)
+ public void shouldNotFindThisClass() throws ClassNotFoundException {
+ Resources.classForName("some.random.class.that.does.not.Exist");
+ }
+
+ @Test
+ public void shouldGetReader() throws IOException {
+
+ // save the value
+ Charset charset = Resources.getCharset();
+
+ // charset
+ Resources.setCharset(new US_ASCII());
+ assertNotNull(Resources.getResourceAsReader(JPETSTORE_PROPERTIES));
+
+ // no charset
+ Resources.setCharset(null);
+ assertNotNull(Resources.getResourceAsReader(JPETSTORE_PROPERTIES));
+ // clean up
+ Resources.setCharset(charset);
+
+ }
+
+ @Test
+ public void shouldGetReaderWithClassLoader() throws IOException {
+
+ // save the value
+ Charset charset = Resources.getCharset();
+
+ // charset
+ Resources.setCharset(new US_ASCII());
+ assertNotNull(Resources.getResourceAsReader(getClass().getClassLoader(),
JPETSTORE_PROPERTIES));
+
+ // no charset
+ Resources.setCharset(null);
+ assertNotNull(Resources.getResourceAsReader(getClass().getClassLoader(),
JPETSTORE_PROPERTIES));
+
+ // clean up
+ Resources.setCharset(charset);
+
+ }
+
+ @Test
+ public void stupidJustForCoverage() {
+ assertNotNull(new Resources());
+ }
}