harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ton...@apache.org
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 GMT
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:<br>
  * 
  * <pre>
- *     &lt;package_prefix&gt; . &lt;scheme&gt; . &lt;scheme&gt;URLContextFactory
+ *            &lt;package_prefix&gt; . &lt;scheme&gt; . &lt;scheme&gt;URLContextFactory
  * </pre>
  * 
  * Several variants of the classname are constructed using each element of the
@@ -126,6 +126,16 @@
     protected Hashtable<Object, Object> myProps;
 
     /**
+     * Contains loaded properties for each classloader
+     */
+    private static Hashtable<ClassLoader, Hashtable<Object, Object>> propsCache
= new Hashtable<ClassLoader, Hashtable<Object, Object>>();
+    
+    /**
+     * Contians properties load from java.home/lib/jndi.properties
+     */
+    private static Hashtable<Object, Object> libProperties = null;
+
+    /**
      * Constructs an <code>InitialContext</code> instance without using any
      * environment properties. This constructor is effectively the same as using
      * constructor <code>InitialContext((Hashtable)null)</code>.
@@ -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<Object, Object> appProps = new Hashtable<Object, Object>();
+            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<Object, Object>();
+            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<String, Context> contextCache = new HashMap<String, Context>();
  
+
+    private HashMap<String, Context> contextCache = new HashMap<String, Context>();
 }

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));
+        }
+    }
 }



Mime
View raw message