You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ibatis.apache.org by cb...@apache.org on 2009/08/08 03:07:33 UTC

svn commit: r802278 - in /ibatis/trunk/java/ibatis-3: ibatis-3-compat/src/test/java/domain/misc/ ibatis-3-compat/src/test/java/resources/ ibatis-3-core/src/main/java/org/apache/ibatis/io/ ibatis-3-core/src/main/java/org/apache/ibatis/parsing/ ibatis-3-...

Author: cbegin
Date: Sat Aug  8 01:07:33 2009
New Revision: 802278

URL: http://svn.apache.org/viewvc?rev=802278&view=rev
Log:
rolling back new ClassLoaderWrapper as it broke the build.

Removed:
    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/src/test/java/domain/misc/Employee.java   (props changed)
    ibatis/trunk/java/ibatis-3/ibatis-3-compat/src/test/java/resources/nodelet_test.xml   (props changed)
    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/main/java/org/apache/ibatis/parsing/XNode.java   (props changed)
    ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/type/NClobTypeHandler.java   (props changed)
    ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/type/NStringTypeHandler.java   (props changed)
    ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/io/ResourcesTest.java

Propchange: ibatis/trunk/java/ibatis-3/ibatis-3-compat/src/test/java/domain/misc/Employee.java
            ('svn:mergeinfo' removed)

Propchange: ibatis/trunk/java/ibatis-3/ibatis-3-compat/src/test/java/resources/nodelet_test.xml
            ('svn:mergeinfo' removed)

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=802278&r1=802277&r2=802278&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 01:07:33 2009
@@ -1,8 +1,7 @@
 package org.apache.ibatis.io;
 
 import java.io.*;
-import java.net.URL;
-import java.net.URLConnection;
+import java.net.*;
 import java.nio.charset.Charset;
 import java.util.Properties;
 
@@ -11,8 +10,7 @@
  */
 public class Resources {
 
-  //  private static ClassLoader defaultClassLoader;
-  private static ClassLoaderWrapper classLoaderWrapper = new ClassLoaderWrapper();
+  private static ClassLoader defaultClassLoader;
 
   /**
    * Charset to use when calling getResourceAsReader.
@@ -20,7 +18,8 @@
    */
   private static Charset charset;
 
-  Resources() {}
+  private Resources() {
+  }
 
   /**
    * Returns the default classloader (may be null).
@@ -28,7 +27,7 @@
    * @return The default classloader
    */
   public static ClassLoader getDefaultClassLoader() {
-    return classLoaderWrapper.defaultClassLoader;
+    return defaultClassLoader;
   }
 
   /**
@@ -37,7 +36,7 @@
    * @param defaultClassLoader - the new default ClassLoader
    */
   public static void setDefaultClassLoader(ClassLoader defaultClassLoader) {
-    classLoaderWrapper.defaultClassLoader = defaultClassLoader;
+    Resources.defaultClassLoader = defaultClassLoader;
   }
 
   /**
@@ -48,7 +47,7 @@
    * @throws java.io.IOException If the resource cannot be found or read
    */
   public static URL getResourceURL(String resource) throws IOException {
-    return classLoaderWrapper.getResourceAsURL(resource);
+    return getResourceURL(getClassLoader(), resource);
   }
 
   /**
@@ -60,7 +59,11 @@
    * @throws java.io.IOException If the resource cannot be found or read
    */
   public static URL getResourceURL(ClassLoader loader, String resource) throws IOException {
-    return classLoaderWrapper.getResourceAsURL(resource, loader);
+    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;
   }
 
   /**
@@ -71,7 +74,7 @@
    * @throws java.io.IOException If the resource cannot be found or read
    */
   public static InputStream getResourceAsStream(String resource) throws IOException {
-    return classLoaderWrapper.getResourceAsStream(resource);
+    return getResourceAsStream(getClassLoader(), resource);
   }
 
   /**
@@ -83,7 +86,11 @@
    * @throws java.io.IOException If the resource cannot be found or read
    */
   public static InputStream getResourceAsStream(ClassLoader loader, String resource) throws IOException {
-    return classLoaderWrapper.getResourceAsStream(resource, loader);
+    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;
   }
 
   /**
@@ -93,7 +100,8 @@
    * @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);
@@ -109,7 +117,8 @@
    * @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);
@@ -125,11 +134,14 @@
    * @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) {
-      return new InputStreamReader(getResourceAsStream(resource));
+      reader = new InputStreamReader(getResourceAsStream(resource));
     } else {
-      return new InputStreamReader(getResourceAsStream(resource), charset);
+      reader = new InputStreamReader(getResourceAsStream(resource), charset);
     }
+
+    return reader;
   }
 
   /**
@@ -141,11 +153,14 @@
    * @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) {
-      return new InputStreamReader(getResourceAsStream(loader, resource));
+      reader = new InputStreamReader(getResourceAsStream(loader, resource));
     } else {
-      return new InputStreamReader(getResourceAsStream(loader, resource), charset);
+      reader = new InputStreamReader(getResourceAsStream(loader, resource), charset);
     }
+
+    return reader;
   }
 
   /**
@@ -218,7 +233,24 @@
    * @throws ClassNotFoundException If the class cannot be found (duh!)
    */
   public static Class classForName(String className) throws ClassNotFoundException {
-    return classLoaderWrapper.classForName(className);
+    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();
+    }
   }
 
   public static Charset getCharset() {

Propchange: ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/parsing/XNode.java
            ('svn:mergeinfo' removed)

Propchange: ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/type/NClobTypeHandler.java
            ('svn:mergeinfo' removed)

Propchange: ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/type/NStringTypeHandler.java
            ('svn:mergeinfo' removed)

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=802278&r1=802277&r2=802278&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 01:07:33 2009
@@ -9,8 +9,6 @@
 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();
@@ -67,25 +65,13 @@
   @Test
   public void shouldGetResourceAsFile() throws Exception {
     File file = Resources.getResourceAsFile(JPETSTORE_PROPERTIES);
-    assertTrue(file.getAbsolutePath().endsWith("jpetstore/jpetstore-hsqldb.properties"));
+    assertTrue(file.toURL().toString().endsWith("jpetstore/jpetstore-hsqldb.properties"));
   }
 
   @Test
   public void shouldGetResourceAsFileWithClassloader() throws Exception {
     File file = Resources.getResourceAsFile(CLASS_LOADER, JPETSTORE_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);
+    assertTrue(file.toURL().toString().endsWith("jpetstore/jpetstore-hsqldb.properties"));
   }
 
   @Test
@@ -106,51 +92,5 @@
     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());
-  }
 }