You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ak...@apache.org on 2007/01/05 23:52:56 UTC

svn commit: r493222 - in /directory/trunks/apacheds: core-unit/src/test/java/org/apache/directory/server/core/schema/ core/src/main/java/org/apache/directory/server/core/ core/src/main/java/org/apache/directory/server/core/schema/ core/src/test/java/or...

Author: akarasulu
Date: Fri Jan  5 14:52:55 2007
New Revision: 493222

URL: http://svn.apache.org/viewvc?view=rev&rev=493222
Log:
added syntax update handler for ou=schema operations

Added:
    directory/trunks/apacheds/core-unit/src/test/java/org/apache/directory/server/core/schema/MetaSyntaxHandlerITest.java
    directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/schema/MetaSyntaxHandler.java
Modified:
    directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/DefaultDirectoryService.java
    directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/schema/SchemaManager.java
    directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/schema/SchemaPartitionDao.java
    directory/trunks/apacheds/core/src/test/java/org/apache/directory/server/core/authz/support/DummyOidRegistry.java
    directory/trunks/apacheds/core/src/test/java/org/apache/directory/server/core/schema/SchemaCheckerTest.java
    directory/trunks/apacheds/schema-registries/src/main/java/org/apache/directory/server/schema/registries/DefaultOidRegistry.java
    directory/trunks/apacheds/schema-registries/src/main/java/org/apache/directory/server/schema/registries/DefaultSyntaxRegistry.java
    directory/trunks/apacheds/schema-registries/src/main/java/org/apache/directory/server/schema/registries/OidRegistry.java
    directory/trunks/apacheds/schema-registries/src/main/java/org/apache/directory/server/schema/registries/SyntaxRegistry.java

Added: directory/trunks/apacheds/core-unit/src/test/java/org/apache/directory/server/core/schema/MetaSyntaxHandlerITest.java
URL: http://svn.apache.org/viewvc/directory/trunks/apacheds/core-unit/src/test/java/org/apache/directory/server/core/schema/MetaSyntaxHandlerITest.java?view=auto&rev=493222
==============================================================================
--- directory/trunks/apacheds/core-unit/src/test/java/org/apache/directory/server/core/schema/MetaSyntaxHandlerITest.java (added)
+++ directory/trunks/apacheds/core-unit/src/test/java/org/apache/directory/server/core/schema/MetaSyntaxHandlerITest.java Fri Jan  5 14:52:55 2007
@@ -0,0 +1,459 @@
+/*
+ *  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.directory.server.core.schema;
+
+
+import javax.naming.NamingException;
+import javax.naming.directory.Attribute;
+import javax.naming.directory.Attributes;
+import javax.naming.directory.DirContext;
+import javax.naming.directory.ModificationItem;
+
+import org.apache.directory.server.constants.MetaSchemaConstants;
+import org.apache.directory.server.constants.SystemSchemaConstants;
+import org.apache.directory.server.core.unit.AbstractAdminTestCase;
+import org.apache.directory.shared.ldap.exception.LdapInvalidNameException;
+import org.apache.directory.shared.ldap.message.LockableAttributeImpl;
+import org.apache.directory.shared.ldap.message.LockableAttributesImpl;
+import org.apache.directory.shared.ldap.message.ResultCodeEnum;
+import org.apache.directory.shared.ldap.name.LdapDN;
+import org.apache.directory.shared.ldap.schema.Syntax;
+
+
+/**
+ * A test case which tests the addition of various schema elements
+ * to the ldap server.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class MetaSyntaxHandlerITest extends AbstractAdminTestCase
+{
+    private static final String DESCRIPTION0 = "A test normalizer";
+    private static final String DESCRIPTION1 = "An alternate description";
+
+    private static final String OID = "1.3.6.1.4.1.18060.0.4.0.0.100000";
+    private static final String NEW_OID = "1.3.6.1.4.1.18060.0.4.0.0.100001";
+
+    
+    /**
+     * Gets relative DN to ou=schema.
+     */
+    private final LdapDN getSyntaxContainer( String schemaName ) throws NamingException
+    {
+        return new LdapDN( "ou=normalizers,cn=" + schemaName );
+    }
+    
+    
+    // ----------------------------------------------------------------------
+    // Test all core methods with normal operational pathways
+    // ----------------------------------------------------------------------
+
+    
+    public void testAddSyntax() throws NamingException
+    {
+        Attributes attrs = new LockableAttributesImpl();
+        Attribute oc = new LockableAttributeImpl( SystemSchemaConstants.OBJECT_CLASS_AT, "top" );
+        oc.add( MetaSchemaConstants.META_TOP_OC );
+        oc.add( MetaSchemaConstants.META_SYNTAX_OC );
+        attrs.put( oc );
+        attrs.put( MetaSchemaConstants.M_OID_AT, OID );
+        attrs.put( MetaSchemaConstants.M_DESCRIPTION_AT, DESCRIPTION0 );
+        
+        LdapDN dn = getSyntaxContainer( "apachemeta" );
+        dn.add( MetaSchemaConstants.M_OID_AT + "=" + OID );
+        super.schemaRoot.createSubcontext( dn, attrs );
+        
+        assertTrue( registries.getSyntaxRegistry().hasSyntax( OID ) );
+        assertEquals( registries.getSyntaxRegistry().getSchemaName( OID ), "apachemeta" );
+    }
+    
+    
+    public void testDeleteSyntax() throws NamingException
+    {
+        LdapDN dn = getSyntaxContainer( "apachemeta" );
+        dn.add( MetaSchemaConstants.M_OID_AT + "=" + OID );
+        testAddSyntax();
+        
+        super.schemaRoot.destroySubcontext( dn );
+
+        assertFalse( "syntax should be removed from the registry after being deleted", 
+            registries.getSyntaxRegistry().hasSyntax( OID ) );
+        
+        try
+        {
+            registries.getSyntaxRegistry().lookup( OID );
+            fail( "syntax lookup should fail after deleting it" );
+        }
+        catch( NamingException e )
+        {
+        }
+    }
+
+
+    public void testRenameSyntax() throws NamingException
+    {
+        LdapDN dn = getSyntaxContainer( "apachemeta" );
+        dn.add( MetaSchemaConstants.M_OID_AT + "=" + OID );
+        testAddSyntax();
+        
+        LdapDN newdn = getSyntaxContainer( "apachemeta" );
+        newdn.add( MetaSchemaConstants.M_OID_AT + "=" + NEW_OID );
+        super.schemaRoot.rename( dn, newdn );
+
+        assertFalse( "old syntax OID should be removed from the registry after being renamed", 
+            registries.getSyntaxRegistry().hasSyntax( OID ) );
+        
+        try
+        {
+            registries.getSyntaxRegistry().lookup( OID );
+            fail( "syntax lookup should fail after deleting the syntax" );
+        }
+        catch( NamingException e )
+        {
+        }
+
+        assertTrue( registries.getSyntaxRegistry().hasSyntax( NEW_OID ) );
+    }
+
+
+    public void testMoveSyntax() throws NamingException
+    {
+        testAddSyntax();
+        
+        LdapDN dn = getSyntaxContainer( "apachemeta" );
+        dn.add( MetaSchemaConstants.M_OID_AT + "=" + OID );
+
+        LdapDN newdn = getSyntaxContainer( "apache" );
+        newdn.add( MetaSchemaConstants.M_OID_AT + "=" + OID );
+        
+        super.schemaRoot.rename( dn, newdn );
+
+        assertTrue( "syntax OID should still be present", 
+            registries.getSyntaxRegistry().hasSyntax( OID ) );
+        
+        assertEquals( "syntax schema should be set to apache not apachemeta", 
+            registries.getSyntaxRegistry().getSchemaName( OID ), "apache" );
+    }
+
+
+    public void testMoveSyntaxAndChangeRdn() throws NamingException
+    {
+        testAddSyntax();
+        
+        LdapDN dn = getSyntaxContainer( "apachemeta" );
+        dn.add( MetaSchemaConstants.M_OID_AT + "=" + OID );
+
+        LdapDN newdn = getSyntaxContainer( "apache" );
+        newdn.add( MetaSchemaConstants.M_OID_AT + "=" + NEW_OID );
+        
+        super.schemaRoot.rename( dn, newdn );
+
+        assertFalse( "old syntax OID should NOT be present", 
+            registries.getSyntaxRegistry().hasSyntax( OID ) );
+        
+        assertTrue( "new syntax OID should be present", 
+            registries.getSyntaxRegistry().hasSyntax( NEW_OID ) );
+        
+        assertEquals( "syntax with new oid should have schema set to apache NOT apachemeta", 
+            registries.getSyntaxRegistry().getSchemaName( NEW_OID ), "apache" );
+    }
+
+    
+    public void testModifySyntaxWithModificationItems() throws NamingException
+    {
+        testAddSyntax();
+        
+        Syntax syntax = registries.getSyntaxRegistry().lookup( OID );
+        assertEquals( syntax.getDescription(), DESCRIPTION0 );
+
+        LdapDN dn = getSyntaxContainer( "apachemeta" );
+        dn.add( MetaSchemaConstants.M_OID_AT + "=" + OID );
+        
+        ModificationItem[] mods = new ModificationItem[1];
+        Attribute attr = new LockableAttributeImpl( MetaSchemaConstants.M_DESCRIPTION_AT, DESCRIPTION1 );
+        mods[0] = new ModificationItem( DirContext.REPLACE_ATTRIBUTE, attr );
+        super.schemaRoot.modifyAttributes( dn, mods );
+
+        assertTrue( "syntax OID should still be present", 
+            registries.getSyntaxRegistry().hasSyntax( OID ) );
+        
+        assertEquals( "syntax schema should be set to apachemeta", 
+            registries.getSyntaxRegistry().getSchemaName( OID ), "apachemeta" );
+        
+        syntax = registries.getSyntaxRegistry().lookup( OID );
+        assertEquals( syntax.getDescription(), DESCRIPTION1 );
+    }
+
+    
+    public void testModifySyntaxWithAttributes() throws NamingException
+    {
+        testAddSyntax();
+        
+        Syntax syntax = registries.getSyntaxRegistry().lookup( OID );
+        assertEquals( syntax.getDescription(), DESCRIPTION0 );
+
+        LdapDN dn = getSyntaxContainer( "apachemeta" );
+        dn.add( MetaSchemaConstants.M_OID_AT + "=" + OID );
+        
+        Attributes mods = new LockableAttributesImpl();
+        mods.put( MetaSchemaConstants.M_DESCRIPTION_AT, DESCRIPTION1 );
+        super.schemaRoot.modifyAttributes( dn, DirContext.REPLACE_ATTRIBUTE, mods );
+
+        assertTrue( "syntax OID should still be present", 
+            registries.getSyntaxRegistry().hasSyntax( OID ) );
+        
+        assertEquals( "syntax schema should be set to apachemeta", 
+            registries.getSyntaxRegistry().getSchemaName( OID ), "apachemeta" );
+
+        syntax = registries.getSyntaxRegistry().lookup( OID );
+        assertEquals( syntax.getDescription(), DESCRIPTION1 );
+    }
+    
+
+    // ----------------------------------------------------------------------
+    // Test move, rename, and delete when a MR exists and uses the Normalizer
+    // ----------------------------------------------------------------------
+
+    
+//    public void testDeleteSyntaxWhenInUse() throws NamingException
+//    {
+//        LdapDN dn = getSyntaxContainer( "apachemeta" );
+//        dn.add( MetaSchemaConstants.M_OID_AT + "=" + OID );
+//        testAddSyntax();
+//        addDependeeMatchingRule();
+//        
+//        try
+//        {
+//            super.schemaRoot.destroySubcontext( dn );
+//            fail( "should not be able to delete a syntax in use" );
+//        }
+//        catch( LdapOperationNotSupportedException e ) 
+//        {
+//            assertEquals( e.getResultCode(), ResultCodeEnum.UNWILLING_TO_PERFORM );
+//        }
+//
+//        assertTrue( "syntax should still be in the registry after delete failure", 
+//            registries.getSyntaxRegistry().hasSyntax( OID ) );
+//    }
+//    
+//    
+//    public void testMoveSyntaxWhenInUse() throws NamingException
+//    {
+//        testAddSyntax();
+//        addDependeeMatchingRule();
+//        
+//        LdapDN dn = getSyntaxContainer( "apachemeta" );
+//        dn.add( MetaSchemaConstants.M_OID_AT + "=" + OID );
+//
+//        LdapDN newdn = getSyntaxContainer( "apache" );
+//        newdn.add( MetaSchemaConstants.M_OID_AT + "=" + OID );
+//        
+//        try
+//        {
+//            super.schemaRoot.rename( dn, newdn );
+//            fail( "should not be able to move a syntax in use" );
+//        }
+//        catch( LdapOperationNotSupportedException e ) 
+//        {
+//            assertEquals( e.getResultCode(), ResultCodeEnum.UNWILLING_TO_PERFORM );
+//        }
+//
+//        assertTrue( "syntax should still be in the registry after move failure", 
+//            registries.getSyntaxRegistry().hasSyntax( OID ) );
+//    }
+//
+//
+//    public void testMoveSyntaxAndChangeRdnWhenInUse() throws NamingException
+//    {
+//        testAddSyntax();
+//        addDependeeMatchingRule()
+//        
+//        LdapDN dn = getSyntaxContainer( "apachemeta" );
+//        dn.add( MetaSchemaConstants.M_OID_AT + "=" + OID );
+//
+//        LdapDN newdn = getSyntaxContainer( "apache" );
+//        newdn.add( MetaSchemaConstants.M_OID_AT + "=" + NEW_OID );
+//        
+//        try
+//        {
+//            super.schemaRoot.rename( dn, newdn );
+//            fail( "should not be able to move a syntax in use" );
+//        }
+//        catch( LdapOperationNotSupportedException e ) 
+//        {
+//            assertEquals( e.getResultCode(), ResultCodeEnum.UNWILLING_TO_PERFORM );
+//        }
+//
+//        assertTrue( "syntax should still be in the registry after move failure", 
+//            registries.getSyntaxRegistry().hasSyntax( OID ) );
+//    }
+//
+//    
+
+    // Need to add body to this method which creates a new matchingRule after 
+    // the matchingRule addition code has been added.
+    
+//    private void addDependeeMatchingRule()
+//    {
+//        throw new NotImplementedException();
+//    }
+//    
+//    public void testRenameNormalizerWhenInUse() throws NamingException
+//    {
+//        LdapDN dn = getSyntaxContainer( "apachemeta" );
+//        dn.add( MetaSchemaConstants.M_OID_AT + "=" + OID );
+//        testAddSyntax();
+//        addDependeeMatchingRule();
+//        
+//        LdapDN newdn = getSyntaxContainer( "apachemeta" );
+//        newdn.add( MetaSchemaConstants.M_OID_AT + "=" + NEW_OID );
+//        
+//        try
+//        {
+//            super.schemaRoot.rename( dn, newdn );
+//            fail( "should not be able to rename a syntax in use" );
+//        }
+//        catch( LdapOperationNotSupportedException e ) 
+//        {
+//            assertEquals( e.getResultCode(), ResultCodeEnum.UNWILLING_TO_PERFORM );
+//        }
+//
+//        assertTrue( "syntax should still be in the registry after rename failure", 
+//            registries.getSyntaxRegistry().hasSyntax( OID ) );
+//    }
+
+
+    // ----------------------------------------------------------------------
+    // Let's try some freaky stuff
+    // ----------------------------------------------------------------------
+
+
+    public void testMoveSyntaxToTop() throws NamingException
+    {
+        testAddSyntax();
+        
+        LdapDN dn = getSyntaxContainer( "apachemeta" );
+        dn.add( MetaSchemaConstants.M_OID_AT + "=" + OID );
+
+        LdapDN top = new LdapDN();
+        top.add( MetaSchemaConstants.M_OID_AT + "=" + OID );
+        
+        try
+        {
+            super.schemaRoot.rename( dn, top );
+            fail( "should not be able to move a syntax up to ou=schema" );
+        }
+        catch( LdapInvalidNameException e ) 
+        {
+            assertEquals( e.getResultCode(), ResultCodeEnum.NAMING_VIOLATION );
+        }
+
+        assertTrue( "syntax should still be in the registry after move failure", 
+            registries.getSyntaxRegistry().hasSyntax( OID ) );
+    }
+
+
+    public void testMoveSyntaxToComparatorContainer() throws NamingException
+    {
+        testAddSyntax();
+        
+        LdapDN dn = getSyntaxContainer( "apachemeta" );
+        dn.add( MetaSchemaConstants.M_OID_AT + "=" + OID );
+
+        LdapDN newdn = new LdapDN( "ou=comparators,cn=apachemeta" );
+        newdn.add( MetaSchemaConstants.M_OID_AT + "=" + OID );
+        
+        try
+        {
+            super.schemaRoot.rename( dn, newdn );
+            fail( "should not be able to move a syntax into comparators container" );
+        }
+        catch( LdapInvalidNameException e ) 
+        {
+            assertEquals( e.getResultCode(), ResultCodeEnum.NAMING_VIOLATION );
+        }
+
+        assertTrue( "syntax should still be in the registry after move failure", 
+            registries.getSyntaxRegistry().hasSyntax( OID ) );
+    }
+    
+    
+    public void testAddSyntaxToDisabledSchema() throws NamingException
+    {
+        Attributes attrs = new LockableAttributesImpl();
+        Attribute oc = new LockableAttributeImpl( SystemSchemaConstants.OBJECT_CLASS_AT, "top" );
+        oc.add( MetaSchemaConstants.META_TOP_OC );
+        oc.add( MetaSchemaConstants.META_SYNTAX_OC );
+        attrs.put( oc );
+        attrs.put( MetaSchemaConstants.M_OID_AT, OID );
+        attrs.put( MetaSchemaConstants.M_DESCRIPTION_AT, DESCRIPTION0 );
+        
+        // nis is by default inactive
+        LdapDN dn = getSyntaxContainer( "nis" );
+        dn.add( MetaSchemaConstants.M_OID_AT + "=" + OID );
+        super.schemaRoot.createSubcontext( dn, attrs );
+        
+        assertFalse( "adding new syntax to disabled schema should not register it into the registries", 
+            registries.getSyntaxRegistry().hasSyntax( OID ) );
+    }
+
+
+    public void testMoveSyntaxToDisabledSchema() throws NamingException
+    {
+        testAddSyntax();
+        
+        LdapDN dn = getSyntaxContainer( "apachemeta" );
+        dn.add( MetaSchemaConstants.M_OID_AT + "=" + OID );
+
+        // nis is inactive by default
+        LdapDN newdn = getSyntaxContainer( "nis" );
+        newdn.add( MetaSchemaConstants.M_OID_AT + "=" + OID );
+        
+        super.schemaRoot.rename( dn, newdn );
+
+        assertFalse( "syntax OID should no longer be present", 
+            registries.getSyntaxRegistry().hasSyntax( OID ) );
+    }
+
+
+    public void testMoveSyntaxToEnabledSchema() throws NamingException
+    {
+        testAddSyntaxToDisabledSchema();
+        
+        // nis is inactive by default
+        LdapDN dn = getSyntaxContainer( "nis" );
+        dn.add( MetaSchemaConstants.M_OID_AT + "=" + OID );
+
+        assertFalse( "syntax OID should NOT be present when added to disabled nis schema", 
+            registries.getSyntaxRegistry().hasSyntax( OID ) );
+
+        LdapDN newdn = getSyntaxContainer( "apachemeta" );
+        newdn.add( MetaSchemaConstants.M_OID_AT + "=" + OID );
+        
+        super.schemaRoot.rename( dn, newdn );
+
+        assertTrue( "syntax OID should be present when moved to enabled schema", 
+            registries.getSyntaxRegistry().hasSyntax( OID ) );
+        
+        assertEquals( "syntax should be in apachemeta schema after move", 
+            registries.getSyntaxRegistry().getSchemaName( OID ), "apachemeta" );
+    }
+}

Modified: directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/DefaultDirectoryService.java
URL: http://svn.apache.org/viewvc/directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/DefaultDirectoryService.java?view=diff&rev=493222&r1=493221&r2=493222
==============================================================================
--- directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/DefaultDirectoryService.java (original)
+++ directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/DefaultDirectoryService.java Fri Jan  5 14:52:55 2007
@@ -909,7 +909,8 @@
             }
         }
         
-        schemaManager = new SchemaManager( globalRegistries, schemaLoader );
+        schemaManager = new SchemaManager( globalRegistries, schemaLoader, 
+            new SchemaPartitionDao( schemaPartition, registries ) );
 
         // now get all the attributeTypes that are binary from the registry
         AttributeTypeRegistry registry = registries.getAttributeTypeRegistry();

Added: directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/schema/MetaSyntaxHandler.java
URL: http://svn.apache.org/viewvc/directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/schema/MetaSyntaxHandler.java?view=auto&rev=493222
==============================================================================
--- directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/schema/MetaSyntaxHandler.java (added)
+++ directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/schema/MetaSyntaxHandler.java Fri Jan  5 14:52:55 2007
@@ -0,0 +1,297 @@
+/*
+ *   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.directory.server.core.schema;
+
+
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.naming.NamingException;
+import javax.naming.directory.Attribute;
+import javax.naming.directory.Attributes;
+import javax.naming.directory.ModificationItem;
+import javax.naming.directory.SearchResult;
+
+import org.apache.directory.server.constants.MetaSchemaConstants;
+import org.apache.directory.server.core.ServerUtils;
+import org.apache.directory.server.schema.bootstrap.Schema;
+import org.apache.directory.server.schema.registries.Registries;
+import org.apache.directory.server.schema.registries.SyntaxRegistry;
+import org.apache.directory.shared.ldap.exception.LdapInvalidNameException;
+import org.apache.directory.shared.ldap.exception.LdapOperationNotSupportedException;
+import org.apache.directory.shared.ldap.message.LockableAttributeImpl;
+import org.apache.directory.shared.ldap.message.ResultCodeEnum;
+import org.apache.directory.shared.ldap.name.LdapDN;
+import org.apache.directory.shared.ldap.name.Rdn;
+import org.apache.directory.shared.ldap.schema.AttributeType;
+import org.apache.directory.shared.ldap.schema.Syntax;
+import org.apache.directory.shared.ldap.util.NamespaceTools;
+
+
+/**
+ * A handler for operations peformed to add, delete, modify, rename and 
+ * move schema normalizers.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class MetaSyntaxHandler implements SchemaChangeHandler
+{
+    private static final String OU_OID = "2.5.4.11";
+
+    private final PartitionSchemaLoader loader;
+    private final SchemaPartitionDao dao;
+    private final SchemaEntityFactory factory;
+    private final Registries targetRegistries;
+    private final SyntaxRegistry syntaxRegistry;
+    private final AttributeType m_oidAT;
+
+    
+
+    public MetaSyntaxHandler( Registries targetRegistries, PartitionSchemaLoader loader, SchemaPartitionDao dao ) 
+        throws NamingException
+    {
+        this.targetRegistries = targetRegistries;
+        this.dao = dao;
+        this.loader = loader;
+        this.syntaxRegistry = targetRegistries.getSyntaxRegistry();
+        this.factory = new SchemaEntityFactory( targetRegistries );
+        this.m_oidAT = targetRegistries.getAttributeTypeRegistry().lookup( MetaSchemaConstants.M_OID_AT );
+    }
+
+
+    private String getOid( Attributes entry ) throws NamingException
+    {
+        Attribute oid = ServerUtils.getAttribute( m_oidAT, entry );
+        if ( oid == null )
+        {
+            return null;
+        }
+        return ( String ) oid.get();
+    }
+    
+    
+    private Schema getSchema( LdapDN name ) throws NamingException
+    {
+        return loader.getSchema( MetaSchemaUtils.getSchemaName( name ) );
+    }
+    
+    
+    private void modify( LdapDN name, Attributes entry, Attributes targetEntry ) throws NamingException
+    {
+        String oldOid = getOid( entry );
+        Syntax syntax = factory.getSyntax( targetEntry, targetRegistries );
+        Schema schema = getSchema( name );
+        
+        if ( ! schema.isDisabled() )
+        {
+            syntaxRegistry.unregister( oldOid );
+            syntaxRegistry.register( schema.getSchemaName(), syntax );
+        }
+    }
+
+
+    public void modify( LdapDN name, int modOp, Attributes mods, Attributes entry, Attributes targetEntry )
+        throws NamingException
+    {
+        modify( name, entry, targetEntry );
+    }
+
+
+    public void modify( LdapDN name, ModificationItem[] mods, Attributes entry, Attributes targetEntry )
+        throws NamingException
+    {
+        modify( name, entry, targetEntry );
+    }
+
+
+    public void add( LdapDN name, Attributes entry ) throws NamingException
+    {
+        LdapDN parentDn = ( LdapDN ) name.clone();
+        parentDn.remove( parentDn.size() - 1 );
+        checkNewParent( parentDn );
+        
+        Syntax syntax = factory.getSyntax( entry, targetRegistries );
+        Schema schema = getSchema( name );
+        
+        if ( ! schema.isDisabled() )
+        {
+            syntaxRegistry.register( schema.getSchemaName(), syntax );
+        }
+    }
+
+
+    private Set<String> getOids( Set<SearchResult> results ) throws NamingException
+    {
+        Set<String> oids = new HashSet<String>( results.size() );
+        
+        for ( SearchResult result : results )
+        {
+            LdapDN dn = new LdapDN( result.getName() );
+            dn.normalize( this.targetRegistries.getAttributeTypeRegistry().getNormalizerMapping() );
+            oids.add( ( String ) dn.getRdn().getValue() );
+        }
+        
+        return oids;
+    }
+    
+    
+    public void delete( LdapDN name, Attributes entry ) throws NamingException
+    {
+        String oid = getOid( entry );
+        
+        Set<SearchResult> dependees = dao.listSyntaxDependies( oid );
+        if ( dependees != null && dependees.size() > 0 )
+        {
+            throw new LdapOperationNotSupportedException( "The syntax with OID " + oid 
+                + " cannot be deleted until all entities" 
+                + " using this syntax have also been deleted.  The following dependees exist: " 
+                + getOids( dependees ), 
+                ResultCodeEnum.UNWILLING_TO_PERFORM );
+        }
+        
+        Schema schema = getSchema( name );
+        
+        if ( ! schema.isDisabled() )
+        {
+            syntaxRegistry.unregister( getOid( entry ) );
+        }
+    }
+
+
+    public void rename( LdapDN name, Attributes entry, String newRdn ) throws NamingException
+    {
+        String oldOid = getOid( entry );
+
+        Set<SearchResult> dependees = dao.listSyntaxDependies( oldOid );
+        if ( dependees != null && dependees.size() > 0 )
+        {
+            throw new LdapOperationNotSupportedException( "The syntax with OID " + oldOid
+                + " cannot be deleted until all entities" 
+                + " using this syntax have also been deleted.  The following dependees exist: " 
+                + getOids( dependees ), 
+                ResultCodeEnum.UNWILLING_TO_PERFORM );
+        }
+
+        Schema schema = getSchema( name );
+        Attributes targetEntry = ( Attributes ) entry.clone();
+        String newOid = NamespaceTools.getRdnValue( newRdn );
+        targetEntry.put( new LockableAttributeImpl( MetaSchemaConstants.M_OID_AT, newOid ) );
+        if ( ! schema.isDisabled() )
+        {
+            Syntax syntax = factory.getSyntax( targetEntry, targetRegistries );
+            syntaxRegistry.unregister( oldOid );
+            syntaxRegistry.register( schema.getSchemaName(), syntax );
+        }
+    }
+
+
+    public void move( LdapDN oriChildName, LdapDN newParentName, String newRn, boolean deleteOldRn, Attributes entry ) 
+        throws NamingException
+    {
+        checkNewParent( newParentName );
+        String oldOid = getOid( entry );
+
+        Set<SearchResult> dependees = dao.listSyntaxDependies( oldOid );
+        if ( dependees != null && dependees.size() > 0 )
+        {
+            throw new LdapOperationNotSupportedException( "The syntax with OID " + oldOid 
+                + " cannot be deleted until all entities" 
+                + " using this syntax have also been deleted.  The following dependees exist: " 
+                + getOids( dependees ), 
+                ResultCodeEnum.UNWILLING_TO_PERFORM );
+        }
+
+        Schema oldSchema = getSchema( oriChildName );
+        Schema newSchema = getSchema( newParentName );
+        Attributes targetEntry = ( Attributes ) entry.clone();
+        String newOid = NamespaceTools.getRdnValue( newRn );
+        targetEntry.put( new LockableAttributeImpl( MetaSchemaConstants.M_OID_AT, newOid ) );
+        Syntax syntax = factory.getSyntax( targetEntry, targetRegistries );
+
+        if ( ! oldSchema.isDisabled() )
+        {
+            syntaxRegistry.unregister( oldOid );
+        }
+
+        if ( ! newSchema.isDisabled() )
+        {
+            syntaxRegistry.register( newSchema.getSchemaName(), syntax );
+        }
+    }
+
+
+    public void move( LdapDN oriChildName, LdapDN newParentName, Attributes entry ) 
+        throws NamingException
+    {
+        checkNewParent( newParentName );
+        String oid = getOid( entry );
+
+        Set<SearchResult> dependees = dao.listSyntaxDependies( oid );
+        if ( dependees != null && dependees.size() > 0 )
+        {
+            throw new LdapOperationNotSupportedException( "The syntax with OID " + oid 
+                + " cannot be deleted until all entities" 
+                + " using this syntax have also been deleted.  The following dependees exist: " 
+                + getOids( dependees ), 
+                ResultCodeEnum.UNWILLING_TO_PERFORM );
+        }
+
+        Schema oldSchema = getSchema( oriChildName );
+        Schema newSchema = getSchema( newParentName );
+        
+        Syntax syntax = factory.getSyntax( entry, targetRegistries );
+        
+        if ( ! oldSchema.isDisabled() )
+        {
+            syntaxRegistry.unregister( oid );
+        }
+        
+        if ( ! newSchema.isDisabled() )
+        {
+            syntaxRegistry.register( newSchema.getSchemaName(), syntax );
+        }
+    }
+    
+    
+    private void checkNewParent( LdapDN newParent ) throws NamingException
+    {
+        if ( newParent.size() != 3 )
+        {
+            throw new LdapInvalidNameException( 
+                "The parent dn of a syntax should be at most 3 name components in length.", 
+                ResultCodeEnum.NAMING_VIOLATION );
+        }
+        
+        Rdn rdn = newParent.getRdn();
+        if ( ! targetRegistries.getOidRegistry().getOid( rdn.getType() ).equals( OU_OID ) )
+        {
+            throw new LdapInvalidNameException( "The parent entry of a syntax should be an organizationalUnit.", 
+                ResultCodeEnum.NAMING_VIOLATION );
+        }
+        
+        if ( ! ( ( String ) rdn.getValue() ).equalsIgnoreCase( "syntaxes" ) )
+        {
+            throw new LdapInvalidNameException( 
+                "The parent entry of a syntax should have a relative name of ou=syntaxes.", 
+                ResultCodeEnum.NAMING_VIOLATION );
+        }
+    }
+}

Modified: directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/schema/SchemaManager.java
URL: http://svn.apache.org/viewvc/directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/schema/SchemaManager.java?view=diff&rev=493222&r1=493221&r2=493222
==============================================================================
--- directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/schema/SchemaManager.java (original)
+++ directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/schema/SchemaManager.java Fri Jan  5 14:52:55 2007
@@ -57,9 +57,11 @@
     private final MetaComparatorHandler metaComparatorHandler;
     private final MetaNormalizerHandler metaNormalizerHandler;
     private final MetaSyntaxCheckerHandler metaSyntaxCheckerHandler;
+    private final MetaSyntaxHandler metaSyntaxHandler;
     
 
-    public SchemaManager( Registries globalRegistries, PartitionSchemaLoader loader ) throws NamingException
+    public SchemaManager( Registries globalRegistries, PartitionSchemaLoader loader, SchemaPartitionDao dao ) 
+        throws NamingException
     {
         this.loader = loader;
         this.globalRegistries = globalRegistries;
@@ -69,6 +71,7 @@
         this.metaComparatorHandler = new MetaComparatorHandler( globalRegistries, loader );
         this.metaNormalizerHandler = new MetaNormalizerHandler( globalRegistries, loader );
         this.metaSyntaxCheckerHandler = new MetaSyntaxCheckerHandler( globalRegistries, loader );
+        this.metaSyntaxHandler = new MetaSyntaxHandler( globalRegistries, loader, dao );
     }
     
     
@@ -112,6 +115,12 @@
             return;
         }
 
+        if ( AttributeUtils.containsValue( oc, MetaSchemaConstants.META_SYNTAX_OC, objectClassAT ) )
+        {
+            metaSyntaxHandler.add( name, entry );
+            return;
+        }
+
         throw new NotImplementedException( "only changes to metaSchema objects are managed at this time" );
     }
     
@@ -144,6 +153,12 @@
             return;
         }
 
+        if ( AttributeUtils.containsValue( oc, MetaSchemaConstants.META_SYNTAX_OC, objectClassAT ) )
+        {
+            metaSyntaxHandler.delete( name, entry );
+            return;
+        }
+
         throw new NotImplementedException( "only changes to metaSchema objects are managed at this time" );
     }
     
@@ -177,6 +192,12 @@
             return;
         }
 
+        if ( AttributeUtils.containsValue( oc, MetaSchemaConstants.META_SYNTAX_OC, objectClassAT ) )
+        {
+            metaSyntaxHandler.modify( name, modOp, mods, entry, targetEntry );
+            return;
+        }
+
         throw new NotImplementedException( "only changes to metaSchema objects are managed at this time" );
     }
 
@@ -210,6 +231,12 @@
             return;
         }
 
+        if ( AttributeUtils.containsValue( oc, MetaSchemaConstants.META_SYNTAX_OC, objectClassAT ) )
+        {
+            metaSyntaxHandler.modify( name, mods, entry, targetEntry );
+            return;
+        }
+
         throw new NotImplementedException( "only changes to metaSchema objects are managed at this time" );
     }
 
@@ -242,6 +269,12 @@
             return;
         }
 
+        if ( AttributeUtils.containsValue( oc, MetaSchemaConstants.META_SYNTAX_OC, objectClassAT ) )
+        {
+            metaSyntaxHandler.rename( name, entry, newRdn );
+            return;
+        }
+
         throw new NotImplementedException( "only changes to metaSchema objects are managed at this time" );
     }
 
@@ -274,6 +307,12 @@
             return;
         }
 
+        if ( AttributeUtils.containsValue( oc, MetaSchemaConstants.META_SYNTAX_OC, objectClassAT ) )
+        {
+            metaSyntaxHandler.move( oriChildName, newParentName, entry );
+            return;
+        }
+
         throw new NotImplementedException( "only changes to metaSchema objects are managed at this time" );
     }
 
@@ -304,6 +343,12 @@
         if ( AttributeUtils.containsValue( oc, MetaSchemaConstants.META_SYNTAX_CHECKER_OC, objectClassAT ) )
         {
             metaSyntaxCheckerHandler.move( oriChildName, newParentName, newRn, deleteOldRn, entry );
+            return;
+        }
+
+        if ( AttributeUtils.containsValue( oc, MetaSchemaConstants.META_SYNTAX_OC, objectClassAT ) )
+        {
+            metaSyntaxHandler.move( oriChildName, newParentName, newRn, deleteOldRn, entry );
             return;
         }
 

Modified: directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/schema/SchemaPartitionDao.java
URL: http://svn.apache.org/viewvc/directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/schema/SchemaPartitionDao.java?view=diff&rev=493222&r1=493221&r2=493222
==============================================================================
--- directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/schema/SchemaPartitionDao.java (original)
+++ directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/schema/SchemaPartitionDao.java Fri Jan  5 14:52:55 2007
@@ -320,4 +320,48 @@
         
         partition.modify( dn, mods );
     }
+
+
+    /**
+     * Returns the set of matchingRules and attributeTypes which depend on the 
+     * provided syntax.
+     *
+     * @param numericOid the numeric identifier for the entity
+     * @return
+     */
+    public Set<SearchResult> listSyntaxDependies( String numericOid ) throws NamingException
+    {
+        Set<SearchResult> set = new HashSet<SearchResult>( );
+        BranchNode filter = new BranchNode( AssertionEnum.AND );
+        
+        // subfilter for (| (objectClass=metaMatchingRule) (objectClass=metaAttributeType))  
+        BranchNode or = new BranchNode( AssertionEnum.OR );
+        or.addNode( new SimpleNode( SystemSchemaConstants.OBJECT_CLASS_AT, 
+            MetaSchemaConstants.META_MATCHING_RULE_OC, AssertionEnum.EQUALITY ) );
+        or.addNode( new SimpleNode( SystemSchemaConstants.OBJECT_CLASS_AT, 
+            MetaSchemaConstants.META_ATTRIBUTE_TYPE_OC, AssertionEnum.EQUALITY ) );
+        
+        filter.addNode( or );
+        filter.addNode( new SimpleNode( MetaSchemaConstants.M_SYNTAX_AT, 
+            numericOid.toLowerCase(), AssertionEnum.EQUALITY ) );
+
+        SearchControls searchControls = new SearchControls();
+        searchControls.setSearchScope( SearchControls.SUBTREE_SCOPE );
+        NamingEnumeration<SearchResult> ne = null;
+        
+        try
+        {
+            ne = partition.search( partition.getSuffix(), new HashMap(), filter, searchControls );
+            while( ne.hasMore() )
+            {
+                set.add( ne.next() );
+            }
+        }
+        finally
+        {
+            ne.close();
+        }
+        
+        return set;
+    }
 }

Modified: directory/trunks/apacheds/core/src/test/java/org/apache/directory/server/core/authz/support/DummyOidRegistry.java
URL: http://svn.apache.org/viewvc/directory/trunks/apacheds/core/src/test/java/org/apache/directory/server/core/authz/support/DummyOidRegistry.java?view=diff&rev=493222&r1=493221&r2=493222
==============================================================================
--- directory/trunks/apacheds/core/src/test/java/org/apache/directory/server/core/authz/support/DummyOidRegistry.java (original)
+++ directory/trunks/apacheds/core/src/test/java/org/apache/directory/server/core/authz/support/DummyOidRegistry.java Fri Jan  5 14:52:55 2007
@@ -58,9 +58,9 @@
     }
 
 
-    public List getNameSet( String oid ) throws NamingException
+    public List<String> getNameSet( String oid ) throws NamingException
     {
-        List list = new ArrayList();
+        List<String> list = new ArrayList<String>();
         list.add( oid );
         return list;
     }
@@ -98,4 +98,8 @@
         return null;
     }
 
+
+    public void unregister( String numericOid ) throws NamingException
+    {
+    }
 }

Modified: directory/trunks/apacheds/core/src/test/java/org/apache/directory/server/core/schema/SchemaCheckerTest.java
URL: http://svn.apache.org/viewvc/directory/trunks/apacheds/core/src/test/java/org/apache/directory/server/core/schema/SchemaCheckerTest.java?view=diff&rev=493222&r1=493221&r2=493222
==============================================================================
--- directory/trunks/apacheds/core/src/test/java/org/apache/directory/server/core/schema/SchemaCheckerTest.java (original)
+++ directory/trunks/apacheds/core/src/test/java/org/apache/directory/server/core/schema/SchemaCheckerTest.java Fri Jan  5 14:52:55 2007
@@ -599,5 +599,9 @@
         {
             return null;
         }
+
+        public void unregister( String numericOid ) throws NamingException
+        {
+        }
     }
 }

Modified: directory/trunks/apacheds/schema-registries/src/main/java/org/apache/directory/server/schema/registries/DefaultOidRegistry.java
URL: http://svn.apache.org/viewvc/directory/trunks/apacheds/schema-registries/src/main/java/org/apache/directory/server/schema/registries/DefaultOidRegistry.java?view=diff&rev=493222&r1=493221&r2=493222
==============================================================================
--- directory/trunks/apacheds/schema-registries/src/main/java/org/apache/directory/server/schema/registries/DefaultOidRegistry.java (original)
+++ directory/trunks/apacheds/schema-registries/src/main/java/org/apache/directory/server/schema/registries/DefaultOidRegistry.java Fri Jan  5 14:52:55 2007
@@ -297,4 +297,19 @@
             log.debug( "registed name '" + name + "' with OID: " + oid );
         }
     }
+
+
+    public void unregister( String numericOid ) throws NamingException
+    {
+        byOid.remove( numericOid );
+        Iterator<String> names = byName.keySet().iterator();
+        while ( names.hasNext() )
+        {
+            String name = names.next();
+            if ( numericOid.equals( byName.get( name ) ) )
+            {
+                byName.remove( name );
+            }
+        }
+    }
 }

Modified: directory/trunks/apacheds/schema-registries/src/main/java/org/apache/directory/server/schema/registries/DefaultSyntaxRegistry.java
URL: http://svn.apache.org/viewvc/directory/trunks/apacheds/schema-registries/src/main/java/org/apache/directory/server/schema/registries/DefaultSyntaxRegistry.java?view=diff&rev=493222&r1=493221&r2=493222
==============================================================================
--- directory/trunks/apacheds/schema-registries/src/main/java/org/apache/directory/server/schema/registries/DefaultSyntaxRegistry.java (original)
+++ directory/trunks/apacheds/schema-registries/src/main/java/org/apache/directory/server/schema/registries/DefaultSyntaxRegistry.java Fri Jan  5 14:52:55 2007
@@ -132,6 +132,11 @@
 
     public String getSchemaName( String id ) throws NamingException
     {
+        if ( ! Character.isDigit( id.charAt( 0 ) ) )
+        {
+            throw new NamingException( "Looks like the arg is not a numeric OID" );
+        }
+
         id = oidRegistry.getOid( id );
         if ( oidToSchema.containsKey( id ) )
         {
@@ -145,5 +150,18 @@
     public Iterator<Syntax> iterator()
     {
         return byOid.values().iterator();
+    }
+
+
+    public void unregister( String numericOid ) throws NamingException
+    {
+        if ( ! Character.isDigit( numericOid.charAt( 0 ) ) )
+        {
+            throw new NamingException( "Looks like the arg is not a numeric OID" );
+        }
+
+        byOid.remove( numericOid );
+        oidToSchema.remove( numericOid );
+        oidRegistry.unregister( numericOid );
     }
 }

Modified: directory/trunks/apacheds/schema-registries/src/main/java/org/apache/directory/server/schema/registries/OidRegistry.java
URL: http://svn.apache.org/viewvc/directory/trunks/apacheds/schema-registries/src/main/java/org/apache/directory/server/schema/registries/OidRegistry.java?view=diff&rev=493222&r1=493221&r2=493222
==============================================================================
--- directory/trunks/apacheds/schema-registries/src/main/java/org/apache/directory/server/schema/registries/OidRegistry.java (original)
+++ directory/trunks/apacheds/schema-registries/src/main/java/org/apache/directory/server/schema/registries/OidRegistry.java Fri Jan  5 14:52:55 2007
@@ -110,4 +110,13 @@
      * @return The Map that contains all the oids
      */
     public Map getNameByOid();
+
+
+    /**
+     * Removes an oid from this registry.
+     *
+     * @param numericOid the numeric identifier for the object
+     * @throws NamingException if the identifier is not numeric
+     */
+    void unregister( String numericOid ) throws NamingException;
 }

Modified: directory/trunks/apacheds/schema-registries/src/main/java/org/apache/directory/server/schema/registries/SyntaxRegistry.java
URL: http://svn.apache.org/viewvc/directory/trunks/apacheds/schema-registries/src/main/java/org/apache/directory/server/schema/registries/SyntaxRegistry.java?view=diff&rev=493222&r1=493221&r2=493222
==============================================================================
--- directory/trunks/apacheds/schema-registries/src/main/java/org/apache/directory/server/schema/registries/SyntaxRegistry.java (original)
+++ directory/trunks/apacheds/schema-registries/src/main/java/org/apache/directory/server/schema/registries/SyntaxRegistry.java Fri Jan  5 14:52:55 2007
@@ -83,4 +83,13 @@
      * @return an Iterator over all the Syntaxes within this registry
      */
     Iterator<Syntax> iterator();
+
+
+    /**
+     * Removes a registered syntax from this registry.
+     *
+     * @param numericOid the numerid id of the syntax being removed
+     * @throws NamingException if the id is not numeric
+     */
+    void unregister( String numericOid ) throws NamingException;
 }