Return-Path: Delivered-To: apmail-harmony-commits-archive@www.apache.org Received: (qmail 39845 invoked from network); 10 Dec 2007 06:21:05 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 10 Dec 2007 06:21:05 -0000 Received: (qmail 80890 invoked by uid 500); 10 Dec 2007 06:20:54 -0000 Delivered-To: apmail-harmony-commits-archive@harmony.apache.org Received: (qmail 80796 invoked by uid 500); 10 Dec 2007 06:20:53 -0000 Mailing-List: contact commits-help@harmony.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@harmony.apache.org Delivered-To: mailing list commits@harmony.apache.org Received: (qmail 80787 invoked by uid 99); 10 Dec 2007 06:20:53 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 09 Dec 2007 22:20:53 -0800 X-ASF-Spam-Status: No, hits=-100.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 10 Dec 2007 06:20:41 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 384701A983A; Sun, 9 Dec 2007 22:20:44 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r602784 - in /harmony/enhanced/classlib/trunk/modules/jndi/src: main/java/javax/naming/InitialContext.java test/java/org/apache/harmony/jndi/tests/javax/naming/InitialContextAppTest.java Date: Mon, 10 Dec 2007 06:19:57 -0000 To: commits@harmony.apache.org From: tonywu@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20071210062044.384701A983A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: tonywu Date: Sun Dec 9 22:19:26 2007 New Revision: 602784 URL: http://svn.apache.org/viewvc?rev=602784&view=rev Log: Apply patch for HARMONY-4942 ([classlib][jndi] InitialContext searches for jndi.properties every contruction time) Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/javax/naming/InitialContext.java harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/tests/javax/naming/InitialContextAppTest.java Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/javax/naming/InitialContext.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/javax/naming/InitialContext.java?rev=602784&r1=602783&r2=602784&view=diff ============================================================================== --- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/javax/naming/InitialContext.java (original) +++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/javax/naming/InitialContext.java Sun Dec 9 22:19:26 2007 @@ -72,7 +72,7 @@ * to construct a classname suffix in the following form:
* *
- *     <package_prefix> . <scheme> . <scheme>URLContextFactory
+ *            <package_prefix> . <scheme> . <scheme>URLContextFactory
  * 
* * Several variants of the classname are constructed using each element of the @@ -126,6 +126,16 @@ protected Hashtable myProps; /** + * Contains loaded properties for each classloader + */ + private static Hashtable> propsCache = new Hashtable>(); + + /** + * Contians properties load from java.home/lib/jndi.properties + */ + private static Hashtable libProperties = null; + + /** * Constructs an InitialContext instance without using any * environment properties. This constructor is effectively the same as using * constructor InitialContext((Hashtable)null). @@ -203,11 +213,26 @@ EnvironmentReader.readSystemProperties(myProps); // 4.1 Read application/applet resource files - EnvironmentReader.readApplicationResourceFiles(myProps); - + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + if (propsCache.containsKey(cl)) { + EnvironmentReader.mergeEnvironment(propsCache.get(cl), myProps, + true); + } else { + Hashtable appProps = new Hashtable(); + EnvironmentReader.readApplicationResourceFiles(appProps); + propsCache.put(cl, appProps); + EnvironmentReader.mergeEnvironment(appProps, myProps, true); + } + // 4.2 Read "java.home"/lib/jndi.properties - EnvironmentReader.readLibraryResourceFile(myProps); - + if (libProperties == null) { + libProperties = new Hashtable(); + EnvironmentReader.readLibraryResourceFile(libProperties); + } + + EnvironmentReader.mergeEnvironment(libProperties, myProps, true); + + // 5. No need to read service provider resource files // if JNDI standard property "java.naming.factory.initial" has a @@ -217,6 +242,7 @@ // defaultInitCtx getDefaultInitCtx(); } + } /** @@ -362,7 +388,7 @@ if (null == ctx) { ctx = getDefaultInitCtx(); } - contextCache.put(scheme, ctx); + contextCache.put(scheme, ctx); } return ctx; } @@ -505,7 +531,7 @@ } public Object addToEnvironment(String propName, Object propVal) - throws NamingException { + throws NamingException { synchronized (contextCache) { myProps.put(propName, propVal); contextCache.clear(); @@ -534,7 +560,6 @@ public String getNameInNamespace() throws NamingException { return getDefaultInitCtx().getNameInNamespace(); } - - - private HashMap contextCache = new HashMap(); + + private HashMap contextCache = new HashMap(); } Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/tests/javax/naming/InitialContextAppTest.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/tests/javax/naming/InitialContextAppTest.java?rev=602784&r1=602783&r2=602784&view=diff ============================================================================== --- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/tests/javax/naming/InitialContextAppTest.java (original) +++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/tests/javax/naming/InitialContextAppTest.java Sun Dec 9 22:19:26 2007 @@ -16,36 +16,155 @@ */ package org.apache.harmony.jndi.tests.javax.naming; +import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.PrintStream; +import java.net.URL; +import java.net.URLClassLoader; import java.util.Enumeration; import java.util.Hashtable; +import java.util.Hashtable; +import javax.naming.InitialContext; import javax.naming.NamingException; import junit.framework.TestCase; import org.apache.harmony.jndi.tests.javax.naming.util.Log; public class InitialContextAppTest extends TestCase { - private static final Log log = new Log(InitialContextAppTest.class); + private static final Log log = new Log(InitialContextAppTest.class); + + public void testConstructor_App() throws NamingException, IOException { + // Comment this test case out because this test case + // needs complex configuration about jndi properties. + + // log.setMethod("testConstructor_App"); + // InitialContext context = new InitialContext(); + // Hashtable props = context.getEnvironment(); + // // printHashtable(props); + // Hashtable expected = TestInitialContextLib.readAllProps(null); + // assertEquals(expected, props); + } + + /** + * regression: Harmony-4942 + * + */ + public void testConstructor() throws Exception { + final File file1 = new File("src/test/resources/test1"); + if (!file1.exists()) { + file1.mkdir(); + } + + URL url = file1.toURL(); + URLClassLoader cltest1 = new URLClassLoader(new URL[] { url }, Thread + .currentThread().getContextClassLoader()); + Thread test1 = new Thread(new Runnable() { + + public void run() { + try { + File propsFile = new File(file1.toString() + + "/jndi.properties"); + + FileOutputStream fos = new FileOutputStream(propsFile); + + PrintStream ps = new PrintStream(fos); + ps + .println("java.naming.factory.initial=org.apache.harmony.jndi.tests.javax.naming.spi.mock.MockContextFactory"); + ps.println("java.naming.provider.url=http://test1"); + ps.close(); + + InitialContext context = new InitialContext(); + Hashtable env = context.getEnvironment(); + assertEquals( + "org.apache.harmony.jndi.tests.javax.naming.spi.mock.MockContextFactory", + env.get("java.naming.factory.initial")); + assertEquals("http://test1", env + .get("java.naming.provider.url")); + + propsFile.delete(); + + // create new properties file with different values + fos = new FileOutputStream(propsFile); + ps = new PrintStream(fos); + ps + .println("java.naming.factory.initial=not.exist.ContextFactory"); + ps.println("java.naming.provider.url=http://test1.new"); + ps.close(); + + context = new InitialContext(); + env = context.getEnvironment(); + assertEquals( + "org.apache.harmony.jndi.tests.javax.naming.spi.mock.MockContextFactory", + env.get("java.naming.factory.initial")); + assertEquals("http://test1", env + .get("java.naming.provider.url")); + + propsFile.delete(); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + }); + // use different classloader + test1.setContextClassLoader(cltest1); + + test1.start(); + + final File file2 = new File("src/test/resources/test2"); + if (!file2.exists()) { + file2.mkdir(); + } + url = file2.toURL(); + URLClassLoader cltest2 = new URLClassLoader(new URL[] { url }, Thread + .currentThread().getContextClassLoader()); + + Thread test2 = new Thread(new Runnable() { + + public void run() { + try { + File propsFile = new File(file2.toString() + + "/jndi.properties"); + FileOutputStream fos = new FileOutputStream(propsFile); + PrintStream ps = new PrintStream(fos); + ps + .println("java.naming.factory.initial=org.apache.harmony.jndi.tests.javax.naming.spi.mock.MockContextFactory"); + ps.println("java.naming.provider.url=http://test2"); + ps.close(); + + InitialContext context = new InitialContext(); + Hashtable env = context.getEnvironment(); + assertEquals( + "org.apache.harmony.jndi.tests.javax.naming.spi.mock.MockContextFactory", + env.get("java.naming.factory.initial")); + assertEquals("http://test2", env + .get("java.naming.provider.url")); + + propsFile.delete(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + }); + + // use different classloader + test2.setContextClassLoader(cltest2); + test2.start(); + + Thread.sleep(1000); + file1.deleteOnExit(); + file2.deleteOnExit(); + } - public void testConstructor_App() throws NamingException, IOException { - //Comment this test case out because this test case - //needs complex configuration about jndi properties. - -// log.setMethod("testConstructor_App"); -// InitialContext context = new InitialContext(); -// Hashtable props = context.getEnvironment(); -// // printHashtable(props); -// Hashtable expected = TestInitialContextLib.readAllProps(null); -// assertEquals(expected, props); - } - - void printHashtable(Hashtable env) { - // TO DO: Need to remove - Enumeration keys = env.keys(); - while (keys.hasMoreElements()) { - Object key = keys.nextElement(); - log.log(key + "=" + env.get(key)); - } - } + void printHashtable(Hashtable env) { + // TO DO: Need to remove + Enumeration keys = env.keys(); + while (keys.hasMoreElements()) { + Object key = keys.nextElement(); + log.log(key + "=" + env.get(key)); + } + } }