harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lvj...@apache.org
Subject svn commit: r595238 - 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/
Date Thu, 15 Nov 2007 08:48:46 GMT
Author: lvjing
Date: Thu Nov 15 00:48:45 2007
New Revision: 595238

URL: http://svn.apache.org/viewvc?rev=595238&view=rev
Log:
Apply for Harmony-5125.

Added:
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapUtilsTest.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/LdapContextImpl.java
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapUtils.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=595238&r1=595237&r2=595238&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 15 00:48:45 2007
@@ -199,6 +199,8 @@
 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.2B=LDAP URL should not be null
 ldap.2C=Invalid LDAP URL
 ldap.2D=LDAP URL may only contain host, port and dn components

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=595238&r1=595237&r2=595238&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 15 00:48:45 2007
@@ -737,7 +737,7 @@
         LdapResult result = op.getResult();
 
         op.getSearchResult().setException(
-                Util.getExceptionFromErrorCode(result.getResultCode()));
+                LdapUtils.getExceptionFromResult(result));
 
         // has error, not deal with referrals
         if (op.getSearchResult().getException() != null) {
@@ -1220,8 +1220,9 @@
 
         // TODO deal with referrals
 
-        if (Util.getExceptionFromErrorCode(result.getResultCode()) != null) {
-            throw Util.getExceptionFromErrorCode(result.getResultCode());
+        NamingException ex = LdapUtils.getExceptionFromResult(result);
+        if (ex != null) {
+            throw ex;
         }
     }
 

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapUtils.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapUtils.java?rev=595238&r1=595237&r2=595238&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapUtils.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapUtils.java
Thu Nov 15 00:48:45 2007
@@ -17,15 +17,77 @@
 
 package org.apache.harmony.jndi.provider.ldap;
 
+import java.lang.reflect.Constructor;
+import java.util.HashMap;
+
+import javax.naming.AuthenticationException;
+import javax.naming.AuthenticationNotSupportedException;
+import javax.naming.CommunicationException;
+import javax.naming.ContextNotEmptyException;
 import javax.naming.InvalidNameException;
+import javax.naming.LimitExceededException;
+import javax.naming.NameAlreadyBoundException;
+import javax.naming.NameNotFoundException;
+import javax.naming.NamingException;
+import javax.naming.NoPermissionException;
+import javax.naming.OperationNotSupportedException;
+import javax.naming.PartialResultException;
+import javax.naming.ServiceUnavailableException;
+import javax.naming.SizeLimitExceededException;
+import javax.naming.TimeLimitExceededException;
+import javax.naming.directory.AttributeInUseException;
+import javax.naming.directory.InvalidAttributeIdentifierException;
+import javax.naming.directory.InvalidAttributeValueException;
 import javax.naming.directory.InvalidSearchFilterException;
+import javax.naming.directory.NoSuchAttributeException;
+import javax.naming.directory.SchemaViolationException;
 
 import org.apache.harmony.jndi.internal.nls.Messages;
 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;
 
+@SuppressWarnings("boxing")
 public class LdapUtils {
+    private static HashMap<Integer, Class<?>> errorCodesMap = new HashMap<Integer,
Class<?>>();
+
+    static {
+        errorCodesMap.put(1, NamingException.class);
+        errorCodesMap.put(2, CommunicationException.class);
+        errorCodesMap.put(3, TimeLimitExceededException.class);
+        errorCodesMap.put(4, SizeLimitExceededException.class);
+        errorCodesMap.put(7, AuthenticationNotSupportedException.class);
+        errorCodesMap.put(8, AuthenticationNotSupportedException.class);
+        errorCodesMap.put(9, PartialResultException.class);
+        errorCodesMap.put(11, LimitExceededException.class);
+        errorCodesMap.put(12, OperationNotSupportedException.class);
+        errorCodesMap.put(13, AuthenticationNotSupportedException.class);
+        errorCodesMap.put(16, NoSuchAttributeException.class);
+        errorCodesMap.put(17, InvalidAttributeIdentifierException.class);
+        errorCodesMap.put(18, InvalidSearchFilterException.class);
+        errorCodesMap.put(19, InvalidAttributeValueException.class);
+        errorCodesMap.put(20, AttributeInUseException.class);
+        errorCodesMap.put(21, InvalidAttributeValueException.class);
+        errorCodesMap.put(32, NameNotFoundException.class);
+        errorCodesMap.put(33, NamingException.class);
+        errorCodesMap.put(34, InvalidNameException.class);
+        errorCodesMap.put(36, NamingException.class);
+        errorCodesMap.put(48, AuthenticationNotSupportedException.class);
+        errorCodesMap.put(49, AuthenticationException.class);
+        errorCodesMap.put(50, NoPermissionException.class);
+        errorCodesMap.put(51, ServiceUnavailableException.class);
+        errorCodesMap.put(52, ServiceUnavailableException.class);
+        errorCodesMap.put(53, OperationNotSupportedException.class);
+        errorCodesMap.put(54, NamingException.class);
+        errorCodesMap.put(64, InvalidNameException.class);
+        errorCodesMap.put(65, SchemaViolationException.class);
+        errorCodesMap.put(66, ContextNotEmptyException.class);
+        errorCodesMap.put(67, SchemaViolationException.class);
+        errorCodesMap.put(68, NameAlreadyBoundException.class);
+        errorCodesMap.put(69, SchemaViolationException.class);
+        errorCodesMap.put(71, NamingException.class);
+        errorCodesMap.put(80, NamingException.class);
+    }
 
     public static Filter parseFilter(String filter)
             throws InvalidSearchFilterException {
@@ -71,5 +133,43 @@
         }
 
         return parser;
+    }
+
+    public static NamingException getExceptionFromResult(LdapResult result) {
+        int errorCode = result.getResultCode();
+        // 0 means successful
+        if (errorCode == 0) {
+            return null;
+        }
+
+        Class<?> exceptionClass = errorCodesMap.get(errorCode);
+        // not in map, using NamingException
+        if (exceptionClass == null) {
+            exceptionClass = NamingException.class;
+        }
+
+        try {
+            Constructor<?> constructor = exceptionClass
+                    .getConstructor(new Class[] { String.class });
+            String message = null;
+
+            if (result.getErrorMessage() != null
+                    && !result.getErrorMessage().equals("")) { //$NON-NLS-1$
+                // ldap.34=[LDAP: error code {0} - {1}]
+                message = Messages.getString("ldap.34", new Object[] { //$NON-NLS-1$
+                        errorCode, result.getErrorMessage() });
+            } else {
+                // ldap.35=[LDAP: error code {0}]
+                message = Messages.getString("ldap.35", //$NON-NLS-1$
+                        new Object[] { errorCode });
+            }
+
+            return (NamingException) constructor
+                    .newInstance(new Object[] { message });
+        } catch (Exception e) {
+            // ldap.35=[LDAP: error code {0}]
+            return new NamingException(Messages.getString("ldap.35", //$NON-NLS-1$
+                    new Object[] { errorCode }));
+        }
     }
 }

Added: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapUtilsTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapUtilsTest.java?rev=595238&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapUtilsTest.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapUtilsTest.java
Thu Nov 15 00:48:45 2007
@@ -0,0 +1,48 @@
+package org.apache.harmony.jndi.provider.ldap;
+
+import javax.naming.CommunicationException;
+import javax.naming.NamingException;
+import javax.naming.TimeLimitExceededException;
+
+import junit.framework.TestCase;
+
+import org.apache.harmony.jndi.provider.ldap.asn1.Utils;
+import org.apache.harmony.security.asn1.ASN1Integer;
+
+public class LdapUtilsTest extends TestCase {
+    public void test_getExceptionFromResult() {
+        String message = "error message";
+        LdapResult result = getLdapResult(0, message);
+        NamingException ex = LdapUtils.getExceptionFromResult(result);
+        assertNull(ex);
+
+        // error code map to CommunicationException
+        result = getLdapResult(2, message);
+        ex = LdapUtils.getExceptionFromResult(result);
+        assertTrue(ex instanceof CommunicationException);
+        assertEquals("[LDAP: error code 2 - error message]", ex.getMessage());
+
+        // error code not in map
+        result = getLdapResult(100, message);
+        ex = LdapUtils.getExceptionFromResult(result);
+        assertTrue(ex instanceof NamingException);
+        assertEquals("[LDAP: error code 100 - error message]", ex.getMessage());
+
+        // empty error message
+        result = getLdapResult(3, "");
+        ex = LdapUtils.getExceptionFromResult(result);
+        assertTrue(ex instanceof TimeLimitExceededException);
+        assertEquals("[LDAP: error code 3]", ex.getMessage());
+    }
+
+    private LdapResult getLdapResult(int errorCode, String message) {
+        LdapResult result = new LdapResult();
+        if (message == null) {
+
+        }
+        Object[] values = new Object[] { ASN1Integer.fromIntValue(errorCode),
+                Utils.getBytes(""), Utils.getBytes(message), null };
+        result.decodeValues(values);
+        return result;
+    }
+}

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



Mime
View raw message