harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ton...@apache.org
Subject svn commit: r659433 - in /harmony/enhanced/classlib/trunk/modules/jndi/src: main/java/org/apache/harmony/jndi/provider/ldap/ test/java/org/apache/harmony/jndi/provider/ldap/
Date Fri, 23 May 2008 05:38:06 GMT
Author: tonywu
Date: Thu May 22 22:38:03 2008
New Revision: 659433

URL: http://svn.apache.org/viewvc?rev=659433&view=rev
Log:
Apply patch for HARMONY-5838 ([classlib][jndi][ldap] - Refactor the ldap schema code)

Modified:
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextImpl.java
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaContextImpl.java
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaContextTest.java

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextImpl.java?rev=659433&r1=659432&r2=659433&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 May 22 22:38:03 2008
@@ -763,8 +763,6 @@
         return getAttributes(convertFromStringToName(s), as);
     }
 
-    public static Hashtable<String, Hashtable<String, Hashtable<String, Object>>>
schemaTree = new Hashtable<String, Hashtable<String, Hashtable<String, Object>>>();
-
     private Hashtable<String, Object> schemaTable = new Hashtable<String, Object>();
 
     private LdapSchemaContextImpl ldapSchemaCtx = null;
@@ -877,15 +875,10 @@
 
             while (ids.hasMoreElements()) {
                 String schemaType = ids.nextElement();
-                if (!schemaTree.contains(schemaType.toLowerCase())) {
-                    schemaTree.put(schemaType.toLowerCase(),
-                            new Hashtable<String, Hashtable<String, Object>>());
-
+                if (!schemaTable.contains(schemaType.toLowerCase())) {
                     schemaTable.put(schemaType.toLowerCase(),
                             new Hashtable<String, String>());
                 }
-                Hashtable<String, Hashtable<String, Object>> schemaDefs = schemaTree
-                        .get(schemaType.toLowerCase());
 
                 Hashtable<String, String> schemaDef = (Hashtable<String, String>)
schemaTable
                         .get(schemaType.toLowerCase());
@@ -897,10 +890,7 @@
                     value = (String) attribute.get(i);
                     attrName = SchemaParser.getName(value);
                     schemaDef.put(attrName.toLowerCase(), value);
-
-                    parseValue(schemaType, value.toLowerCase(), schemaDefs);
                 }
-
             }
         }
 
@@ -909,144 +899,6 @@
         return ldapSchemaCtx;
     }
 
-    Hashtable<String, Object> findSchemaDefInfo(String schemaType,
-            String className) {
-        Hashtable<String, Hashtable<String, Object>> schemaDefs = schemaTree
-                .get(schemaType);
-        Hashtable<String, Object> schemaDef = schemaDefs.get(className);
-        return schemaDef;
-    }
-
-    /*
-     * Sample schema value from Openldap server is ( 2.5.13.8 NAME
-     * 'numericStringMatch' SYNTAX 1.3.6.1.4.1.1466.115.121.1.36 )
-     * 
-     * TODO check with RFC to see whether all the schema definition has been
-     * catered for
-     */
-    private static void parseValue(String schemaType, String value,
-            Hashtable<String, Hashtable<String, Object>> schemaDefs) {
-        StringTokenizer st = new StringTokenizer(value.toLowerCase());
-        // Skip (
-        st.nextToken();
-
-        String oid = st.nextToken();
-
-        Hashtable<String, Object> schemaDef = new Hashtable<String, Object>();
-        schemaDef.put("orig", value);
-        schemaDef.put("numericoid", oid);
-        String token = null;
-        ArrayList<String> values = null;
-        StringBuilder desc = new StringBuilder();
-        while (st.hasMoreTokens()) {
-            String attrName = st.nextToken();
-            if (attrName.startsWith("x-")) {
-                token = st.nextToken();
-                // remove the ending ' symbol
-                token = token.substring(1, token.length() - 1);
-                schemaDef.put(attrName, token);
-            }
-            if (attrName.startsWith("auxiliary")) {
-                schemaDef.put(attrName, "true");
-            }
-            if (attrName.startsWith("structural")) {
-                schemaDef.put(attrName, "true");
-            }
-            if (attrName.equals("usage") || attrName.equals("equality")
-                    || attrName.equals("syntax") || attrName.equals("ordering")
-                    || attrName.equals("substr")) {
-                token = st.nextToken();
-                schemaDef.put(attrName, token);
-            }
-            if (attrName.equals("desc")) {
-                token = st.nextToken();
-
-                // remove the leading ' symbol
-                if (token.startsWith("'"))
-                    token = token.substring(1);
-                while (!token.endsWith("'")) {
-                    desc.append(token).append(" ");
-                    token = st.nextToken();
-                }
-
-                // remove the ending ' symbol
-                desc.append(token.substring(0, token.length() - 1));
-                schemaDef.put(attrName, desc.toString());
-                desc.delete(0, desc.length());
-            }
-            if (attrName.equals("name")) {
-                token = st.nextToken();
-                values = new ArrayList<String>();
-                // Name has multiple values
-                if (token.startsWith("(")) {
-                    token = st.nextToken();
-                    while (!token.equals(")")) {
-                        // remove the leading ' symbol
-                        if (token.startsWith("'"))
-                            token = token.substring(1);
-                        while (!token.endsWith("'")) {
-                            desc.append(token).append(" ");
-                            token = st.nextToken();
-                        }
-
-                        // remove the ending ' symbol
-                        desc.append(token.substring(0, token.length() - 1));
-                        values.add(desc.toString());
-                        desc.delete(0, desc.length());
-
-                        token = st.nextToken();
-                    }
-                } else {
-                    // remove the leading ' symbol
-                    if (token.startsWith("'"))
-                        token = token.substring(1);
-                    while (!token.endsWith("'")) {
-                        desc.append(token).append(" ");
-                        token = st.nextToken();
-                    }
-
-                    // remove the ending ' symbol
-                    desc.append(token.substring(0, token.length() - 1));
-                    values.add(desc.toString());
-                    desc.delete(0, desc.length());
-                }
-                schemaDef.put(attrName, values);
-                if (schemaType
-                        .equalsIgnoreCase(LdapSchemaContextImpl.LDAP_SYNTAXES)) {
-                    schemaDefs.put(oid, schemaDef);
-                } else {
-                    schemaDefs.put(values.get(0), schemaDef);
-                }
-            }
-            if (attrName.equals("must") || attrName.equals("sup")
-                    || attrName.equals("may")) {
-                token = st.nextToken();
-                values = new ArrayList<String>();
-                // has multiple values
-                if (token.startsWith("(")) {
-                    token = st.nextToken();
-                    while (!token.equals(")")) {
-                        if (!token.equals("$"))
-                            values.add(token);
-                        token = st.nextToken();
-                    }
-                } else {
-                    values.add(token);
-                }
-                schemaDef.put(attrName, values);
-            }
-            if (attrName.equals("abstract") || attrName.equals("structual")
-                    || attrName.equals("auxiliary")
-                    || attrName.equals("single-value")
-                    || attrName.equals("no-user-modification")) {
-                schemaDef.put(attrName, "true");
-            }
-        }
-        if (!schemaDef.keySet().contains("name")) {
-            schemaDefs.put(oid, schemaDef);
-        }
-    }
-
     public DirContext getSchema(String s) throws NamingException {
         return getSchema(new CompositeName(s));
     }

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaContextImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaContextImpl.java?rev=659433&r1=659432&r2=659433&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaContextImpl.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaContextImpl.java
Thu May 22 22:38:03 2008
@@ -69,8 +69,6 @@
 
     public static final int DEFINITION_LEVEL = 2;
 
-    protected String subschemasubentry = null;
-
     final private static Hashtable<String, String> schemaJndi2Ldap = new Hashtable<String,
String>();
     static {
         schemaJndi2Ldap.put(CLASS_DEFINITION.toLowerCase(), OBJECT_CLASSES);
@@ -88,7 +86,7 @@
         schemaLdap2Jndi.put(MATCHING_RULES, MATCHING_RULE);
     }
 
-    private LdapContextImpl parent;
+    private LdapContextImpl ldapContext;
 
     private Hashtable<String, Object> schemaTable;
 
@@ -101,7 +99,7 @@
             Hashtable<String, Object> schemaTable, int level)
             throws InvalidNameException {
         super(ctx, env, dn.getPrefix(0).toString());
-        parent = ctx;
+        ldapContext = ctx;
         rdn = dn;
         this.schemaTable = schemaTable;
         this.level = level;
@@ -159,12 +157,12 @@
 
         String schemaLine = SchemaParser.format(attributes);
 
-        ModifyOp op = new ModifyOp(parent.subschemasubentry);
+        ModifyOp op = new ModifyOp(ldapContext.subschemasubentry);
         Name modifySchemaName = name.getPrefix(size - 1).addAll(rdn);
         BasicAttribute schemaEntry = new LdapAttribute(new BasicAttribute(
-                jndi2ldap(modifySchemaName.toString()), schemaLine), parent);
-        op.addModification(jndi2ldap[DirContext.ADD_ATTRIBUTE],
-                new LdapAttribute(schemaEntry, parent));
+                jndi2ldap(modifySchemaName.toString()), schemaLine), ldapContext);
+        op.addModification(OperationJndi2Ldap[DirContext.ADD_ATTRIBUTE],
+                new LdapAttribute(schemaEntry, ldapContext));
         try {
             doBasicOperation(op);
             subSchemaTree.put(subSchemaType.toLowerCase(), schemaLine);
@@ -173,14 +171,12 @@
         }
 
         return (DirContext) lookup(name);
-
     }
 
     @Override
     public DirContext createSubcontext(String name, Attributes attributes)
             throws NamingException {
-        Name n = convertFromStringToName(name);
-        return createSubcontext(n, attributes);
+        return createSubcontext(new CompositeName(name), attributes);
     }
 
     @Override
@@ -244,10 +240,8 @@
     }
 
     @Override
-    public Attributes getAttributes(String attributeName)
-            throws NamingException {
-        Name name = new CompositeName(attributeName);
-        return getAttributes(name);
+    public Attributes getAttributes(String name) throws NamingException {
+        return getAttributes(new CompositeName(name));
     }
 
     @Override
@@ -256,17 +250,14 @@
         return getAttributes(new CompositeName(name), as);
     }
 
-    private void checkName(Name name) {
+    @Override
+    public void modifyAttributes(Name name, int i, Attributes attributes)
+            throws NamingException {
         if (name == null) {
             // jndi.2E=The name is null
             throw new NullPointerException(Messages.getString("jndi.2E")); //$NON-NLS-1$
         }
-    }
-
-    @Override
-    public void modifyAttributes(Name name, int i, Attributes attributes)
-            throws NamingException {
-        checkName(name);
+        
         if (attributes == null) {
             // jndi.13=Non-null attribute is required for modification
             throw new NullPointerException(Messages.getString("jndi.13")); //$NON-NLS-1$
@@ -292,7 +283,8 @@
         modifyAttributes(name, items);
     }
 
-    private static final int jndi2ldap[] = { -1, 0, 2, 1, };
+    // Mapping from DirContext's attribute operation code to server's operation code. 
+    private static final int OperationJndi2Ldap[] = { -1, 0, 2, 1, };
 
     @Override
     public void modifyAttributes(Name name, ModificationItem[] modificationItems)
@@ -321,7 +313,7 @@
 
         String schemaLine = schema.toString();
         if (schema instanceof Hashtable) {
-            Hashtable table = (Hashtable) schema;
+            Hashtable<String, Object> table = (Hashtable<String, Object>) schema;
             schemaLine = table.get(SchemaParser.ORIG).toString();
         }
 
@@ -371,16 +363,16 @@
         String newSchemaLine = SchemaParser.format(attributes);
 
         // Remove old schema, then add new schema.
-        ModifyOp op = new ModifyOp(parent.subschemasubentry);
+        ModifyOp op = new ModifyOp(ldapContext.subschemasubentry);
         Name modifySchemaName = name.getPrefix(size - 1).addAll(rdn);
         BasicAttribute schemaEntry = new LdapAttribute(new BasicAttribute(
-                jndi2ldap(modifySchemaName.toString()), schemaLine), parent);
-        op.addModification(jndi2ldap[DirContext.REMOVE_ATTRIBUTE],
-                new LdapAttribute(schemaEntry, parent));
+                jndi2ldap(modifySchemaName.toString()), schemaLine), ldapContext);
+        op.addModification(OperationJndi2Ldap[DirContext.REMOVE_ATTRIBUTE],
+                new LdapAttribute(schemaEntry, ldapContext));
         BasicAttribute addSchemaEntry = new LdapAttribute(new BasicAttribute(
-                jndi2ldap(modifySchemaName.toString()), newSchemaLine), parent);
-        op.addModification(jndi2ldap[DirContext.ADD_ATTRIBUTE],
-                new LdapAttribute(addSchemaEntry, parent));
+                jndi2ldap(modifySchemaName.toString()), newSchemaLine), ldapContext);
+        op.addModification(OperationJndi2Ldap[DirContext.ADD_ATTRIBUTE],
+                new LdapAttribute(addSchemaEntry, ldapContext));
 
         doBasicOperation(op);
         subSchemaTree.remove(subSchemaType);
@@ -388,29 +380,25 @@
     }
 
     @Override
-    public void modifyAttributes(String s, int i, Attributes attributes)
+    public void modifyAttributes(String name, int i, Attributes attributes)
             throws NamingException {
-        Name name = convertFromStringToName(s);
-        modifyAttributes(name, i, attributes);
+        modifyAttributes(new CompositeName(name), i, attributes);
     }
 
     @Override
-    public void modifyAttributes(String s, ModificationItem[] modificationItems)
-            throws NamingException {
-        Name name = convertFromStringToName(s);
-        modifyAttributes(name, modificationItems);
+    public void modifyAttributes(String name,
+            ModificationItem[] modificationItems) throws NamingException {
+        modifyAttributes(new CompositeName(name), modificationItems);
     }
 
     @Override
     public Context createSubcontext(Name name) throws NamingException {
-        DirContext subContext = createSubcontext(name, null);
-        return subContext;
+        return createSubcontext(name, null);
     }
 
     @Override
     public Context createSubcontext(String name) throws NamingException {
-        Name n = convertFromStringToName(name);
-        return createSubcontext(n);
+        return createSubcontext(new CompositeName(name));
     }
 
     @Override
@@ -440,16 +428,16 @@
 
         String schemaLine = schema.toString();
         if (schema instanceof Hashtable) {
-            Hashtable table = (Hashtable) schema;
+            Hashtable<String, Object> table = (Hashtable<String, Object>) schema;
             schemaLine = table.get(SchemaParser.ORIG).toString();
         }
 
-        ModifyOp op = new ModifyOp(parent.subschemasubentry);
+        ModifyOp op = new ModifyOp(ldapContext.subschemasubentry);
         Name modifySchemaName = name.getPrefix(size - 1).addAll(rdn);
         BasicAttribute schemaEntry = new LdapAttribute(new BasicAttribute(
-                jndi2ldap(modifySchemaName.toString()), schemaLine), parent);
-        op.addModification(jndi2ldap[DirContext.REMOVE_ATTRIBUTE],
-                new LdapAttribute(schemaEntry, parent));
+                jndi2ldap(modifySchemaName.toString()), schemaLine), ldapContext);
+        op.addModification(OperationJndi2Ldap[DirContext.REMOVE_ATTRIBUTE],
+                new LdapAttribute(schemaEntry, ldapContext));
         try {
             doBasicOperation(op);
             subSchemaTree.remove(subSchemaType);
@@ -460,7 +448,7 @@
 
     @Override
     public void destroySubcontext(String name) throws NamingException {
-        destroySubcontext(convertFromStringToName(name));
+        destroySubcontext(new CompositeName(name));
     }
 
     @Override
@@ -490,11 +478,6 @@
     @Override
     protected Name convertFromStringToName(String s)
             throws InvalidNameException {
-        if (s == null) {
-            // jndi.2E=The name is null
-            throw new NullPointerException(Messages.getString("jndi.2E")); //$NON-NLS-1$
-        }
-
         CompositeName name = new CompositeName(s);
         return name;
     }
@@ -502,7 +485,7 @@
     @Override
     public NamingEnumeration<NameClassPair> list(String name)
             throws NamingException {
-        return list(convertFromStringToName(name));
+        return list(new CompositeName(name));
     }
 
     @Override
@@ -532,7 +515,7 @@
     @Override
     public NamingEnumeration<Binding> listBindings(String name)
             throws NamingException {
-        return listBindings(convertFromStringToName(name));
+        return listBindings(new CompositeName(name));
     }
 
     private Hashtable<Name, LdapSchemaContextImpl> cachedSubSchemas = new Hashtable<Name,
LdapSchemaContextImpl>();
@@ -552,7 +535,7 @@
 
         Hashtable<String, Object> newSchemaTable = doLookup(name, size);
 
-        cachedSchema = new LdapSchemaContextImpl(parent, env, targetDN,
+        cachedSchema = new LdapSchemaContextImpl(ldapContext, env, targetDN,
                 newSchemaTable, level - size);
         cachedSubSchemas.put(targetDN, cachedSchema);
 
@@ -591,7 +574,7 @@
 
     @Override
     public Object lookup(String name) throws NamingException {
-        return lookup(convertFromStringToName(name));
+        return lookup(new CompositeName(name));
     }
 
     @Override
@@ -637,9 +620,8 @@
                 if (isMatch(schemaAttributes, matchingAttributes)) {
                     schemaAttributes = filterAttributes(schemaAttributes,
                             attributesToReturn);
-                    searchResult = new SearchResult(
-                            ldap2jndi(schemaName), this.getClass().getName(),
-                            null, schemaAttributes);
+                    searchResult = new SearchResult(ldap2jndi(schemaName), this
+                            .getClass().getName(), null, schemaAttributes);
                     enumeration.add(searchResult);
                 }
             }
@@ -678,9 +660,8 @@
         // SearchControls.OBJECT_SCOPE.
         else {
             schemaAttributes = getAttributes(name);
-            searchResult = new SearchResult(
-                    ldap2jndi(name.toString()), this.getClass().getName(),
-                    null, schemaAttributes);
+            searchResult = new SearchResult(ldap2jndi(name.toString()), this
+                    .getClass().getName(), null, schemaAttributes);
             searchResults.add(searchResult);
         }
 
@@ -720,6 +701,26 @@
         return enumeration;
     }
 
+    @Override
+    public NamingEnumeration<SearchResult> search(String name, Attributes attributes,
String[] as) throws NamingException {
+        return search(new CompositeName(name), attributes, as);
+    }
+
+    @Override
+    public NamingEnumeration<SearchResult> search(String name, Attributes attributes)
throws NamingException {
+        return search(new CompositeName(name), attributes);
+    }
+
+    @Override
+    public NamingEnumeration<SearchResult> search(String name, String filter, Object[]
objs, SearchControls searchControls) throws NamingException {
+        return search(new CompositeName(name), filter, objs, searchControls);
+    }
+
+    @Override
+    public NamingEnumeration<SearchResult> search(String name, String filter, SearchControls
searchControls) throws NamingException {
+        return search(new CompositeName(name), filter, searchControls);
+    }
+    
     protected DirContext getClassDefinition(Attribute objectclassAttr)
             throws NamingException {
         Hashtable<String, Object> definitionTable = new Hashtable<String, Object>();
@@ -760,8 +761,8 @@
             tempName = tempName.add(schemaName);
             if (tempName.size() < level) {
                 schemaAttributes = getAttributes(tempName);
-                searchResult = new SearchResult(tempName.toString(),
-                        this.getClass().getName(), null, schemaAttributes);
+                searchResult = new SearchResult(tempName.toString(), this
+                        .getClass().getName(), null, schemaAttributes);
                 searchResults.add(searchResult);
 
                 if (searchSubTree) {
@@ -823,6 +824,7 @@
         return true;
     }
 
+    // Convert ldap name to jndi name.
     private String ldap2jndi(String jndiName) {
         String ldapName = schemaLdap2Jndi.get(jndiName);
         if (null == ldapName) {
@@ -832,12 +834,20 @@
         return ldapName;
     }
 
-    private String jndi2ldap(String ldapName) {
-        String jndiName = schemaJndi2Ldap.get(ldapName.toLowerCase());
-        if (null == jndiName) {
-            jndiName = ldapName;
+    // Convert jndi name to ldap name.
+    private String jndi2ldap(String jndiName) {
+        // If the parameter indeed is ldapName, convert it to jndiName to avoid
+        // confusion.
+        String ldapName = schemaLdap2Jndi.get(jndiName);
+        if (null != ldapName) {
+            return ldapName;
+        }
+
+        ldapName = schemaJndi2Ldap.get(jndiName.toLowerCase());
+        if (null == ldapName) {
+            ldapName = jndiName;
         }
 
-        return jndiName.toLowerCase();
+        return ldapName.toLowerCase();
     }
 }

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaContextTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaContextTest.java?rev=659433&r1=659432&r2=659433&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaContextTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaContextTest.java
Thu May 22 22:38:03 2008
@@ -224,6 +224,13 @@
         } catch (NameNotFoundException e) {
             // Expected.
         }
+        
+        try {
+            namingEnum = schema.list("objectclasses");
+            fail("Should throw NameNotFoundException.");
+        } catch (NameNotFoundException e) {
+            // Expected.
+        }
     }
 
     public void testList_Address() throws Exception {



Mime
View raw message