harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ton...@apache.org
Subject svn commit: r599715 - 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 Fri, 30 Nov 2007 07:41:25 GMT
Author: tonywu
Date: Thu Nov 29 23:41:16 2007
New Revision: 599715

URL: http://svn.apache.org/viewvc?rev=599715&view=rev
Log:
Apply patch HARMONY-5226 ([classlib][jndi][ldap] add referral support on ldap service provider)

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/LdapContextImpl.java
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSearchResult.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/mock/MockLdapServer.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=599715&r1=599714&r2=599715&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
Thu Nov 29 23:41:16 2007
@@ -178,6 +178,7 @@
 ldap.0E=[LDAP: error code 7 - Authentication method not supported]
 ldap.0F=[LDAP: error code 8 - strongAuthRequired: refused to return sorted results via insecure
protocol]
 ldap.10=[LDAP: error code 9 - Partial results being returned]
+ldap.1A=[LDAP: error code 10 - Referral]
 ldap.1B=[LDAP: error code 11 - adminLimitExceeded: too many matching entries for the server
to sort]
 ldap.1C=[LDAP: error code 16 - noSuchAttribute: unrecognized attribute type in sort key]
 ldap.11=[LDAP: error code 18 - inappropriateMatching: unrecognized or inappropriate matching
rule in sort key]

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=599715&r1=599714&r2=599715&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
Thu Nov 29 23:41:16 2007
@@ -44,15 +44,18 @@
 import javax.naming.ConfigurationException;
 import javax.naming.Context;
 import javax.naming.InvalidNameException;
+import javax.naming.LimitExceededException;
 import javax.naming.Name;
 import javax.naming.NameClassPair;
 import javax.naming.NameNotFoundException;
 import javax.naming.NameParser;
 import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
+import javax.naming.PartialResultException;
 import javax.naming.RefAddr;
 import javax.naming.Reference;
 import javax.naming.Referenceable;
+import javax.naming.ReferralException;
 import javax.naming.StringRefAddr;
 import javax.naming.directory.Attribute;
 import javax.naming.directory.Attributes;
@@ -90,7 +93,9 @@
 import org.apache.harmony.jndi.provider.ldap.asn1.Utils;
 import org.apache.harmony.jndi.provider.ldap.event.ECNotificationControl;
 import org.apache.harmony.jndi.provider.ldap.event.PersistentSearchResult;
+import org.apache.harmony.jndi.provider.ldap.ext.StartTlsResponseImpl;
 import org.apache.harmony.jndi.provider.ldap.parser.FilterParser;
+import org.apache.harmony.jndi.provider.ldap.parser.LdapUrlParser;
 import org.apache.harmony.jndi.provider.ldap.parser.ParseException;
 import org.apache.harmony.jndi.provider.ldap.sasl.SaslBind;
 
@@ -263,8 +268,22 @@
 
     public ExtendedResponse extendedOperation(ExtendedRequest request)
             throws NamingException {
-        // TODO not yet implemented
-        throw new NotYetImplementedException();
+        ExtendedOp op = new ExtendedOp(request);
+        try {
+            doBasicOperation(op);
+        } catch (ReferralException e) {
+            if (isFollowReferral(e)) {
+                LdapContext referralContext = (LdapContext) getReferralContext(e);
+                return referralContext.extendedOperation(request);
+            }
+            throw e;
+        }
+        ExtendedResponse response = op.getExtendedResponse();
+        // set existing underlying socket to startTls extended response
+        if (response instanceof StartTlsResponseImpl) {
+            ((StartTlsResponseImpl) response).setSocket(client.getSocket());
+        }
+        return response;
     }
 
     public Control[] getConnectControls() throws NamingException {
@@ -289,10 +308,10 @@
         return rtValue;
     }
 
-	public LdapContext newInstance(Control[] reqCtrls) throws NamingException {
+    public LdapContext newInstance(Control[] reqCtrls) throws NamingException {
         LdapContextImpl instance = new LdapContextImpl(this, env, contextDn
                 .toString());
-		instance.setRequestControls(reqCtrls);
+        instance.setRequestControls(reqCtrls);
         return instance;
     }
 
@@ -536,13 +555,59 @@
 
         // do add operation
         AddOp op = new AddOp(targetDN, la);
-
-        doBasicOperation(op);
+        try {
+            doBasicOperation(op);
+        } catch (ReferralException e) {
+            if (isFollowReferral(e)) {
+                DirContext referralContext = getReferralContext(e);
+                return referralContext.createSubcontext(name, attributes);
+            }
+            throw e;
+        }
 
         LdapResult result = op.getResult();
         return new LdapContextImpl(this, env, result.getMachedDN());
     }
 
+    private DirContext getReferralContext(ReferralException e)
+            throws LimitExceededException, NamingException {
+        int limit = 0;
+        if (env.get("java.naming.ldap.referral.limit") != null) {
+            limit = Integer.valueOf(
+                    (String) env.get("java.naming.ldap.referral.limit"))
+                    .intValue();
+        }
+
+        if (limit == -1) {
+            throw new LimitExceededException(Messages.getString("ldap.25")); //$NON-NLS-1$
+        }
+
+        if (limit == 1) {
+            limit = -1;
+        } else if (limit != 0) {
+            limit -= 1;
+        }
+
+        Hashtable<Object, Object> newEnv = (Hashtable<Object, Object>) env
+                .clone();
+        newEnv.put("java.naming.ldap.referral.limit", String.valueOf(limit));
+        DirContext referralContext = null;
+
+        while (true) {
+            try {
+                referralContext = (DirContext) e.getReferralContext(newEnv);
+                break;
+            } catch (NamingException ex) {
+                if (e.skipReferral()) {
+                    continue;
+                }
+                throw ex;
+            }
+        }
+
+        return referralContext;
+    }
+
     /**
      * merge two instanceof <code>Attributes</code> to one
      * 
@@ -1090,7 +1155,16 @@
             }
         }
 
-        doBasicOperation(op);
+        try {
+            doBasicOperation(op);
+        } catch (ReferralException e) {
+            if (isFollowReferral(e)) {
+                DirContext referralContext = getReferralContext(e);
+                referralContext.modifyAttributes(name, modificationItems);
+                return;
+            }
+            throw e;
+        }
     }
 
     public void modifyAttributes(String s, int i, Attributes attributes)
@@ -1187,13 +1261,24 @@
         tempName.addAll(name);
         String baseDN = tempName.toString();
         for (String dn : entries.keySet()) {
-            String relativeName = convertToRelativeName(dn, baseDN);
-            SearchResult sr = new SearchResult(relativeName, null, entries
-                    .get(dn));
-            sr.setNameInNamespace(dn);
+            SearchResult sr = null;
+            if (dn.startsWith("ldap://")) {
+                sr = new SearchResult(dn, null, entries.get(dn), false);
+                int index = dn.indexOf("/", 7);
+                sr.setNameInNamespace(dn.substring(index + 1, dn.length()));
+                list.add(sr);
+            } else {
+                String relativeName = convertToRelativeName(dn, baseDN);
+                sr = new SearchResult(relativeName, null, entries.get(dn));
+                sr.setNameInNamespace(dn);
+            }
             list.add(sr);
         }
 
+        if (list.size() == 0 && result.getException() != null) {
+            throw result.getException();
+        }
+
         return new LdapNamingEnumeration<SearchResult>(list, result
                 .getException());
     }
@@ -1223,7 +1308,7 @@
 
         // get absolute dn name
         String targetDN = getTargetDN(name, contextDn);
-        
+
         Filter f = LdapUtils.parseFilter(filter, objs);
 
         LdapSearchResult result = doSearch(targetDN, f, searchControls);
@@ -1234,13 +1319,24 @@
         tempName.addAll(name);
         String baseDN = tempName.toString();
         for (String dn : entries.keySet()) {
-            String relativeName = convertToRelativeName(dn, baseDN);
-            SearchResult sr = new SearchResult(relativeName, null, entries
-                    .get(dn));
-            sr.setNameInNamespace(dn);
+            SearchResult sr = null;
+            if (dn.startsWith("ldap://")) {
+                sr = new SearchResult(dn, null, entries.get(dn), false);
+                int index = dn.indexOf("/", 7);
+                sr.setNameInNamespace(dn.substring(index + 1, dn.length()));
+                list.add(sr);
+            } else {
+                String relativeName = convertToRelativeName(dn, baseDN);
+                sr = new SearchResult(relativeName, null, entries.get(dn));
+                sr.setNameInNamespace(dn);
+            }
             list.add(sr);
         }
 
+        if (list.size() == 0 && result.getException() != null) {
+            throw result.getException();
+        }
+
         return new LdapNamingEnumeration<SearchResult>(list, result
                 .getException());
     }
@@ -1331,26 +1427,101 @@
                 LdapUtils.getExceptionFromResult(result));
 
         // has error, not deal with referrals
-        if (op.getSearchResult().getException() != null) {
+        if (result.getResultCode() != LdapResult.REFERRAL
+                && op.getSearchResult().getException() != null) {
             return op.getSearchResult();
         }
 
         // baseObject is not located at the server
-        if (result.getResultCode() == 10) {
-            // TODO deal with referrals
-            throw new NotYetImplementedException();
+        if (result.getResultCode() == LdapResult.REFERRAL) {
+            ReferralException ex = new ReferralExceptionImpl(contextDn
+                    .toString(), result.getReferrals(), env);
+            try {
+                if (isFollowReferral(ex)) {
+                    LdapContextImpl ctx = (LdapContextImpl) getReferralContext(ex);
+                    return ctx.doSearch(op);
+                } else {
+                    op.getSearchResult().setException(ex);
+                    return op.getSearchResult();
+                }
+            } catch (PartialResultException e) {
+                op.getSearchResult().setException(e);
+                return op.getSearchResult();
+            }
         }
 
         // there are SearchResultReference in search result
         if (op.getSearchResult().getRefURLs() != null
                 && op.getSearchResult().getRefURLs().size() != 0) {
-            // TODO deal with referrals
-            throw new NotYetImplementedException();
+            ReferralException ex = new ReferralExceptionImpl(contextDn
+                    .toString(), op.getSearchResult().getRefURLs().toArray(
+                    new String[0]), env);
+            try {
+                if (isFollowReferral(ex)) {
+                    processSearchRef(op, ex);
+                } else {
+                    op.getSearchResult().setException(ex);
+                    return op.getSearchResult();
+                }
+            } catch (PartialResultException e) {
+                op.getSearchResult().setException(e);
+                return op.getSearchResult();
+            }
         }
 
         return op.getSearchResult();
     }
 
+    /**
+     * Follow referrals in SearchResultReference. Referrals in
+     * SearchResultReference is different with LDAPResult, which may contians
+     * filter parts. Filter and dn part of url will overwrite filter and
+     * baseObject of last search operation.
+     * 
+     * @param op
+     *            last search operation
+     * @param ex
+     */
+    private void processSearchRef(SearchOp op, ReferralException ex) {
+        LdapSearchResult result = op.getSearchResult();
+        List<String> urls = result.getRefURLs();
+
+        // clean referrals
+        result.setRefURLs(null);
+
+        try {
+            for (String url : urls) {
+
+                LdapUrlParser urlParser = LdapUtils.parserURL(url, true);
+                // if url has dn part overwrite baseObject of last search
+                // operation
+                if (!urlParser.getBaseObject().equals("")) {
+                    op.setBaseObject(urlParser.getBaseObject());
+                }
+                // if url has filter part overwrite filter of last search
+                // operation
+                if (urlParser.hasFilter()) {
+                    op.setFilter(urlParser.getFilter());
+                }
+                LdapContextImpl ctx = (LdapContextImpl) getReferralContext(ex);
+                result.setAddress("ldap://" + urlParser.getHost() + ":"
+                        + urlParser.getPort() + "/");
+                ctx.doSearch(op);
+                result.setAddress(null);
+            }
+        } catch (NamingException e) {
+            /*
+             * occrus exception, set to search result and return, not continue
+             * to follow referral
+             * 
+             * TODO test the behavior of ri
+             * 
+             */
+            result.setException(e);
+            return;
+        }
+    }
+
     LdapSearchResult doSearch(String dn, Filter filter, SearchControls controls)
             throws NamingException {
         SearchOp op = new SearchOp(dn, controls, filter);
@@ -1475,6 +1646,13 @@
         DeleteOp op = new DeleteOp(targetDN);
         try {
             doBasicOperation(op);
+        } catch (ReferralException e) {
+            if (isFollowReferral(e)) {
+                DirContext referralContext = getReferralContext(e);
+                referralContext.destroySubcontext(name);
+                return;
+            }
+            throw e;
         } catch (NameNotFoundException e) {
             // target dn doesn't exist, do nothing
         }
@@ -1565,6 +1743,11 @@
             list.add(pair);
         }
 
+        // no entries return
+        if (list.size() == 0 && result.getException() != null) {
+            throw result.getException();
+        }
+
         return new LdapNamingEnumeration<NameClassPair>(list, result
                 .getException());
     }
@@ -1960,7 +2143,16 @@
         ModifyDNOp op = new ModifyDNOp(oldTargetDN, rdn.toString(),
                 isDeleteRdn, name.getPrefix(name.size() - 1).toString());
 
-        doBasicOperation(op);
+        try {
+            doBasicOperation(op);
+        } catch (ReferralException e) {
+            if (isFollowReferral(e)) {
+                DirContext referralContext = getReferralContext(e);
+                referralContext.rename(nOld, nNew);
+                return;
+            }
+            throw e;
+        }
     }
 
     private boolean isInSameNamespace(Name first, Name second) {
@@ -2014,12 +2206,37 @@
         responseControls = narrowingControls(rawControls);
 
         LdapResult result = op.getResult();
+        if (result.getResultCode() == LdapResult.REFERRAL) {
+            throw new ReferralExceptionImpl(contextDn.toString(), result
+                    .getReferrals(), env);
+        }
 
-        // TODO deal with referrals
+        if (LdapUtils.getExceptionFromResult(result) != null) {
+            throw LdapUtils.getExceptionFromResult(result);
+        }
+    }
 
-        NamingException ex = LdapUtils.getExceptionFromResult(result);
-        if (ex != null) {
-            throw ex;
+    private boolean isFollowReferral(ReferralException e)
+            throws ReferralException, PartialResultException {
+        // ignore referral
+        String action = (String) env.get(Context.REFERRAL);
+        if (action == null) {
+            action = "ignore";
+        }
+
+        if ("follow".equals(action)) {
+            return true;
+        } else if ("throw".equals(action)) {
+            return false;
+
+        } else if ("ignore".equals(action)) {
+            // ldap.1A=[LDAP: error code 10 - Referral]
+            throw new PartialResultException(Messages.getString("ldap.1A"));
+
+        } else {
+            throw new IllegalArgumentException(Messages.getString(
+                    "ldap.30", new Object[] { //$NON-NLS-1$
+                    env.get(Context.REFERRAL), Context.REFERRAL }));
         }
     }
 

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSearchResult.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSearchResult.java?rev=599715&r1=599714&r2=599715&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSearchResult.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSearchResult.java
Thu Nov 29 23:41:16 2007
@@ -40,14 +40,23 @@
 
     /**
      * SearchResultReference from server
-     * TODO: deal with the references
      */
-    private List<String> refURLs = new ArrayList<String>();
+    private List<String> refURLs;
 
     private LdapResult result;
 
     private NamingException ex;
 
+    private String address;
+
+    public String getAddress() {
+		return address;
+	}
+
+	public void setAddress(String address) {
+		this.address = address;
+	}
+
     public void decodeSearchResponse(Object[] values) {
         ChosenValue chosen = (ChosenValue) values[0];
         switch (chosen.getIndex()) {
@@ -69,6 +78,10 @@
     }
 
     protected void decodeRef(Object value) {
+        if (refURLs == null) {
+            refURLs = new ArrayList<String>();
+        }
+
         Collection<byte[]> list = (Collection<byte[]>) value;
         for (byte[] bs : list) {
             refURLs.add(Utils.getString(bs));
@@ -78,6 +91,10 @@
     protected void decodeEntry(Object value) {
         Object[] values = (Object[]) value;
         String name = Utils.getString((byte[]) values[0]);
+
+        if (address != null) {
+        	name = address + name;
+        }
 
         Attributes attrs = null;
 

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=599715&r1=599714&r2=599715&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
Thu Nov 29 23:41:16 2007
@@ -20,6 +20,9 @@
 import java.util.Hashtable;
 
 import javax.naming.Context;
+import javax.naming.PartialResultException;
+import javax.naming.ReferralException;
+import javax.naming.directory.DirContext;
 import javax.naming.directory.InitialDirContext;
 import javax.naming.ldap.Control;
 import javax.naming.ldap.InitialLdapContext;
@@ -45,6 +48,9 @@
         env.put(Context.INITIAL_CONTEXT_FACTORY,
                 "org.apache.harmony.jndi.provider.ldap.LdapContextFactory");
         env.put(Context.PROVIDER_URL, server.getURL());
+        env.put(Context.SECURITY_AUTHENTICATION, "simple");
+        env.put(Context.SECURITY_PRINCIPAL, "");
+        env.put(Context.SECURITY_CREDENTIALS, "");
     }
 
     @Override
@@ -228,4 +234,97 @@
         }
 
     }
+
+    public void testRerralIgnore() throws Exception {
+        env.put(Context.REFERRAL, "ignore");
+
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
+        DirContext context = new InitialDirContext(env);
+
+        EncodableLdapResult result = new EncodableLdapResult();
+        result = new EncodableLdapResult(LdapResult.REFERRAL, "", "",
+                new String[] { "ldap://localhost" });
+
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_SEARCH_RESULT_DONE, result, null) });
+        try {
+            context.getAttributes("cn=test");
+            fail("Should throw PartialResultException");
+        } catch (PartialResultException e) {
+            // expected
+        }
+
+        result = new EncodableLdapResult(LdapResult.REFERRAL, null, null, null);
+
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_DEL_RESPONSE, result, null) });
+        try {
+            context.destroySubcontext("cn=test");
+            fail("Should throw PartialResultException");
+        } catch (PartialResultException e) {
+            // expected
+        }
+    }
+
+    public void testReferralThrow() throws Exception {
+        env.put(Context.REFERRAL, "throw");
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
+        DirContext context = new InitialDirContext(env);
+
+        EncodableLdapResult result = new EncodableLdapResult(
+                LdapResult.REFERRAL, "", "",
+                new String[] { "ldap://localhost" });
+
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_SEARCH_RESULT_DONE, result, null) });
+        try {
+            context.getAttributes("cn=test");
+            fail("Should throw ReferralException");
+        } catch (ReferralException e) {
+            assertEquals("ldap://localhost", e.getReferralInfo());
+        }
+
+        result = new EncodableLdapResult(LdapResult.REFERRAL, "", "",
+                new String[] { "ldap://localhost" });
+
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_DEL_RESPONSE, result, null) });
+
+        try {
+            context.destroySubcontext("");
+            fail("Should throw ReferralException");
+        } catch (ReferralException e) {
+            assertEquals("ldap://localhost", e.getReferralInfo());
+        }
+    }
+
+    public void testReferralFollow() throws Exception {
+        env.put(Context.REFERRAL, "follow");
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
+        DirContext context = new InitialDirContext(env);
+
+        MockLdapServer referralServer = new MockLdapServer();
+        referralServer.start();
+
+        EncodableLdapResult result = new EncodableLdapResult(
+                LdapResult.REFERRAL, "", "", new String[] { referralServer
+                        .getURL() });
+
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_SEARCH_RESULT_DONE, result, null) });
+
+        referralServer.setResponseSeq(new LdapMessage[] {
+                new LdapMessage(LdapASN1Constant.OP_BIND_RESPONSE,
+                        new BindResponse(), null),
+                new LdapMessage(LdapASN1Constant.OP_SEARCH_RESULT_DONE,
+                        new EncodableLdapResult(), null) });
+
+        context.getAttributes("cn=test");
+
+        referralServer.stop();
+    }
+
 }

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=599715&r1=599714&r2=599715&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
Thu Nov 29 23:41:16 2007
@@ -22,6 +22,7 @@
 import java.io.OutputStream;
 import java.net.ServerSocket;
 import java.net.Socket;
+import java.util.LinkedList;
 
 import org.apache.harmony.jndi.provider.ldap.LdapMessage;
 import org.apache.harmony.security.asn1.ASN1Integer;
@@ -40,7 +41,7 @@
 
     private Socket socket;
 
-    private LdapMessage[] responses;
+	private LinkedList<LdapMessage> responses = new LinkedList<LdapMessage>();
 
     private int port;
 
@@ -93,8 +94,12 @@
         return port;
     }
 
-    public void setResponseSeq(LdapMessage[] msges) {
-        this.responses = msges;
+	public void setResponseSeq(LdapMessage[] msges) {
+		synchronized (responses) {
+			for (LdapMessage message : msges) {
+				responses.addLast(message);
+			}
+		}
 
         synchronized (lock) {
             lock.notify();
@@ -109,7 +114,7 @@
             in = socket.getInputStream();
             out = socket.getOutputStream();
             while (!isStopped) {
-                if (responses == null) {
+				if (responses.size() == 0) {
                     try {
                         synchronized (lock) {
                             lock.wait();
@@ -119,9 +124,17 @@
                     }
                 } else {
 
-                    for (int i = 0; i < responses.length; i++) {
+					while (true) {
+						LdapMessage temp = null;
+						synchronized (responses) {
+							if (responses.size() == 0) {
+								break;
+							}
+							temp = responses.removeFirst();
+						}
+						
                         final MockLdapMessage response = new MockLdapMessage(
-                                responses[i]);
+								temp);
                         LdapMessage request = new LdapMessage(null) {
                             public void decodeValues(Object[] values) {
                                 response.setMessageId(ASN1Integer
@@ -136,7 +149,6 @@
                         }
                         out.write(response.encode());
                     }
-                    responses = null;
                 }
             }
         } catch (IOException e) {



Mime
View raw message