You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ibatis.apache.org by lm...@apache.org on 2009/08/08 20:39:36 UTC
svn commit: r802432 - in /ibatis/trunk/java/ibatis-3: ibatis-3-compat/
ibatis-3-core/ ibatis-3-core/src/main/java/org/apache/ibatis/io/
ibatis-3-core/src/test/java/org/apache/ibatis/io/
Author: lmeadors
Date: Sat Aug 8 18:39:36 2009
New Revision: 802432
URL: http://svn.apache.org/viewvc?rev=802432&view=rev
Log:
Making the Resources class do a more exhaustive search for stuff.
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-compat/pom.xml
ibatis/trunk/java/ibatis-3/ibatis-3-core/pom.xml
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
Modified: ibatis/trunk/java/ibatis-3/ibatis-3-compat/pom.xml
URL: http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-compat/pom.xml?rev=802432&r1=802431&r2=802432&view=diff
==============================================================================
--- ibatis/trunk/java/ibatis-3/ibatis-3-compat/pom.xml (original)
+++ ibatis/trunk/java/ibatis-3/ibatis-3-compat/pom.xml Sat Aug 8 18:39:36 2009
@@ -9,7 +9,10 @@
<artifactId>ibatis-3-compat</artifactId>
<name>ibatis-3-compat</name>
<packaging>jar</packaging>
- <url>http://maven.apache.org</url>
+ <url>http://ibatis.apache.org</url>
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ </properties>
<build>
<plugins>
<plugin>
Modified: ibatis/trunk/java/ibatis-3/ibatis-3-core/pom.xml
URL: http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/pom.xml?rev=802432&r1=802431&r2=802432&view=diff
==============================================================================
--- ibatis/trunk/java/ibatis-3/ibatis-3-core/pom.xml (original)
+++ ibatis/trunk/java/ibatis-3/ibatis-3-core/pom.xml Sat Aug 8 18:39:36 2009
@@ -9,7 +9,10 @@
<artifactId>ibatis-3-core</artifactId>
<name>ibatis-3-core</name>
<packaging>jar</packaging>
- <url>http://maven.apache.org</url>
+ <url>http://ibatis.apache.org</url>
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ </properties>
<build>
<plugins>
<plugin>
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=802432&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 Sat Aug 8 18:39:36 2009
@@ -0,0 +1,210 @@
+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) {
+ 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);
+
+ if (null != returnValue) return returnValue;
+ }
+ }
+ 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=802432&r1=802431&r2=802432&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 Sat Aug 8 18:39:36 2009
@@ -10,7 +10,7 @@
*/
public class Resources {
- private static ClassLoader defaultClassLoader;
+ private static ClassLoaderWrapper classLoaderWrapper = new ClassLoaderWrapper();
/**
* Charset to use when calling getResourceAsReader.
@@ -18,8 +18,7 @@
*/
private static Charset charset;
- private Resources() {
- }
+ Resources() {}
/**
* Returns the default classloader (may be null).
@@ -27,7 +26,7 @@
* @return The default classloader
*/
public static ClassLoader getDefaultClassLoader() {
- return defaultClassLoader;
+ return classLoaderWrapper.defaultClassLoader;
}
/**
@@ -36,7 +35,7 @@
* @param defaultClassLoader - the new default ClassLoader
*/
public static void setDefaultClassLoader(ClassLoader defaultClassLoader) {
- Resources.defaultClassLoader = defaultClassLoader;
+ classLoaderWrapper.defaultClassLoader = defaultClassLoader;
}
/**
@@ -47,7 +46,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,9 +58,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);
+ URL url = classLoaderWrapper.getResourceAsURL(resource, loader);
if (url == null) throw new IOException("Could not find resource " + resource);
return url;
}
@@ -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 getResourceAsStream(null, resource);
}
/**
@@ -86,9 +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);
+ InputStream in = classLoaderWrapper.getResourceAsStream(resource, loader);
if (in == null) throw new IOException("Could not find resource " + resource);
return in;
}
@@ -100,8 +95,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 +111,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);
@@ -140,7 +133,6 @@
} else {
reader = new InputStreamReader(getResourceAsStream(resource), charset);
}
-
return reader;
}
@@ -159,7 +151,6 @@
} else {
reader = new InputStreamReader(getResourceAsStream(loader, resource), charset);
}
-
return reader;
}
@@ -233,24 +224,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=802432&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 Sat Aug 8 18:39:36 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=802432&r1=802431&r2=802432&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 Sat Aug 8 18:39:36 2009
@@ -3,8 +3,12 @@
import org.apache.ibatis.BaseDataTest;
import static org.junit.Assert.*;
import org.junit.Test;
+import sun.nio.cs.US_ASCII;
-import java.io.*;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Properties;
@@ -65,13 +69,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 +108,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());
+ }
+}