harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lvj...@apache.org
Subject svn commit: r643260 - in /harmony/enhanced/classlib/trunk/modules/jndi/src: main/java/org/apache/harmony/jndi/provider/ldap/ test/java/org/apache/harmony/jndi/provider/ldap/ test/java/org/apache/harmony/jndi/provider/ldap/mock/
Date Tue, 01 Apr 2008 03:47:23 GMT
Author: lvjing
Date: Mon Mar 31 20:47:21 2008
New Revision: 643260

URL: http://svn.apache.org/viewvc?rev=643260&view=rev
Log:
Apply patch for HARMONY-5670, [classlib][jndi][ldap] - new properties should make effect when
next calling Context methods

Modified:
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapClient.java
    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/LdapContextServerMockedTest.java
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/MockLdapClient.java
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/ReferralExceptionTest.java
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/ldapURLContextTest.java
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/mock/MockLdapServer.java

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapClient.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapClient.java?rev=643260&r1=643259&r2=643260&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapClient.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapClient.java
Mon Mar 31 20:47:21 2008
@@ -94,6 +94,7 @@
      * registered UnsolicitedListener
      */
     private List<UnsolicitedListener> unls = new ArrayList<UnsolicitedListener>();
+    private int referCount = 0;
 
     // constructor for test
     public LdapClient() {
@@ -705,6 +706,16 @@
         }
     }
 
+    // FIXME simple implementation
+    public void use() {
+        referCount++;
+    }
+
+    // FIXME simple implementation
+    public void unuse() {
+        referCount--;
+    }
+
     private void notifyPersistenSearchListener(Element element) {
         PersistentSearchResult psr = (PersistentSearchResult) ((SearchOp) element.response
                 .getResponseOp()).getSearchResult();
@@ -726,4 +737,7 @@
         }
     }
 
+    public int getReferCount() {
+        return referCount;
+    }
 }

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=643260&r1=643259&r2=643260&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
Mon Mar 31 20:47:21 2008
@@ -136,6 +136,10 @@
      */
     private Control[] connCtls;
 
+    private boolean isReBind = false;
+
+    private boolean isReConnect = false;
+
     private HashMap<NamingListener, List<Integer>> listeners;
 
     private List<UnsolicitedNotificationListener> unls;
@@ -199,20 +203,14 @@
             Hashtable<Object, Object> environment, String dn)
             throws NamingException {
         initial(client, environment, dn);
-
-        try {
             doBindOperation(connCtls);
-        } catch (IOException e) {
-            CommunicationException ex = new CommunicationException();
-            ex.setRootCause(e);
-            throw ex;
-        }
     }
 
     private void initial(LdapClient ldapClient,
             Hashtable<Object, Object> environment, String dn)
             throws InvalidNameException {
         this.client = ldapClient;
+        client.use();
         if (environment == null) {
             this.env = new Hashtable<Object, Object>();
         } else {
@@ -239,13 +237,16 @@
      * @throws NamingException
      * @throws ParseException
      */
-    private void doBindOperation(Control[] connCtsl) throws IOException,
-            NamingException {
+    private void doBindOperation(Control[] connCtsl) throws NamingException {
+        if (client.getReferCount() > 1) {
+            changeConnection();
+        }
 
         SaslBind saslBind = new SaslBind();
         LdapResult result = null;
 
         SaslBind.AuthMech authMech = saslBind.valueAuthMech(env);
+        try {
         if (authMech == SaslBind.AuthMech.None) {
             BindOp bind = new BindOp("", "", null, null);
             client.doOperation(bind, connCtsl);
@@ -259,6 +260,11 @@
             result = bind.getResult();
         } else if (authMech == SaslBind.AuthMech.SASL) {
             result = saslBind.doSaslBindOperation(env, client, connCtsl);
+            }
+        } catch (IOException e) {
+            CommunicationException ex = new CommunicationException();
+            ex.setRootCause(e);
+            throw ex;
         }
 
         if (LdapUtils.getExceptionFromResult(result) != null) {
@@ -316,14 +322,16 @@
     }
 
     public void reconnect(Control[] ac) throws NamingException {
-        connCtls = ac;
-        try {
-            doBindOperation(connCtls);
-        } catch (IOException e) {
-            CommunicationException ex = new CommunicationException();
-            ex.setRootCause(e);
-            throw ex;
+        connCtls = copyControls(ac);
+        if (isReConnect) {
+            try {
+                changeConnection();
+            } finally {
+                isReConnect = false;
+            }
         }
+
+        doBindOperation(connCtls);
     }
 
     public void setRequestControls(Control[] controls) throws NamingException {
@@ -1377,6 +1385,8 @@
     }
 
     LdapSearchResult doSearch(SearchOp op) throws NamingException {
+        applyEnvChange();
+
         if (env.get(LDAP_DEREF_ALIASES) != null) {
             String derefAliases = (String) env.get(LDAP_DEREF_ALIASES);
             if (derefAliases.equals("always")) {
@@ -1480,6 +1490,16 @@
         return op.getSearchResult();
     }
 
+    private void applyEnvChange() throws NamingException {
+        if (isReBind) {
+            try {
+                reconnect(connCtls);
+            } finally {
+                isReBind = false;
+            }
+        }
+    }
+
     /**
      * Follow referrals in SearchResultReference. Referrals in
      * SearchResultReference is different with LDAPResult, which may contians
@@ -1537,6 +1557,10 @@
     }
 
     public Object addToEnvironment(String s, Object o) throws NamingException {
+        if (s == null || o == null) {
+            throw new NullPointerException();
+        }
+
         Object preValue = env.put(s, o);
 
         // if preValue equals o, do nothing
@@ -1545,7 +1569,13 @@
             return preValue;
         }
 
-        updateEnvironment(s);
+        if (connectionProperties.contains(s)) {
+            if (s.equals(Context.SECURITY_PROTOCOL)
+                    || s.equals("java.naming.ldap.factory.socket")) {
+                isReConnect = true;
+            }
+            isReBind = true;
+        }
 
         return preValue;
     }
@@ -1561,6 +1591,7 @@
     public void close() throws NamingException {
         if (!isClosed) {
             isClosed = true;
+            client.unuse();
             client = null;
         }
     }
@@ -2062,6 +2093,10 @@
     }
 
     public Object removeFromEnvironment(String s) throws NamingException {
+        if (s == null) {
+            throw new NullPointerException();
+        }
+
         Object preValue = env.remove(s);
 
         // if s doesn't exist in env
@@ -2069,33 +2104,25 @@
             return preValue;
         }
 
-        updateEnvironment(s);
+        if (connectionProperties.contains(s)) {
+            if (s.equals(Context.SECURITY_PROTOCOL)
+                    || s.equals("java.naming.ldap.factory.socket")) {
+                isReConnect = true;
+            }
+            isReBind = true;
+        }
 
         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();
+    private void changeConnection() throws NamingException {
 
-                client = LdapClient.newInstance(address, port, env);
-                try {
-                    doBindOperation(connCtls);
-                } catch (IOException e) {
-                    CommunicationException ex = new CommunicationException();
-                    ex.setRootCause(e);
-                    throw ex;
-                }
-            } else {
-
-                reconnect(connCtls);
-            }
-        }
+        // use new socket factory to connect server
+        String address = client.getAddress();
+        int port = client.getPort();
+        client.unuse();
+        client = LdapClient.newInstance(address, port, env);
+        client.use();
     }
 
     public void rename(Name nOld, Name nNew) throws NamingException {
@@ -2176,6 +2203,8 @@
      * @throws NamingException
      */
     protected void doBasicOperation(LdapOperation op) throws NamingException {
+        applyEnvChange();
+
         LdapMessage message = null;
         try {
             message = client.doOperation(op, requestControls);
@@ -2449,7 +2478,8 @@
 
     private int doPersistentSearch(String targetDN, final String baseDN,
             Filter filter, SearchControls controls,
-            NamingListener namingListener) throws CommunicationException {
+            NamingListener namingListener) throws NamingException {
+        applyEnvChange();
 
         SearchOp op = new SearchOp(targetDN, controls, filter);
 

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextServerMockedTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextServerMockedTest.java?rev=643260&r1=643259&r2=643260&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextServerMockedTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextServerMockedTest.java
Mon Mar 31 20:47:21 2008
@@ -27,6 +27,7 @@
 import javax.naming.ldap.Control;
 import javax.naming.ldap.InitialLdapContext;
 import javax.naming.ldap.LdapContext;
+import javax.naming.ldap.PagedResultsControl;
 import javax.naming.ldap.SortControl;
 
 import junit.framework.TestCase;
@@ -42,7 +43,7 @@
     private Hashtable<Object, Object> env = new Hashtable<Object, Object>();
 
     @Override
-    public void setUp() {
+    public void setUp() throws Exception {
         server = new MockLdapServer();
         server.start();
         env.put(Context.INITIAL_CONTEXT_FACTORY,
@@ -132,8 +133,11 @@
 
         assertNull(context.getConnectControls());
 
+        server = new MockLdapServer(server);
+        server.start();
         server.setResponseSeq(new LdapMessage[] { new LdapMessage(
                 LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
+
         context.reconnect(new Control[] { new SortControl("",
                 Control.NONCRITICAL) });
 
@@ -325,6 +329,140 @@
         context.getAttributes("cn=test");
 
         referralServer.stop();
+    }
+
+    public void testAddToEnvironment() throws Exception {
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
+
+        assertNull(env.get(Context.REFERRAL));
+
+        InitialDirContext initialDirContext = new InitialDirContext(env);
+
+        // Context.REFERRAL changed doesn't cause re-bind operation
+        initialDirContext.addToEnvironment(Context.REFERRAL, "ignore");
+
+        assertEquals("ignore", initialDirContext.getEnvironment().get(
+                Context.REFERRAL));
+
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_DEL_RESPONSE, new EncodableLdapResult(),
+                null) });
+
+        initialDirContext.destroySubcontext("cn=test");
+
+        /*
+         * Context.SECURITY_AUTHENTICATION will case re-bind when invoke context
+         * methods at first time
+         */
+        Object preValue = initialDirContext.addToEnvironment(
+                Context.SECURITY_AUTHENTICATION, "none");
+        assertFalse("none".equals(preValue));
+
+        server.setResponseSeq(new LdapMessage[] {
+                new LdapMessage(LdapASN1Constant.OP_BIND_RESPONSE,
+                        new BindResponse(), null),
+                new LdapMessage(LdapASN1Constant.OP_SEARCH_RESULT_DONE,
+                        new EncodableLdapResult(), null) });
+
+        initialDirContext.lookup("");
+
+        preValue = initialDirContext.addToEnvironment(
+                Context.SECURITY_AUTHENTICATION, "simple");
+        assertFalse("simple".equals(preValue));
+
+        // initialDirContext is shared connection, will create new connection
+        server = new MockLdapServer(server);
+        server.start();
+        server.setResponseSeq(new LdapMessage[] {
+                new LdapMessage(LdapASN1Constant.OP_BIND_RESPONSE,
+                        new BindResponse(), null),
+                new LdapMessage(LdapASN1Constant.OP_SEARCH_RESULT_DONE,
+                        new EncodableLdapResult(), null) });
+
+        initialDirContext.lookup("");
+
+    }
+
+    public void testReconnect() throws Exception {
+        Control[] expected = new Control[] { new PagedResultsControl(10,
+                Control.NONCRITICAL) };
+        env.put("java.naming.ldap.control.connect", expected);
+
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
+        LdapContext context = new InitialLdapContext(env, null);
+
+        Control[] controls = context.getConnectControls();
+        assertNotNull(controls);
+        assertNotSame(expected, controls);
+
+        Control c = controls[0];
+        assertTrue(c instanceof PagedResultsControl);
+        assertEquals(Control.NONCRITICAL, ((PagedResultsControl) c)
+                .isCritical());
+        assertEquals(expected[0], c);
+
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
+
+        expected = new Control[] { new SortControl("", Control.NONCRITICAL) };
+        context.reconnect(expected);
+
+        controls = context.getConnectControls();
+        assertNotNull(controls);
+        assertEquals(1, controls.length);
+        c = controls[0];
+        assertTrue(c instanceof SortControl);
+        assertEquals(Control.NONCRITICAL, ((SortControl) c).isCritical());
+        assertNotSame(expected, controls);
+        assertEquals(expected[0], c);
+
+        expected[0] = new PagedResultsControl(10, Control.NONCRITICAL);
+        controls = context.getConnectControls();
+        assertNotNull(controls);
+        assertEquals(1, controls.length);
+        c = controls[0];
+        assertTrue(c instanceof SortControl);
+        assertEquals(Control.NONCRITICAL, ((SortControl) c).isCritical());
+
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
+        context.reconnect(null);
+
+        assertNull(context.getConnectControls());
+    }
+
+    public void testReconnect_share_connection() throws Exception {
+
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
+        LdapContext context = new InitialLdapContext(env, null);
+
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
+
+        // doesn't create new connection
+        context.reconnect(null);
+
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_SEARCH_RESULT_DONE,
+                new EncodableLdapResult(), null) });
+        // another and context share the same connection now
+        LdapContext another = (LdapContext) context.lookup("");
+
+        MockLdapServer one = new MockLdapServer(server);
+        one.start();
+
+        one.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
+        // create new connection
+        context.reconnect(null);
+
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
+        // use original connection
+        another.reconnect(null);
     }
 
 }

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/MockLdapClient.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/MockLdapClient.java?rev=643260&r1=643259&r2=643260&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/MockLdapClient.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/MockLdapClient.java
Mon Mar 31 20:47:21 2008
@@ -86,6 +86,16 @@
         request = op;
     }
 
+    @Override
+    public void use() {
+        // do nothing
+    }
+
+    @Override
+    public void unuse() {
+        // do nothing
+    }
+
     public ASN1Encodable getRequest() {
         return request;
     }

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/ReferralExceptionTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/ReferralExceptionTest.java?rev=643260&r1=643259&r2=643260&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/ReferralExceptionTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/ReferralExceptionTest.java
Mon Mar 31 20:47:21 2008
@@ -30,7 +30,7 @@
 public class ReferralExceptionTest extends TestCase {
 	private MockLdapServer server;
 
-	public void setUp() {
+	public void setUp() throws Exception {
 		server = new MockLdapServer();
 		server.start();
 	}

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/ldapURLContextTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/ldapURLContextTest.java?rev=643260&r1=643259&r2=643260&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/ldapURLContextTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/ldapURLContextTest.java
Mon Mar 31 20:47:21 2008
@@ -34,7 +34,7 @@
     private MockLdapServer server;
 
     @Override
-    public void setUp() {
+    public void setUp() throws Exception {
         server = new MockLdapServer();
         server.start();
     }

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/mock/MockLdapServer.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/mock/MockLdapServer.java?rev=643260&r1=643259&r2=643260&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/mock/MockLdapServer.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/mock/MockLdapServer.java
Mon Mar 31 20:47:21 2008
@@ -51,15 +51,19 @@
 
     private static int DEFAULT_PORT = 1024;
 
-    public void start() {
-        port = DEFAULT_PORT;
-        while (true) {
-            try {
-                server = new ServerSocket(port);
-                break;
-            } catch (IOException e) {
-                ++port;
-            }
+    public MockLdapServer() {
+        // do nothing
+    }
+
+    public MockLdapServer(MockLdapServer mockServer) {
+        this.server = mockServer.server;
+        port = mockServer.port;
+    }
+
+    public void start() throws IOException {
+        if (server == null) {
+            server = new ServerSocket(0);
+            port = server.getLocalPort();
         }
 
         isStopped = false;



Mime
View raw message