You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by to...@apache.org on 2008/05/21 13:59:51 UTC

svn commit: r658641 - in /harmony/enhanced/classlib/trunk/modules/jndi: META-INF/ src/main/java/org/apache/harmony/jndi/provider/ldap/ src/test/java/org/apache/harmony/jndi/provider/ldap/

Author: tonywu
Date: Wed May 21 04:59:51 2008
New Revision: 658641

URL: http://svn.apache.org/viewvc?rev=658641&view=rev
Log:
Apply patch for HARMONY-5835 ([classlib][jndi][ldap] - replace LdapSchemaAttrDefContextImpl and LdapSchemaClassDefContextImpl with LdapSchemaContextImpl)

Removed:
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaAttrDefContextImpl.java
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaClassDefContextImpl.java
Modified:
    harmony/enhanced/classlib/trunk/modules/jndi/META-INF/MANIFEST.MF
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapAttribute.java
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextImpl.java
    harmony/enhanced/classlib/trunk/modules/jndi/src/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/META-INF/MANIFEST.MF
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/META-INF/MANIFEST.MF?rev=658641&r1=658640&r2=658641&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/META-INF/MANIFEST.MF (original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/META-INF/MANIFEST.MF Wed May 21 04:59:51 2008
@@ -28,6 +28,7 @@
  java.text;resolution:=optional,
  java.util,
  java.util.jar,
+ java.util.regex,
  java.util.zip;resolution:=optional,
  javax.accessibility;resolution:=optional,
  javax.net,

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapAttribute.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapAttribute.java?rev=658641&r1=658640&r2=658641&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapAttribute.java (original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapAttribute.java Wed May 21 04:59:51 2008
@@ -1,4 +1,4 @@
-/* 
+/*
  *  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. 
@@ -45,20 +45,10 @@
     private static final long serialVersionUID = -6492847268062616321L;
 
     /**
-     * TODO: when to initialize it?
-     */
-    private DirContext attributeDefinition = null;
-
-    /**
-     * TODO: when to initialize it?
-     */
-    private DirContext attributeSyntaxDefinition = null;
-
-    /**
      * whether the value of attribute is binary
      */
     private boolean isBinary;
-    
+
     private LdapContextImpl context = null;
 
     private static HashSet<String> BINARY_ATTRIBUTE = new HashSet<String>();
@@ -92,7 +82,7 @@
         isBinary = isBinary(id);
         context = ctx;
     }
-    
+
     void setContext(LdapContextImpl ctx) {
         context = ctx;
     }
@@ -112,9 +102,6 @@
             Object value = enu.next();
             add(value);
         }
-
-        attributeDefinition = null;
-        attributeSyntaxDefinition = null;
         context = ctx;
     }
 
@@ -155,37 +142,35 @@
 
     @Override
     public DirContext getAttributeDefinition() throws NamingException {
-        if (attributeDefinition != null) {
-            return attributeDefinition;
-        }
-        attributeDefinition = context
-                .getSchemaAttributeDefinition(getID());
-        return attributeDefinition;
+        DirContext schema = context.getSchema(""); //$NON-NLS-1$
+
+        return (DirContext) schema
+                .lookup(LdapSchemaContextImpl.ATTRIBUTE_DEFINITION
+                        + "/" + getID()); //$NON-NLS-1$
     }
 
     @Override
     public DirContext getAttributeSyntaxDefinition() throws NamingException {
-        if (attributeSyntaxDefinition != null) {
-            return attributeSyntaxDefinition;
-        }
-        //      get the syntax id from the attribute def
-        DirContext schema = context.getSchema("");
-        DirContext attrDef = (DirContext)schema.lookup(
-                LdapSchemaContextImpl.ATTRIBUTE_DEFINITION + "/" + getID());
+        DirContext schema = context.getSchema(""); //$NON-NLS-1$
+        DirContext attrDef = (DirContext) schema
+                .lookup(LdapSchemaContextImpl.ATTRIBUTE_DEFINITION + "/" //$NON-NLS-1$
+                        + getID());
 
-        Attribute syntaxAttr = attrDef.getAttributes("").get("syntax");
+        Attribute syntaxAttr = attrDef.getAttributes("").get("syntax"); //$NON-NLS-1$ //$NON-NLS-2$
 
         if (syntaxAttr == null || syntaxAttr.size() == 0) {
-            throw new NameNotFoundException(Messages.getString("jndi.90",
+            // jndi.90={0} does not have a syntax associated with it
+            throw new NameNotFoundException(Messages.getString("jndi.90", //$NON-NLS-1$
                     getID()));
         }
 
-        String syntaxName = (String)syntaxAttr.get();
+        String syntaxName = (String) syntaxAttr.get();
 
         // look in the schema tree for the syntax definition
-        return (DirContext)schema.lookup(
-                LdapSchemaContextImpl.SYNTAX_DEFINITION + "/" + syntaxName);
-        
+        return (DirContext) schema
+                .lookup(LdapSchemaContextImpl.SYNTAX_DEFINITION + "/" //$NON-NLS-1$
+                        + syntaxName);
+
     }
 
     private static boolean isBinary(String name) {

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=658641&r1=658640&r2=658641&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 Wed May 21 04:59:51 2008
@@ -1051,77 +1051,15 @@
         return getSchema(new CompositeName(s));
     }
 
-    DirContext getSchemaAttributeDefinition(String name) throws NamingException {
-        if (null == ldapSchemaCtx) {
-            getSchema("");
-        }
-        Hashtable<String, Object> attrDef = findSchemaDefInfo(
-                LdapSchemaContextImpl.ATTRIBUTE_TYPES, name);
-
-        return new LdapSchemaAttrDefContextImpl(new CompositeName(name), env,
-                attrDef, this);
-    }
-
     public DirContext getSchemaClassDefinition(Name name)
             throws NamingException {
-        checkName(name);
+        Attributes attrs = getAttributes(name, new String[] { "objectClass" }); //$NON-NLS-1$
+        Attribute attr = attrs.get("objectClass"); //$NON-NLS-1$
 
         if (null == ldapSchemaCtx) {
-            getSchema("");
-        }
-
-        Hashtable<String, ArrayList<String>> classTree = new Hashtable<String, ArrayList<String>>();
-
-        SearchControls searchControls = new SearchControls();
-        searchControls.setSearchScope(SearchControls.OBJECT_SCOPE);
-        searchControls.setReturningAttributes(new String[] { "objectClass", });
-        searchControls.setReturningObjFlag(false);
-        FilterParser parser = new FilterParser("(objectClass=*)");
-        Filter filter = null;
-        try {
-            filter = parser.parse();
-        } catch (ParseException e1) {
-            // Should not throw this excption
+            getSchema(""); //$NON-NLS-1$
         }
-        String targetDN = getTargetDN(name, contextDn);
-        SearchOp search = new SearchOp(targetDN, searchControls, filter);
-
-        try {
-            client.doOperation(search, requestControls);
-        } catch (IOException e) {
-            CommunicationException ex = new CommunicationException(e
-                    .getMessage());
-            ex.setRootCause(e);
-            if (search.getSearchResult().isEmpty()) {
-                throw ex;
-            }
-            search.getSearchResult().setException(ex);
-        }
-        LdapSearchResult sre = search.getSearchResult();
-        Map<String, Attributes> names = sre.getEntries();
-
-        Set<String> keyset = names.keySet();
-        for (Iterator<String> iterator = keyset.iterator(); iterator.hasNext();) {
-            String key = iterator.next();
-            Attributes as = names.get(key);
-            NamingEnumeration<String> ids = as.getIDs();
-
-            while (ids.hasMoreElements()) {
-                String schemaType = ids.nextElement();
-                if (!classTree.contains(schemaType)) {
-                    classTree.put(schemaType, new ArrayList());
-                }
-                ArrayList<String> classDefs = classTree.get(schemaType);
-                LdapAttribute attribute = (LdapAttribute) as.get(schemaType);
-                for (int i = 0; i < attribute.size(); i++) {
-                    String value = (String) attribute.get(i);
-                    classDefs.add(value);
-                }
-            }
-        }
-
-        return new LdapSchemaClassDefContextImpl(new CompositeName(targetDN),
-                env, classTree, this);
+        return ldapSchemaCtx.getClassDefinition(attr);
     }
 
     public DirContext getSchemaClassDefinition(String s) throws NamingException {

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=658641&r1=658640&r2=658641&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 Wed May 21 04:59:51 2008
@@ -67,6 +67,8 @@
 
     public static final int SCHEMA_ROOT_LEVEL = 3;
 
+    public static final int DEFINITION_LEVEL = 2;
+
     protected String subschemasubentry = null;
 
     final private static Hashtable<String, String> schemaJndi2Ldap = new Hashtable<String, String>();
@@ -718,6 +720,27 @@
         return enumeration;
     }
 
+    protected DirContext getClassDefinition(Attribute objectclassAttr)
+            throws NamingException {
+        Hashtable<String, Object> definitionTable = new Hashtable<String, Object>();
+        Hashtable<String, Object> allDefinitionTable = (Hashtable<String, Object>) schemaTable
+                .get(OBJECT_CLASSES);
+
+        if (objectclassAttr != null) {
+            NamingEnumeration<?> ne = objectclassAttr.getAll();
+            String attributeType;
+            while (ne.hasMore()) {
+                attributeType = ne.next().toString().toLowerCase();
+                definitionTable.put(attributeType, allDefinitionTable
+                        .get(attributeType));
+            }
+        }
+
+        return new LdapSchemaContextImpl(this, env, new CompositeName(
+                OBJECT_CLASSES), definitionTable,
+                LdapSchemaContextImpl.DEFINITION_LEVEL);
+    }
+
     private HashSet<SearchResult> doSimpleSearch(Name name,
             boolean searchSubTree) throws NamingException {
         int size = name.size();

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=658641&r1=658640&r2=658641&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 Wed May 21 04:59:51 2008
@@ -2067,4 +2067,143 @@
         }
         assertEquals(2, count);
     }
+
+    public void testClassDefinition() throws NamingException {
+        MockLdapSchemaContext mockSchema = new MockLdapSchemaContext(context,
+                null, name, schemaTable,
+                LdapSchemaContextImpl.SCHEMA_ROOT_LEVEL);
+        Attribute attribute = new BasicAttribute("objectClass", "javaClass");
+        attribute.add("extensibleobject");
+        attribute.add("prefNode");
+
+        DirContext classDefSchema = mockSchema.getClassDefinition(attribute);
+        ArrayList<String> verifyList = new ArrayList<String>();
+        verifyList.add("javaclass");
+        verifyList.add("prefnode");
+        verifyList.add("extensibleobject");
+        NamingEnumeration<NameClassPair> ne = classDefSchema.list("");
+        NameClassPair pair;
+        int count = 0;
+        while (ne.hasMore()) {
+            pair = ne.next();
+            count++;
+            assertTrue(verifyList.remove(pair.getName().toLowerCase()));
+        }
+        assertEquals(3, count);
+
+        ne = classDefSchema.list("prefnode");
+        assertFalse(ne.hasMore());
+
+        ne = classDefSchema.list("extensibleobject");
+        assertFalse(ne.hasMore());
+    }
+
+    public void testAttributeDefinition() throws NamingException {
+        addMoreAttributeData();
+        MockLdapContext mockContext = new MockLdapContext(context, null, "");
+        Attribute attr = new LdapAttribute("objectclass", mockContext);
+
+        DirContext attributeDefinition = attr.getAttributeDefinition();
+        NamingEnumeration<NameClassPair> ne = attributeDefinition.list("");
+        assertFalse(ne.hasMore());
+
+        try {
+            ne = attributeDefinition.list("invalid");
+            fail("Should throw NameNotFoundException");
+        } catch (NameNotFoundException e) {
+            // Expected.
+        }
+
+        Attributes schemaAttributes = attributeDefinition.getAttributes("");
+        assertEquals(7, schemaAttributes.size());
+        assertEquals("1.3.6.1.4.1.1466.115.121.1.38", schemaAttributes.get(
+                "syntax").get());
+        assertEquals("objectClass", schemaAttributes.get("name").get());
+        assertEquals("2.5.4.0", schemaAttributes.get("numericoid").get());
+        assertEquals("userApplications", schemaAttributes.get("usage").get());
+        assertEquals("objectIdentifierMatch", schemaAttributes.get("equality")
+                .get());
+    }
+
+    public void testSyntaxDefinition() throws NamingException {
+        addMoreAttributeData();
+        MockLdapContext mockContext = new MockLdapContext(context, null, "");
+        Attribute attr = new LdapAttribute("objectclass", mockContext);
+        DirContext attributeDefinition = attr.getAttributeSyntaxDefinition();
+        NamingEnumeration<NameClassPair> ne = attributeDefinition.list("");
+        assertFalse(ne.hasMore());
+
+        try {
+            ne = attributeDefinition.list("invalid");
+            fail("Should throw NameNotFoundException");
+        } catch (NameNotFoundException e) {
+            // Expected.
+        }
+        Attributes schemaAttributes = attributeDefinition.getAttributes("");
+        assertEquals(3, schemaAttributes.size());
+        assertEquals("system", schemaAttributes.get("x-schema").get());
+        assertEquals("true", schemaAttributes.get("x-is-human-readable").get());
+        assertEquals("1.3.6.1.4.1.1466.115.121.1.38", schemaAttributes.get(
+                "numericoid").get());
+    }
+
+    private void addMoreAttributeData() throws InvalidNameException {
+        // Add more schema data.
+        Hashtable subschemaTable = (Hashtable) schemaTable
+                .get("attributetypes");
+
+        subschemaTable
+                .put(
+                        "objectclass",
+                        "( 2.5.4.0 NAME 'objectClass' "
+                                + "DESC 'RFC2256: object classes of the entity'  "
+                                + "EQUALITY objectIdentifierMatch SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 "
+                                + "USAGE userApplications X-SCHEMA 'system' )");
+
+        subschemaTable = (Hashtable) schemaTable.get("ldapsyntaxes");
+        subschemaTable
+                .put(
+                        "1.3.6.1.4.1.1466.115.121.1.38",
+                        "( 1.3.6.1.4.1.1466.115.121.1.38  X-SCHEMA 'system' X-IS-HUMAN-READABLE 'true' )");
+        schema = new LdapSchemaContextImpl(context, null, name, schemaTable,
+                LdapSchemaContextImpl.SCHEMA_ROOT_LEVEL);
+    }
+
+    public class MockLdapContext extends LdapContextImpl {
+        public MockLdapContext(LdapContextImpl context,
+                Hashtable<Object, Object> environment, String dn)
+                throws InvalidNameException {
+            super(context, environment, dn);
+        }
+
+        public DirContext getSchema(String name) {
+            return schema;
+        }
+
+        public Attributes getAttributes(Name name, String returningAttributes[])
+                throws NamingException {
+            Attribute attribute = new BasicAttribute("objectClass", "javaClass");
+            attribute.add("extensibleobject");
+            attribute.add("prefNode");
+            Attributes attributes = new BasicAttributes(true);
+            attributes.put(attribute);
+
+            return attributes;
+        }
+    }
+
+    public class MockLdapSchemaContext extends LdapSchemaContextImpl {
+
+        public MockLdapSchemaContext(LdapContextImpl ctx,
+                Hashtable<Object, Object> env, Name dn,
+                Hashtable<String, Object> schemaTable, int level)
+                throws InvalidNameException {
+            super(ctx, env, dn, schemaTable, level);
+        }
+
+        public DirContext getClassDefinition(Attribute attr)
+                throws NamingException {
+            return super.getClassDefinition(attr);
+        }
+    }
 }