Return-Path: Delivered-To: apmail-harmony-commits-archive@www.apache.org Received: (qmail 62712 invoked from network); 21 Nov 2007 13:30:57 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 21 Nov 2007 13:30:57 -0000 Received: (qmail 61953 invoked by uid 500); 21 Nov 2007 13:30:45 -0000 Delivered-To: apmail-harmony-commits-archive@harmony.apache.org Received: (qmail 61931 invoked by uid 500); 21 Nov 2007 13:30:45 -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 61921 invoked by uid 99); 21 Nov 2007 13:30:45 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 21 Nov 2007 05:30:45 -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; Wed, 21 Nov 2007 13:30:47 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 930811A9832; Wed, 21 Nov 2007 05:30:25 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@harmony.apache.org From: tellison@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20071121133025.930811A9832@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 Context.addToEnvironment or + * Context.removeFromEnvironment 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 connectionProperties = new HashSet(); + + 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 LdapContextImpl * * @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 env = new Hashtable(); + + context = new LdapContextImpl(client, env, "cn=test"); + + Object preValue = context.addToEnvironment(Context.REFERRAL, "ignore"); + assertNull(preValue); + Hashtable returnedEnv = (Hashtable) context + .getEnvironment(); + assertTrue(returnedEnv.containsKey(Context.REFERRAL)); + assertEquals("ignore", returnedEnv.get(Context.REFERRAL)); + + preValue = context.addToEnvironment(Context.REFERRAL, "follow"); + assertEquals("ignore", preValue); + returnedEnv = (Hashtable) context.getEnvironment(); + assertTrue(returnedEnv.containsKey(Context.REFERRAL)); + assertEquals("follow", returnedEnv.get(Context.REFERRAL)); + + } + + public void test_removeFromEnvironment() throws Exception { + MockLdapClient client = new MockLdapClient(); + Hashtable env = new Hashtable(); + + context = new LdapContextImpl(client, env, "cn=test"); + + Object preValue = context.removeFromEnvironment(Context.REFERRAL); + assertNull(preValue); + Hashtable returnedEnv = (Hashtable) 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) context.getEnvironment(); + assertFalse(returnedEnv.containsKey(Context.REFERRAL)); + } }