harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r597075 - in /harmony/enhanced/classlib/trunk/modules/jndi/src: main/java/org/apache/harmony/jndi/provider/ldap/LdapContextImpl.java test/java/org/apache/harmony/jndi/provider/ldap/LdapContextImplTest.java
Date Wed, 21 Nov 2007 13:30:24 GMT
Author: tellison
Date: Wed Nov 21 05:30:22 2007
New Revision: 597075

URL: http://svn.apache.org/viewvc?rev=597075&view=rev
Log:
Apply patch HARMONY-5134 ([classlib][jndi][ldap] implements LdapContextImpl removeFromEnvironment
and addToEnvironment)

Modified:
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextImpl.java
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextImplTest.java

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextImpl.java?rev=597075&r1=597074&r2=597075&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextImpl.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextImpl.java
Wed Nov 21 05:30:22 2007
@@ -19,6 +19,7 @@
 
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.HashSet;
 import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.List;
@@ -26,10 +27,12 @@
 import java.util.Set;
 import java.util.StringTokenizer;
 
+import javax.naming.AuthenticationNotSupportedException;
 import javax.naming.Binding;
 import javax.naming.CannotProceedException;
 import javax.naming.CommunicationException;
 import javax.naming.CompositeName;
+import javax.naming.ConfigurationException;
 import javax.naming.Context;
 import javax.naming.InvalidNameException;
 import javax.naming.Name;
@@ -112,6 +115,23 @@
     private static final String LDAP_TYPES_ONLY = "java.naming.ldap.typesOnly"; //$NON-NLS-1$
 
     /**
+     * Some properties, such as 'java.naming.security.authentication', changed
+     * by <code>Context.addToEnvironment</code> or
+     * <code>Context.removeFromEnvironment</code> may affect connection with
+     * LDAP server. This variable contains all such properties, which need
+     * re-communication with LDAP server after changing.
+     */
+    private static final HashSet<String> connectionProperties = new HashSet<String>();
+
+    static {
+        connectionProperties.add(Context.SECURITY_AUTHENTICATION);
+        connectionProperties.add(Context.SECURITY_CREDENTIALS);
+        connectionProperties.add(Context.SECURITY_PRINCIPAL);
+        connectionProperties.add(Context.SECURITY_PROTOCOL);
+        connectionProperties.add("java.naming.ldap.factory.socket");
+    }
+
+    /**
      * construct a new inherit <code>LdapContextImpl</code>
      * 
      * @param context
@@ -1039,8 +1059,17 @@
     }
 
     public Object addToEnvironment(String s, Object o) throws NamingException {
-        // TODO not yet implemented
-        throw new NotYetImplementedException();
+        Object preValue = env.put(s, o);
+
+        // if preValue equals o, do nothing
+        if ((preValue != null && preValue.equals(o))
+                || (preValue == null && o == null)) {
+            return preValue;
+        }
+
+        updateEnvironment(s);
+
+        return preValue;
     }
 
     public void bind(Name n, Object o) throws NamingException {
@@ -1401,8 +1430,40 @@
     }
 
     public Object removeFromEnvironment(String s) throws NamingException {
-        // TODO not yet implemented
-        throw new NotYetImplementedException();
+        Object preValue = env.remove(s);
+
+        // if s doesn't exist in env
+        if (preValue == null) {
+            return preValue;
+        }
+
+        updateEnvironment(s);
+
+        return preValue;
+    }
+
+    private void updateEnvironment(String propName) throws NamingException,
+            AuthenticationNotSupportedException, CommunicationException,
+            ConfigurationException {
+        if (connectionProperties.contains(propName)) {
+            if (propName.equals("java.naming.ldap.factory.socket")) {
+                // use new socket factory to connect server
+                String address = client.getAddress();
+                int port = client.getPort();
+
+                client = LdapClient.newInstance(address, port, env);
+                try {
+                    doBindOperation(connCtls);
+                } catch (IOException e) {
+                    CommunicationException ex = new CommunicationException();
+                    ex.setRootCause(e);
+                    throw ex;
+                }
+            } else {
+
+                reconnect(connCtls);
+            }
+        }
     }
 
     public void rename(Name nOld, Name nNew) throws NamingException {

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextImplTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextImplTest.java?rev=597075&r1=597074&r2=597075&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextImplTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextImplTest.java
Wed Nov 21 05:30:22 2007
@@ -503,4 +503,45 @@
         }
     }
 
+    public void test_addToEnvironment() throws Exception {
+        MockLdapClient client = new MockLdapClient();
+        Hashtable<Object, Object> env = new Hashtable<Object, Object>();
+
+        context = new LdapContextImpl(client, env, "cn=test");
+
+        Object preValue = context.addToEnvironment(Context.REFERRAL, "ignore");
+        assertNull(preValue);
+        Hashtable<Object, Object> returnedEnv = (Hashtable<Object, Object>) context
+                .getEnvironment();
+        assertTrue(returnedEnv.containsKey(Context.REFERRAL));
+        assertEquals("ignore", returnedEnv.get(Context.REFERRAL));
+
+        preValue = context.addToEnvironment(Context.REFERRAL, "follow");
+        assertEquals("ignore", preValue);
+        returnedEnv = (Hashtable<Object, Object>) context.getEnvironment();
+        assertTrue(returnedEnv.containsKey(Context.REFERRAL));
+        assertEquals("follow", returnedEnv.get(Context.REFERRAL));
+
+    }
+
+    public void test_removeFromEnvironment() throws Exception {
+        MockLdapClient client = new MockLdapClient();
+        Hashtable<Object, Object> env = new Hashtable<Object, Object>();
+
+        context = new LdapContextImpl(client, env, "cn=test");
+
+        Object preValue = context.removeFromEnvironment(Context.REFERRAL);
+        assertNull(preValue);
+        Hashtable<Object, Object> returnedEnv = (Hashtable<Object, Object>) context
+                .getEnvironment();
+        assertFalse(returnedEnv.containsKey(Context.REFERRAL));
+
+        env.clear();
+        env.put(Context.REFERRAL, "ignore");
+        context = new LdapContextImpl(client, env, "cn=test");
+        preValue = context.removeFromEnvironment(Context.REFERRAL);
+        assertEquals("ignore", preValue);
+        returnedEnv = (Hashtable<Object, Object>) context.getEnvironment();
+        assertFalse(returnedEnv.containsKey(Context.REFERRAL));
+    }
 }



Mime
View raw message