harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From py...@apache.org
Subject svn commit: r432191 - in /incubator/harmony/enhanced/classlib/trunk/modules/nio_char: ./ src/main/java/java/nio/charset/ src/test/java/tests/api/java/nio/charset/ src/test/resources/jars/
Date Thu, 17 Aug 2006 09:42:46 GMT
Author: pyang
Date: Thu Aug 17 02:42:45 2006
New Revision: 432191

URL: http://svn.apache.org/viewvc?rev=432191&view=rev
Log:
Patch applied for HARMONY-1051 ([classlib][nio_char] Use system classloader as an alternative
to load charset provider classes when thread's context classloader fails to load them)

Added:
    incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/resources/jars/
    incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/resources/jars/charset_provider.jar
  (with props)
Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/nio_char/.classpath
    incubator/harmony/enhanced/classlib/trunk/modules/nio_char/build.xml
    incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/Charset.java
    incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/CharsetTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio_char/.classpath
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/nio_char/.classpath?rev=432191&r1=432190&r2=432191&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio_char/.classpath (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio_char/.classpath Thu Aug 17 02:42:45
2006
@@ -8,5 +8,6 @@
 	<classpathentry kind="lib" path="depends/jars/icu4jni-3.4.jar"/>
 	<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
 	<classpathentry sourcepath="JUNIT_SRC_HOME/junitsrc.zip" kind="var" path="JUNIT_HOME/junit.jar"/>
+	<classpathentry kind="lib" path="src/test/resources/jars/charset_provider.jar"/>
 	<classpathentry kind="output" path="bin/main"/>
 </classpath>

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio_char/build.xml
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/nio_char/build.xml?rev=432191&r1=432190&r2=432191&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio_char/build.xml (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio_char/build.xml Thu Aug 17 02:42:45
2006
@@ -121,6 +121,9 @@
                 <pathelement path="${hy.nio_char.bin.test}"/>
                 <pathelement path="../../build/tests" />
                 <pathelement path="${hy.hdk}/build/test/support.jar" />
+                <fileset dir="${hy.nio_char.src.test.resources}/jars">
+                    <include name="**/*.jar" />
+                </fileset>
             </classpath>
 
             <formatter type="xml" />

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/Charset.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/Charset.java?rev=432191&r1=432190&r2=432191&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/Charset.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/Charset.java
Thu Aug 17 02:42:45 2006
@@ -96,6 +96,8 @@
 	 * the comment string used in configuration files
 	 */
 	private static final String PROVIDER_CONFIGURATION_FILE_COMMENT = "#"; //$NON-NLS-1$
+    
+    private static ClassLoader systemClassLoader;
 
 	/*
 	 * --------------------------------------------------------------------
@@ -253,6 +255,20 @@
 					}
 				});
 	}
+    
+    /*
+     * Use privileged code to get the system class loader.
+     */
+    private static void getSystemClassLoader() {
+        if (null == systemClassLoader) {
+            systemClassLoader = AccessController
+                    .doPrivileged(new PrivilegedAction<ClassLoader>() {
+                        public ClassLoader run() {
+                            return ClassLoader.getSystemClassLoader();
+                        }
+                    });
+        }
+    }
 
 	/*
 	 * Add the charsets supported by the given provider to the map.
@@ -286,8 +302,8 @@
 	 * Read a configuration file and add the charsets supported by the providers
 	 * specified by this configuration file to the map.
 	 */
-	private static void loadConfiguredCharsets(URL configFile, ClassLoader cl,
-			TreeMap<String, Charset> charsets) {
+	private static void loadConfiguredCharsets(URL configFile,
+            ClassLoader contextClassLoader, TreeMap<String, Charset> charsets) {
 		BufferedReader reader = null;
 		try {
 			InputStream is = configFile.openStream();
@@ -302,14 +318,21 @@
 					// Load the charset provider
 					Object cp = null;
 					try {
-						Class c = Class.forName(providerClassName, true, cl);
-						cp = c.newInstance();
-					} catch (SecurityException ex) {
-						// assume no permission to use charset provider
-						throw ex;
-					} catch (Exception ex) {
-						throw new Error(ex.getMessage(), ex);
-					}
+                        Class c = Class.forName(providerClassName, true,
+                                contextClassLoader);
+                        cp = c.newInstance();
+                    } catch (Exception ex) {
+                        // try to use system classloader when context
+                        // classloader failed to load config file.
+                        try {
+                            getSystemClassLoader();
+                            Class c = Class.forName(providerClassName, true,
+                                    systemClassLoader);
+                            cp = c.newInstance();
+                        } catch (Exception e) {
+                            throw new Error(e.getMessage(), e);
+                        }
+                    }
 					// Put the charsets supported by this provider into the map
 					addCharsets((CharsetProvider) cp, charsets);
 				}
@@ -357,21 +380,25 @@
 				.clone();
 
 		// Collect all charsets provided by charset providers
-		final ClassLoader cl = getContextClassLoader();
-		if (null != cl) {
-			try {
-				// Load all configuration files
-				Enumeration e = cl
-						.getResources(PROVIDER_CONFIGURATION_FILE_NAME);
-				// Examine each configuration file
-				while (e.hasMoreElements()) {
-					loadConfiguredCharsets((URL) e.nextElement(), cl, charsets);
-				}
-			} catch (IOException ex) {
-				// Unexpected ClassLoader exception, ignore
-			}
-		}
-
+        ClassLoader contextClassLoader = getContextClassLoader();
+        Enumeration e = null;
+        try {
+            if (null != contextClassLoader) {
+                e = contextClassLoader
+                        .getResources(PROVIDER_CONFIGURATION_FILE_NAME);
+            } else {
+                getSystemClassLoader();
+                e = systemClassLoader
+                        .getResources(PROVIDER_CONFIGURATION_FILE_NAME);
+            }
+            // Examine each configuration file
+            while (e.hasMoreElements()) {
+                loadConfiguredCharsets((URL) e.nextElement(),
+                        contextClassLoader, charsets);
+            }
+        } catch (IOException ex) {
+            // Unexpected ClassLoader exception, ignore
+        } 
 		return Collections.unmodifiableSortedMap(charsets);
 	}
 
@@ -381,7 +408,7 @@
 	 * file.
 	 */
 	private static Charset searchConfiguredCharsets(String charsetName,
-			URL configFile, ClassLoader cl) {
+            ClassLoader contextClassLoader, URL configFile) {
 		BufferedReader reader = null;
 		try {
 			InputStream is = configFile.openStream();
@@ -395,14 +422,20 @@
 					// Load the charset provider
 					Object cp = null;
 					try {
-						Class c = Class.forName(providerClassName, true, cl);
-						cp = c.newInstance();
-					} catch (SecurityException ex) {
-						// assume no permission to use charset provider
-						throw ex;
-					} catch (Exception ex) {
-						throw new Error(ex.getMessage(), ex);
-					}
+                        Class c = Class.forName(providerClassName, true, contextClassLoader);
+                        cp = c.newInstance();
+                    } catch (Exception ex) {
+                        // try to use system classloader when context
+                        // classloader failed to load config file.
+                        try {
+                            getSystemClassLoader();
+                            Class c = Class.forName(providerClassName, true,
+                                    systemClassLoader);
+                            cp = c.newInstance();
+                        } catch (Exception e) {
+                            throw new Error(e.getMessage(), e);
+                        }
+                    }
 					// Try to get the desired charset from this provider
 					Charset cs = ((CharsetProvider) cp)
 							.charsetForName(charsetName);
@@ -452,26 +485,30 @@
 				return cs;
 			}
 
-			// Collect all charsets provided by charset providers
-			final ClassLoader cl = getContextClassLoader();
-			if (null != cl) {
-				try {
-					// Load all configuration files
-					Enumeration e = cl
-							.getResources(PROVIDER_CONFIGURATION_FILE_NAME);
-					// Examine each configuration file
-					while (e.hasMoreElements()) {
-						cs = searchConfiguredCharsets(charsetName, (URL) e
-								.nextElement(), cl);
-						if (null != cs) {
-							cacheCharset(cs);
-							return cs;
-						}
-					}
-				} catch (IOException ex) {
-					// Unexpected ClassLoader exception, ignore
-				}
-			}
+            // Collect all charsets provided by charset providers
+            ClassLoader contextClassLoader = getContextClassLoader();
+            Enumeration e = null;
+            try {
+                if (null != contextClassLoader) {
+                    e = contextClassLoader
+                            .getResources(PROVIDER_CONFIGURATION_FILE_NAME);
+                } else {
+                    getSystemClassLoader();
+                    e = systemClassLoader
+                            .getResources(PROVIDER_CONFIGURATION_FILE_NAME);
+                }
+                // Examine each configuration file
+                while (e.hasMoreElements()) {
+                    cs = searchConfiguredCharsets(charsetName,
+                            contextClassLoader, (URL) e.nextElement());
+                    if (null != cs) {
+                        cacheCharset(cs);
+                        return cs;
+                    }
+                }
+            } catch (IOException ex) {
+                // Unexpected ClassLoader exception, ignore
+            } 
 		}
 		return null;
 	}

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/CharsetTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/CharsetTest.java?rev=432191&r1=432190&r2=432191&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/CharsetTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/CharsetTest.java
Thu Aug 17 02:42:45 2006
@@ -27,6 +27,7 @@
 import java.security.Permission;
 import java.util.Iterator;
 import java.util.Locale;
+import java.util.SortedMap;
 import java.util.Vector;
 
 import junit.framework.TestCase;
@@ -701,6 +702,39 @@
 		assertTrue(-1 != c1.toString().indexOf("mock"));
 	}
 
+    /**
+     * @tests java.nio.charset.Charset#availableCharsets()
+     */
+    public void test_availableCharsets() throws Exception {
+        // regression test for Harmony-1051
+        ClassLoader originalClassLoader = Thread.currentThread()
+                .getContextClassLoader();
+        try {
+            Thread.currentThread().setContextClassLoader(null);
+            SortedMap<String, Charset> charsets = Charset.availableCharsets();
+            // make sure "mockCharset00" is loaded by MockCharsetProvider
+            assertTrue(charsets.containsKey("mockCharset00"));
+        } finally {
+            Thread.currentThread().setContextClassLoader(originalClassLoader);
+        }
+    }
+    
+    /**
+     * @tests java.nio.charset.Charset#availableCharsets()
+     */
+    public void test_forNameLString() throws Exception {
+        // regression test for Harmony-1051
+        ClassLoader originalClassLoader = Thread.currentThread()
+                .getContextClassLoader();
+        try {
+            Thread.currentThread().setContextClassLoader(null);
+            // make sure "mockCharset00" is loaded by MockCharsetProvider
+            assertNotNull(Charset.forName("mockCharset00"));
+        } finally {
+            Thread.currentThread().setContextClassLoader(originalClassLoader);
+        }
+    }
+    
 	/*
 	 * Mock charset class.
 	 */
@@ -802,48 +836,7 @@
 			return v.iterator();
 		}
 	}
-
-	/*
-	 * Another mock charset provider.
-	 */
-	public static class MockCharsetProvider2 extends CharsetProvider {
-
-		public Charset charsetForName(String charsetName) {
-			if ("MockCharset10".equalsIgnoreCase(charsetName)
-					|| "MockCharset11".equals(charsetName)
-					|| "MockCharset12".equalsIgnoreCase(charsetName)) {
-				return charset2;
-			}
-			return null;
-		}
-
-		public Iterator charsets() {
-			Vector v = new Vector();
-			v.add(charset2);
-			return v.iterator();
-		}
-	}
-
-	/*
-	 * Another mock charset provider providing build-in charset "ascii".
-	 */
-	public static class MockCharsetProvider3 extends CharsetProvider {
-
-		public Charset charsetForName(String charsetName) {
-			if ("US-ASCII".equalsIgnoreCase(charsetName)
-					|| "ASCII".equalsIgnoreCase(charsetName)) {
-				return new MockCharset("US-ASCII", new String[] { "ASCII" });
-			}
-			return null;
-		}
-
-		public Iterator charsets() {
-			Vector v = new Vector();
-			v.add(new MockCharset("US-ASCII", new String[] { "ASCII" }));
-			return v.iterator();
-		}
-	}
-
+    
 	/*
 	 * Used to grant all permissions except charset provider access.
 	 */

Added: incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/resources/jars/charset_provider.jar
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/resources/jars/charset_provider.jar?rev=432191&view=auto
==============================================================================
Binary file - no diff available.

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/resources/jars/charset_provider.jar
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream



Mime
View raw message