harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r598944 - in /harmony/enhanced/classlib/trunk/modules/jndi/src: main/java/org/apache/harmony/jndi/internal/nls/ main/java/org/apache/harmony/jndi/provider/ldap/ test/java/org/apache/harmony/jndi/provider/ldap/ test/java/org/apache/harmony/j...
Date Wed, 28 Nov 2007 10:35:48 GMT
Author: tellison
Date: Wed Nov 28 02:35:44 2007
New Revision: 598944

URL: http://svn.apache.org/viewvc?rev=598944&view=rev
Log:
Apply patch HARMONY-5209 ([classlib][jndi][ldap] implements LdapReferralException)

Added:
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/ReferralExceptionImpl.java
  (with props)
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/ReferralExceptionTest.java
  (with props)
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/ldapURLContextTest.java
  (with props)
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/mock/
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/mock/BindResponse.java
  (with props)
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/mock/EncodableLdapResult.java
  (with props)
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/mock/MockLdapMessage.java
  (with props)
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/mock/MockLdapServer.java
  (with props)
Modified:
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/internal/nls/messages.properties
    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/LdapMessage.java

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/internal/nls/messages.properties
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/internal/nls/messages.properties?rev=598944&r1=598943&r2=598944&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/internal/nls/messages.properties
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/internal/nls/messages.properties
Wed Nov 28 02:35:44 2007
@@ -199,12 +199,19 @@
 ldap.21=was not found
 ldap.22=foundObjects is null
 ldap.23= not found in jar file 
+ldap.24=can only bind Reference, Referenceable, Serializable, DirContext 
+ldap.25=limit of following in a chain of referrals is exceeded.
 ldap.26=Only instances of CompositeName class or LdapName class are acceptable
+ldap.27=Non-null ModificationItem is required for modification
 ldap.28=Parameter of filter should not be null
 ldap.29=Invalid search filter
-ldap.30=Illegal value: {0} for {1} property
-ldap.34=[LDAP: error code {0} - {1}]
-ldap.35=[LDAP: error code {0}]
+ldap.2A=old name and new name should be in same namespace
 ldap.2B=LDAP URL should not be null
 ldap.2C=Invalid LDAP URL
 ldap.2D=LDAP URL may only contain host, port and dn components
+ldap.30=Illegal value: {0} for {1} property
+ldap.31=Read LDAP response message time out
+ldap.32=Target cannot span multiple namespaces: {0}
+ldap.33=Scope should be one of 'OBJECT_SCOPE', 'ONELEVEL_SCOPE' or 'SUBTREE_SCOPE'
+ldap.34=[LDAP: error code {0} - {1}]
+ldap.35=[LDAP: error code {0}]

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=598944&r1=598943&r2=598944&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
Wed Nov 28 02:35:44 2007
@@ -246,11 +246,12 @@
                                     .getExtendedRequest().getID().equals(
                                             StartTlsRequest.OID)) {
                         /*
-                         * When establishing TLS by StartTls extended operation, no 
+                         * When establishing TLS by StartTls extended operation,
+                         * no
                          */
                         isStopped = true;
                     }
-                    
+
                     synchronized (element.lock) {
                         element.lock.notify();
                     }
@@ -347,16 +348,24 @@
      */
     private LdapMessage waitResponse(Integer messageID, Object lock)
             throws IOException {
+        Element element = requests.get(messageID);
 
-        synchronized (lock) {
-            try {
-                lock.wait(MAX_WAIT_TIME);
-            } catch (InterruptedException e) {
-                // ignore
+        /*
+         * test if dispatcher has not received response message from server,
+         * wait response
+         */
+        if (element.response.getMessageId() != messageID.intValue()) {
+
+            synchronized (lock) {
+                try {
+                    lock.wait(MAX_WAIT_TIME);
+                } catch (InterruptedException e) {
+                    // ignore
+                }
             }
         }
-
-        Element element = requests.get(messageID);
+        
+        element = requests.get(messageID);
 
         // wait time out
         if (element.response.getMessageId() != messageID.intValue()) {
@@ -669,7 +678,7 @@
     protected void finalize() {
         close();
     }
-    
+
     public Socket getSocket() {
         return this.socket;
     }

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapMessage.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapMessage.java?rev=598944&r1=598943&r2=598944&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapMessage.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapMessage.java
Wed Nov 28 02:35:44 2007
@@ -218,4 +218,8 @@
         return responseOp;
     }
 
+    public ASN1Encodable getRequestOp() {
+        return requestOp;
+    }
+
 }

Added: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/ReferralExceptionImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/ReferralExceptionImpl.java?rev=598944&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/ReferralExceptionImpl.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/ReferralExceptionImpl.java
Wed Nov 28 02:35:44 2007
@@ -0,0 +1,122 @@
+/* 
+ *  Licensed to the Apache Software Foundation (ASF) under one or more 
+ *  contributor license agreements.  See the NOTICE file distributed with 
+ *  this work for additional information regarding copyright ownership. 
+ *  The ASF licenses this file to You under the Apache License, Version 2.0 
+ *  (the "License"); you may not use this file except in compliance with 
+ *  the License.  You may obtain a copy of the License at 
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0 
+ * 
+ *  Unless required by applicable law or agreed to in writing, software 
+ *  distributed under the License is distributed on an "AS IS" BASIS, 
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+ *  See the License for the specific language governing permissions and 
+ *  limitations under the License. 
+ */
+
+package org.apache.harmony.jndi.provider.ldap;
+
+import java.util.Hashtable;
+
+import javax.naming.Context;
+import javax.naming.NamingException;
+import javax.naming.ldap.Control;
+import javax.naming.ldap.LdapReferralException;
+
+import org.apache.harmony.jndi.provider.ldap.parser.LdapUrlParser;
+
+public class ReferralExceptionImpl extends LdapReferralException {
+
+    private static final long serialVersionUID = -485662331078312979L;
+
+    private String targetDN;
+
+    private String[] referrals;
+
+    private Hashtable<Object, Object> env;
+
+    private int index = 0;
+
+    public ReferralExceptionImpl(String targetDN, String[] referrals,
+            Hashtable<Object, Object> env) {
+        this.targetDN = targetDN;
+        this.referrals = referrals;
+        if (env == null) {
+            this.env = new Hashtable<Object, Object>();
+        } else {
+            this.env = (Hashtable<Object, Object>) env.clone();
+        }
+    }
+
+    @Override
+    public Context getReferralContext() throws NamingException {
+        if (index >= referrals.length) {
+            return null;
+        }
+
+        LdapUrlParser parser = LdapUtils.parserURL(referrals[index], false);
+
+        String host = parser.getHost();
+        int port = parser.getPort();
+
+        LdapClient client = LdapClient.newInstance(host, port, env);
+
+        LdapContextImpl context = new LdapContextImpl(client,
+                (Hashtable<Object, Object>) env, targetDN);
+        return context;
+
+    }
+
+    @Override
+    public Context getReferralContext(Hashtable<?, ?> h) throws NamingException {
+        if (index >= referrals.length) {
+            return null;
+        }
+
+        if (h == null) {
+            return getReferralContext();
+        }
+
+        Hashtable<Object, Object> myEnv = (Hashtable<Object, Object>) h.clone();
+        LdapUrlParser parser = LdapUtils.parserURL(referrals[index], true);
+
+        String host = parser.getHost();
+        int port = parser.getPort();
+
+        LdapClient client = LdapClient.newInstance(host, port, myEnv);
+
+        LdapContextImpl context = new LdapContextImpl(client, myEnv, targetDN);
+
+        return context;
+    }
+
+    @Override
+    public Object getReferralInfo() {
+        if (index >= referrals.length) {
+            return null;
+        }
+        return referrals[index];
+    }
+
+    @Override
+    public void retryReferral() {
+        // TODO what should we do?
+        // do nothing
+    }
+
+    @Override
+    public boolean skipReferral() {
+        index++;
+        return index < referrals.length;
+    }
+
+    @Override
+    public Context getReferralContext(Hashtable<?, ?> h, Control[] cs)
+            throws NamingException {
+        Hashtable<Object, Object> myEnv = (Hashtable<Object, Object>) h;
+        myEnv.put("java.naming.ldap.control.connect", cs); //$NON-NLS-1$
+        return getReferralContext(myEnv);
+    }
+
+}

Propchange: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/ReferralExceptionImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 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=598944&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/ReferralExceptionTest.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/ReferralExceptionTest.java
Wed Nov 28 02:35:44 2007
@@ -0,0 +1,127 @@
+/* 
+ *  Licensed to the Apache Software Foundation (ASF) under one or more 
+ *  contributor license agreements.  See the NOTICE file distributed with 
+ *  this work for additional information regarding copyright ownership. 
+ *  The ASF licenses this file to You under the Apache License, Version 2.0 
+ *  (the "License"); you may not use this file except in compliance with 
+ *  the License.  You may obtain a copy of the License at 
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0 
+ * 
+ *  Unless required by applicable law or agreed to in writing, software 
+ *  distributed under the License is distributed on an "AS IS" BASIS, 
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+ *  See the License for the specific language governing permissions and 
+ *  limitations under the License. 
+ */
+
+package org.apache.harmony.jndi.provider.ldap;
+
+import java.util.Hashtable;
+
+import javax.naming.Context;
+
+import junit.framework.TestCase;
+
+import org.apache.harmony.jndi.provider.ldap.asn1.LdapASN1Constant;
+import org.apache.harmony.jndi.provider.ldap.mock.BindResponse;
+import org.apache.harmony.jndi.provider.ldap.mock.MockLdapServer;
+
+public class ReferralExceptionTest extends TestCase {
+	private MockLdapServer server;
+
+	public void setUp() {
+		server = new MockLdapServer();
+		server.start();
+	}
+
+	public void tearDown() {
+		server.stop();
+	}
+
+	public void testGetReferralInfo() throws Exception {
+		Hashtable<Object, Object> env = new Hashtable<Object, Object>();
+		env.put(Context.REFERRAL, "throw");
+		String[] referrals = new String[] { "ldap://localhost:389",
+				"ldap://localhost:333" };
+		ReferralExceptionImpl ex = new ReferralExceptionImpl("cn=dn",
+				referrals, env);
+		// try {
+		// context.search("", null);
+		// } catch (ReferralException e) {
+		// ex = e;
+		// }
+
+		assertEquals(referrals[0], ex.getReferralInfo());
+
+		assertTrue(ex.skipReferral());
+
+		assertEquals(referrals[1], ex.getReferralInfo());
+
+		assertFalse(ex.skipReferral());
+
+		assertNull(ex.getReferralInfo());
+	}
+
+	public void testGetReferralContext() throws Exception {
+		Hashtable<Object, Object> env = new Hashtable<Object, Object>();
+
+		env.put(Context.REFERRAL, "throw");
+		env.put("test.getReferralContext", "GetReferralContext");
+
+		String[] referrals = new String[] { server.getURL() };
+		ReferralExceptionImpl ex = new ReferralExceptionImpl("cn=dn",
+				referrals, env);
+
+		assertEquals(referrals[0], ex.getReferralInfo());
+
+		server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+				LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
+
+		Context refContext = ex.getReferralContext();
+
+		Hashtable<Object, Object> refEnv = (Hashtable<Object, Object>) refContext
+				.getEnvironment();
+
+		assertEquals(env.get(Context.REFERRAL), refEnv.get(Context.REFERRAL));
+		assertEquals(env.get("test.getReferralContext"), refEnv
+				.get("test.getReferralContext"));
+
+		assertFalse(ex.skipReferral());
+
+		assertNull(ex.getReferralInfo());
+	}
+
+	public void testGetReferralContext2() throws Exception {
+		Hashtable<Object, Object> initialEnv = new Hashtable<Object, Object>();
+
+		initialEnv.put(Context.REFERRAL, "throw");
+		initialEnv.put("test.getReferralContext", "GetReferralContext");
+
+		String[] referrals = new String[] { server.getURL() };
+		ReferralExceptionImpl ex = new ReferralExceptionImpl("cn=dn",
+				referrals, initialEnv);
+
+		Hashtable<Object, Object> env = new Hashtable<Object, Object>();
+
+		env.put(Context.REFERRAL, "follow");
+		env.put("test.getReferralContext", "changed");
+
+		server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+				LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
+
+		assertEquals(referrals[0], ex.getReferralInfo());
+		Context refContext = ex.getReferralContext(env);
+
+		Hashtable<Object, Object> refEnv = (Hashtable<Object, Object>) refContext
+				.getEnvironment();
+
+		assertEquals(env.get(Context.REFERRAL), refEnv.get(Context.REFERRAL));
+		assertEquals(env.get("test.getReferralContext"), refEnv
+				.get("test.getReferralContext"));
+
+		assertFalse(ex.skipReferral());
+
+		assertNull(ex.getReferralInfo());
+	}
+}

Propchange: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/ReferralExceptionTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 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=598944&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/ldapURLContextTest.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/ldapURLContextTest.java
Wed Nov 28 02:35:44 2007
@@ -0,0 +1,123 @@
+/* 
+ *  Licensed to the Apache Software Foundation (ASF) under one or more 
+ *  contributor license agreements.  See the NOTICE file distributed with 
+ *  this work for additional information regarding copyright ownership. 
+ *  The ASF licenses this file to You under the Apache License, Version 2.0 
+ *  (the "License"); you may not use this file except in compliance with 
+ *  the License.  You may obtain a copy of the License at 
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0 
+ * 
+ *  Unless required by applicable law or agreed to in writing, software 
+ *  distributed under the License is distributed on an "AS IS" BASIS, 
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+ *  See the License for the specific language governing permissions and 
+ *  limitations under the License. 
+ */
+
+package org.apache.harmony.jndi.provider.ldap;
+
+import java.util.Hashtable;
+
+import javax.naming.Context;
+import javax.naming.NamingException;
+import javax.naming.ldap.LdapContext;
+import javax.naming.spi.ResolveResult;
+
+import org.apache.harmony.jndi.provider.ldap.asn1.LdapASN1Constant;
+import org.apache.harmony.jndi.provider.ldap.mock.BindResponse;
+import org.apache.harmony.jndi.provider.ldap.mock.MockLdapServer;
+
+import junit.framework.TestCase;
+
+public class ldapURLContextTest extends TestCase {
+    private MockLdapServer server;
+
+    @Override
+    public void setUp() {
+        server = new MockLdapServer();
+        server.start();
+    }
+
+    @Override
+    public void tearDown() {
+        server.stop();
+    }
+
+    public void testGetRootURLContext() throws Exception {
+        MockLdapURLContext context = new MockLdapURLContext();
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
+
+        ResolveResult result = context.getRootURLContext(server.getURL(), null);
+
+        assertEquals("", result.getRemainingName().toString());
+        assertTrue(result.getResolvedObj() instanceof LdapContextImpl);
+    }
+
+    public void testGetRootURLContext2() throws Exception {
+        Hashtable<Object, Object> initialEnv = new Hashtable<Object, Object>();
+        initialEnv.put(Context.REFERRAL, "throw");
+        initialEnv.put("test.getRootURLContext", "test");
+
+        MockLdapURLContext context = new MockLdapURLContext(initialEnv);
+
+        Hashtable<Object, Object> env = new Hashtable<Object, Object>();
+        env.put(Context.REFERRAL, "ignore");
+        env.put("test.getRootURLContext", "GetRootURLContext");
+
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
+
+        ResolveResult result = context.getRootURLContext(server.getURL(), env);
+
+        assertEquals("", result.getRemainingName().toString());
+        assertTrue(result.getResolvedObj() instanceof LdapContextImpl);
+
+        LdapContext newContext = (LdapContext) result.getResolvedObj();
+        Hashtable<Object, Object> newEnv = (Hashtable<Object, Object>) newContext
+                .getEnvironment();
+
+        assertEquals("ignore", newEnv.get(Context.REFERRAL));
+        assertEquals("GetRootURLContext", newEnv.get("test.getRootURLContext"));
+    }
+
+    public void testGetRootURLContext3() throws Exception {
+        Hashtable<Object, Object> initialEnv = new Hashtable<Object, Object>();
+        initialEnv.put(Context.REFERRAL, "throw");
+        initialEnv.put("test.getRootURLContext", "test");
+
+        MockLdapURLContext context = new MockLdapURLContext(initialEnv);
+
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
+
+        ResolveResult result = context.getRootURLContext(server.getURL(), null);
+
+        assertEquals("", result.getRemainingName().toString());
+        assertTrue(result.getResolvedObj() instanceof LdapContextImpl);
+
+        LdapContext newContext = (LdapContext) result.getResolvedObj();
+        Hashtable<Object, Object> newEnv = (Hashtable<Object, Object>) newContext
+                .getEnvironment();
+
+        assertEquals("throw", newEnv.get(Context.REFERRAL));
+        assertEquals("test", newEnv.get("test.getRootURLContext"));
+    }
+
+    public static class MockLdapURLContext extends ldapURLContext {
+        public MockLdapURLContext() {
+            super();
+        }
+
+        public MockLdapURLContext(Hashtable<Object, Object> env) {
+            super(env);
+        }
+
+        @Override
+        public ResolveResult getRootURLContext(String url, Hashtable<?, ?> env)
+                throws NamingException {
+            return super.getRootURLContext(url, env);
+        }
+    }
+}

Propchange: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/ldapURLContextTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/mock/BindResponse.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/mock/BindResponse.java?rev=598944&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/mock/BindResponse.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/mock/BindResponse.java
Wed Nov 28 02:35:44 2007
@@ -0,0 +1,42 @@
+/* 
+ *  Licensed to the Apache Software Foundation (ASF) under one or more 
+ *  contributor license agreements.  See the NOTICE file distributed with 
+ *  this work for additional information regarding copyright ownership. 
+ *  The ASF licenses this file to You under the Apache License, Version 2.0 
+ *  (the "License"); you may not use this file except in compliance with 
+ *  the License.  You may obtain a copy of the License at 
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0 
+ * 
+ *  Unless required by applicable law or agreed to in writing, software 
+ *  distributed under the License is distributed on an "AS IS" BASIS, 
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+ *  See the License for the specific language governing permissions and 
+ *  limitations under the License. 
+ */
+
+package org.apache.harmony.jndi.provider.ldap.mock;
+
+import org.apache.harmony.jndi.provider.ldap.asn1.ASN1Encodable;
+
+public class BindResponse implements ASN1Encodable {
+
+    private EncodableLdapResult result;
+
+    private String saslCreds;
+
+    public BindResponse() {
+        this(new EncodableLdapResult(), null);
+    }
+
+    public BindResponse(EncodableLdapResult result, String saslCreds) {
+        this.result = result;
+        this.saslCreds = saslCreds;
+    }
+
+    public void encodeValues(Object[] values) {
+        result.encodeValues(values);
+        values[4] = saslCreds;
+    }
+
+}

Propchange: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/mock/BindResponse.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/mock/EncodableLdapResult.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/mock/EncodableLdapResult.java?rev=598944&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/mock/EncodableLdapResult.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/mock/EncodableLdapResult.java
Wed Nov 28 02:35:44 2007
@@ -0,0 +1,86 @@
+/* 
+ *  Licensed to the Apache Software Foundation (ASF) under one or more 
+ *  contributor license agreements.  See the NOTICE file distributed with 
+ *  this work for additional information regarding copyright ownership. 
+ *  The ASF licenses this file to You under the Apache License, Version 2.0 
+ *  (the "License"); you may not use this file except in compliance with 
+ *  the License.  You may obtain a copy of the License at 
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0 
+ * 
+ *  Unless required by applicable law or agreed to in writing, software 
+ *  distributed under the License is distributed on an "AS IS" BASIS, 
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+ *  See the License for the specific language governing permissions and 
+ *  limitations under the License. 
+ */
+
+package org.apache.harmony.jndi.provider.ldap.mock;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.harmony.jndi.provider.ldap.asn1.ASN1Encodable;
+import org.apache.harmony.jndi.provider.ldap.asn1.Utils;
+import org.apache.harmony.security.asn1.ASN1Integer;
+
+public class EncodableLdapResult implements ASN1Encodable {
+
+    private int resultCode;
+
+    private String machedDN;
+
+    private String errorMessage;
+
+    private String[] referrals;
+
+    public EncodableLdapResult() {
+        this(0, "", "", null);
+    }
+
+    public String getErrorMessage() {
+        return errorMessage;
+    }
+
+    public String getMachedDN() {
+        return machedDN;
+    }
+
+    public String[] getReferrals() {
+        return referrals;
+    }
+
+    public int getResultCode() {
+        return resultCode;
+    }
+
+    public EncodableLdapResult(int resultCode, String machedDN,
+            String errorMessage, String[] referrals) {
+        this.resultCode = resultCode;
+        if (machedDN == null) {
+            this.machedDN = "";
+        } else {
+            this.machedDN = machedDN;
+        }
+        if (errorMessage == null) {
+            this.errorMessage = "";
+        } else {
+            this.errorMessage = errorMessage;
+        }
+        this.referrals = referrals;
+    }
+
+    public void encodeValues(Object[] values) {
+        values[0] = ASN1Integer.fromIntValue(resultCode);
+        values[1] = Utils.getBytes(machedDN);
+        values[2] = Utils.getBytes(errorMessage);
+        if (referrals != null) {
+            List<byte[]> refs = new ArrayList<byte[]>();
+            for (int i = 0; i < referrals.length; i++) {
+                refs.add(Utils.getBytes(referrals[i]));
+            }
+            values[3] = refs;
+        }
+    }
+
+}

Propchange: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/mock/EncodableLdapResult.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/mock/MockLdapMessage.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/mock/MockLdapMessage.java?rev=598944&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/mock/MockLdapMessage.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/mock/MockLdapMessage.java
Wed Nov 28 02:35:44 2007
@@ -0,0 +1,45 @@
+/* 
+ *  Licensed to the Apache Software Foundation (ASF) under one or more 
+ *  contributor license agreements.  See the NOTICE file distributed with 
+ *  this work for additional information regarding copyright ownership. 
+ *  The ASF licenses this file to You under the Apache License, Version 2.0 
+ *  (the "License"); you may not use this file except in compliance with 
+ *  the License.  You may obtain a copy of the License at 
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0 
+ * 
+ *  Unless required by applicable law or agreed to in writing, software 
+ *  distributed under the License is distributed on an "AS IS" BASIS, 
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+ *  See the License for the specific language governing permissions and 
+ *  limitations under the License. 
+ */
+
+package org.apache.harmony.jndi.provider.ldap.mock;
+
+import org.apache.harmony.jndi.provider.ldap.LdapMessage;
+import org.apache.harmony.security.asn1.ASN1Integer;
+
+public class MockLdapMessage extends LdapMessage {
+	
+	private int messageId;
+	
+	public MockLdapMessage(LdapMessage msg) {
+		super(msg.getOperationIndex(), msg.getRequestOp(), null);
+	}
+	
+	@Override
+    public void encodeValues(Object[] values) {
+		super.encodeValues(values);
+		values[0] = ASN1Integer.fromIntValue(messageId);
+	}
+
+	@Override
+    public int getMessageId() {
+		return messageId;
+	}
+
+	public void setMessageId(int messageId) {
+		this.messageId = messageId;
+	}
+}

Propchange: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/mock/MockLdapMessage.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 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=598944&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/mock/MockLdapServer.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/mock/MockLdapServer.java
Wed Nov 28 02:35:44 2007
@@ -0,0 +1,161 @@
+/* 
+ *  Licensed to the Apache Software Foundation (ASF) under one or more 
+ *  contributor license agreements.  See the NOTICE file distributed with 
+ *  this work for additional information regarding copyright ownership. 
+ *  The ASF licenses this file to You under the Apache License, Version 2.0 
+ *  (the "License"); you may not use this file except in compliance with 
+ *  the License.  You may obtain a copy of the License at 
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0 
+ * 
+ *  Unless required by applicable law or agreed to in writing, software 
+ *  distributed under the License is distributed on an "AS IS" BASIS, 
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+ *  See the License for the specific language governing permissions and 
+ *  limitations under the License. 
+ */
+
+package org.apache.harmony.jndi.provider.ldap.mock;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.ServerSocket;
+import java.net.Socket;
+
+import org.apache.harmony.jndi.provider.ldap.LdapMessage;
+import org.apache.harmony.security.asn1.ASN1Integer;
+
+/**
+ * This class is a mock ldap server which only support one connection.
+ * 
+ * NOTE: before client send request to the mock server, must set expected
+ * response message sequence using <code>etResponseSeq(LdapMessage[])</code>
+ * method, so the server will send response messages in order of parameter
+ * <code>LdapMessage[]</code>.
+ */
+public class MockLdapServer implements Runnable {
+
+    private ServerSocket server;
+
+    private Socket socket;
+
+    private LdapMessage[] responses;
+
+    private int port;
+
+    private Object lock = new Object();
+
+    private boolean isStopped;
+
+    private static int DEFAULT_PORT = 1024;
+
+    public void start() {
+        port = DEFAULT_PORT;
+        while (true) {
+            try {
+                server = new ServerSocket(port);
+                break;
+            } catch (IOException e) {
+                ++port;
+            }
+        }
+
+        isStopped = false;
+        new Thread(this).start();
+    }
+
+    public void stop() {
+        isStopped = true;
+
+        synchronized (lock) {
+            lock.notify();
+        }
+
+        if (server != null) {
+            try {
+                server.close();
+            } catch (IOException e) {
+                // ignore
+            }
+        }
+
+        if (socket != null) {
+            try {
+                socket.close();
+            } catch (IOException e) {
+                // ignore
+            }
+        }
+    }
+
+    public int getPort() {
+        return port;
+    }
+
+    public void setResponseSeq(LdapMessage[] msges) {
+        this.responses = msges;
+
+        synchronized (lock) {
+            lock.notify();
+        }
+    }
+
+    public void run() {
+        InputStream in = null;
+        OutputStream out = null;
+        try {
+            socket = server.accept();
+            in = socket.getInputStream();
+            out = socket.getOutputStream();
+            while (!isStopped) {
+                if (responses == null) {
+                    try {
+                        synchronized (lock) {
+                            lock.wait();
+                        }
+                    } catch (InterruptedException e) {
+                        // ignore
+                    }
+                } else {
+
+                    for (int i = 0; i < responses.length; i++) {
+                        final MockLdapMessage response = new MockLdapMessage(
+                                responses[i]);
+                        LdapMessage request = new LdapMessage(null) {
+                            public void decodeValues(Object[] values) {
+                                response.setMessageId(ASN1Integer
+                                        .toIntValue(values[0]));
+                            }
+                        };
+                        request.decode(in);
+                        out.write(response.encode());
+                    }
+                    responses = null;
+                }
+            }
+        } catch (IOException e) {
+            // FIXME deal with the exception
+        } finally {
+            try {
+                if (socket != null) {
+                    socket.close();
+                }
+
+                if (in != null) {
+                    in.close();
+                }
+
+                if (out != null) {
+                    out.close();
+                }
+            } catch (IOException e) {
+                // ignore
+            }
+        }
+    }
+
+    public String getURL() {
+        return "ldap://localhost:" + port;
+    }
+}

Propchange: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/mock/MockLdapServer.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message