directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r892548 - in /directory/shared/branches/shared-schema: ldap-schema-loader/src/main/java/org/apache/directory/shared/schema/ ldap-schema-loader/src/test/java/org/apache/directory/server/schema/ ldap/src/main/java/org/apache/directory/shared/...
Date Sun, 20 Dec 2009 01:23:20 GMT
Author: elecharny
Date: Sun Dec 20 01:23:19 2009
New Revision: 892548

URL: http://svn.apache.org/viewvc?rev=892548&view=rev
Log:
o Implemented the unload() methods in SchemaManager
o Update the dependencies hen a schema is added or removed
o Fixed some potential NPE
o If a schema does not exist while trying to load/unload it, trhow an exception now
o Added a removeSchema() method in SchemaLoader
o Fix the schemaLoader tests
o Return only the enabled schema in getAllEnabled() method

Modified:
    directory/shared/branches/shared-schema/ldap-schema-loader/src/main/java/org/apache/directory/shared/schema/DefaultSchemaManager.java
    directory/shared/branches/shared-schema/ldap-schema-loader/src/test/java/org/apache/directory/server/schema/SchemaManagerLoadTest.java
    directory/shared/branches/shared-schema/ldap-schema-loader/src/test/java/org/apache/directory/server/schema/SchemaManagerLoadWithDepsTest.java
    directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/schema/SchemaManager.java
    directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/schema/registries/AbstractSchemaLoader.java
    directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/schema/registries/SchemaLoader.java

Modified: directory/shared/branches/shared-schema/ldap-schema-loader/src/main/java/org/apache/directory/shared/schema/DefaultSchemaManager.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-schema/ldap-schema-loader/src/main/java/org/apache/directory/shared/schema/DefaultSchemaManager.java?rev=892548&r1=892547&r2=892548&view=diff
==============================================================================
--- directory/shared/branches/shared-schema/ldap-schema-loader/src/main/java/org/apache/directory/shared/schema/DefaultSchemaManager.java
(original)
+++ directory/shared/branches/shared-schema/ldap-schema-loader/src/main/java/org/apache/directory/shared/schema/DefaultSchemaManager.java
Sun Dec 20 01:23:19 2009
@@ -21,6 +21,8 @@
 
 
 import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -109,6 +111,9 @@
 
     /** the normalized name for the schema modification attributes */
     private LdapDN schemaModificationAttributesDN;
+    
+    /** A Map containing all the schema being dependent from a schema */
+    private Map<String, Set<String>> schemaDependences = new HashMap<String,
Set<String>>();
 
     /** A flag indicating that the SchemaManager is relaxed or not */
     private boolean isRelaxed = STRICT;
@@ -128,7 +133,7 @@
         // Default to the the root (one schemaManager for all the entries
         namingContext = LdapDN.EMPTY_LDAPDN;
         this.schemaLoader = loader;
-        errors = null;
+        errors = new ArrayList<Throwable>();
         registries = new Registries( this );
         factory = new SchemaEntityFactory();
         isRelaxed = STRICT;
@@ -145,7 +150,7 @@
     {
         this.namingContext = namingContext;
         this.schemaLoader = loader;
-        errors = null;
+        errors = new ArrayList<Throwable>();
         registries = new Registries( this );
         factory = new SchemaEntityFactory();
         isRelaxed = STRICT;
@@ -188,7 +193,16 @@
 
         for ( String schemaName : schemas )
         {
-            schemaArray[n++] = schemaLoader.getSchema( schemaName );
+            Schema schema = schemaLoader.getSchema( schemaName );
+            
+            if ( schema != null )
+            {
+                schemaArray[n++] = schema;
+            }
+            else
+            {
+                throw new LdapOperationNotSupportedException( "Cannot load the unknown schema
" + schemaName, ResultCodeEnum.UNWILLING_TO_PERFORM );
+            }
         }
 
         return schemaArray;
@@ -234,9 +248,25 @@
         {
             registries.delete( errors, schemaObject );
         }
+    }
 
-        // TODO Add some listener handling at this point
-        //notifyListenerOrRegistries( schema, registries );
+    
+    /**
+     * Tells if there are schemaObjects for a given schema from the registries
+     */
+    private boolean hasSchemaObjects( Schema schema, Registries registries ) throws Exception
+    {
+        Map<String, Set<SchemaObjectWrapper>> schemaObjects = registries.getObjectBySchemaName();
+        Set<SchemaObjectWrapper> content = schemaObjects.get( StringTools.toLowerCase(
schema.getSchemaName() ) );
+
+        if ( ( content == null ) || content.isEmpty() )
+        {
+            return false;
+        }
+        else
+        {
+            return true;
+        }
     }
 
 
@@ -490,6 +520,11 @@
      */
     public boolean load( Schema... schemas ) throws Exception
     {
+        if ( schemas.length == 0 )
+        {
+            return true;
+        }
+        
         boolean loaded = false;
 
         // Reset the errors if not null
@@ -525,6 +560,28 @@
                 for ( Schema schema : schemas )
                 {
                     load( registries, schema );
+                    
+                    // Update the schema dependences if needed
+                    
+                    if ( schema.getDependencies() != null )
+                    {
+                        for ( String dep : schema.getDependencies() )
+                        {
+                            Set<String> deps = schemaDependences.get( dep );
+                            
+                            if ( deps == null )
+                            {
+                                deps = new HashSet<String>();
+                                deps.add( schema.getSchemaName() );
+                            }
+                            
+                            // Replace the dependences
+                            schemaDependences.put( dep, deps );
+                        }
+                    }
+                    
+                    // add the schema to the schemaLoader
+                    schemaLoader.addSchema( schema );
                 }
 
                 // Build the cross references
@@ -547,6 +604,11 @@
      */
     public boolean load( String... schemaNames ) throws Exception
     {
+        if ( schemaNames.length == 0 )
+        {
+            return true;
+        }
+        
         Schema[] schemas = toArray( schemaNames );
 
         return load( schemas );
@@ -581,10 +643,9 @@
         {
             if ( registries.isDisabledAccepted() )
             {
-                LOG.info( "Loading {} schema: \n{}", schema.getSchemaName(), schema );
+                LOG.info( "Loading {} disbaled schema: \n{}", schema.getSchemaName(), schema
);
 
                 registries.schemaLoaded( schema );
-
                 addSchemaObjects( schema, registries );
             }
             else
@@ -594,7 +655,7 @@
         }
         else
         {
-            LOG.info( "Loading {} schema: \n{}", schema.getSchemaName(), schema );
+            LOG.info( "Loading {} enabled schema: \n{}", schema.getSchemaName(), schema );
 
             registries.schemaLoaded( schema );
             addSchemaObjects( schema, registries );
@@ -1087,20 +1148,81 @@
     /**
      * {@inheritDoc}
      */
-    public boolean unload( Schema... schemas )
+    public boolean unload( Schema... schemas ) throws Exception
     {
-        // TODO Auto-generated method stub
-        return false;
+        boolean unloaded = false;
+
+        // Reset the errors if not null
+        if ( errors != null )
+        {
+            errors.clear();
+        }
+        
+        // Work on a cloned and relaxed registries
+        Registries clonedRegistries = cloneRegistries();
+        clonedRegistries.setRelaxed();
+
+        // Load the schemas
+        for ( Schema schema : schemas )
+        {
+            unload( clonedRegistries, schema );
+        }
+
+        // Build the cross references
+        errors = clonedRegistries.buildReferences();
+
+        if ( errors.isEmpty() )
+        {
+            // Ok no errors. Check the registries now
+            errors = clonedRegistries.checkRefInteg();
+
+            if ( errors.isEmpty() )
+            {
+                // We are golden : let's apply the schema in the real registries
+                registries.setRelaxed();
+
+                // Load the schemas
+                for ( Schema schema : schemas )
+                {
+                    unload( registries, schema );
+                    
+                    // Update the schema dependences
+                    for ( String dep : schema.getDependencies() )
+                    {
+                        Set<String> deps = schemaDependences.get( dep );
+                        
+                        if ( deps != null )
+                        {
+                            deps.remove( schema.getSchemaName() );
+                        }
+                    }
+                    
+                    schemaLoader.removeSchema( schema );
+                }
+
+                // Build the cross references
+                errors = registries.buildReferences();
+                registries.setStrict();
+
+                unloaded = true;
+            }
+        }
+
+        // clear the cloned registries
+        clonedRegistries.clear();
+
+        return unloaded;
     }
 
 
     /**
      * {@inheritDoc}
      */
-    public boolean unload( String... schemas )
+    public boolean unload( String... schemaNames ) throws Exception
     {
-        // TODO Auto-generated method stub
-        return false;
+        Schema[] schemas = toArray( schemaNames );
+
+        return unload( schemas );
     }
 
 
@@ -1855,6 +1977,15 @@
         return !isRelaxed;
     }
 
+    
+    /**
+     * {@inheritDoc}
+     */
+    public Set<String> listDependentSchemaNames( String schemaName )
+    {
+        return schemaDependences.get( schemaName );
+    }
+
 
     /**
      * Change the SchemaManager to a relaxed mode, where invalid SchemaObjects

Modified: directory/shared/branches/shared-schema/ldap-schema-loader/src/test/java/org/apache/directory/server/schema/SchemaManagerLoadTest.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-schema/ldap-schema-loader/src/test/java/org/apache/directory/server/schema/SchemaManagerLoadTest.java?rev=892548&r1=892547&r2=892548&view=diff
==============================================================================
--- directory/shared/branches/shared-schema/ldap-schema-loader/src/test/java/org/apache/directory/server/schema/SchemaManagerLoadTest.java
(original)
+++ directory/shared/branches/shared-schema/ldap-schema-loader/src/test/java/org/apache/directory/server/schema/SchemaManagerLoadTest.java
Sun Dec 20 01:23:19 2009
@@ -25,11 +25,13 @@
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.io.File;
 import java.io.IOException;
 
 import org.apache.commons.io.FileUtils;
+import org.apache.directory.shared.ldap.exception.LdapOperationNotSupportedException;
 import org.apache.directory.shared.ldap.schema.SchemaManager;
 import org.apache.directory.shared.ldap.schema.ldif.extractor.SchemaLdifExtractor;
 import org.apache.directory.shared.ldap.schema.registries.DefaultSchema;
@@ -548,7 +550,15 @@
         SchemaManager schemaManager = new DefaultSchemaManager( loader );
 
         assertTrue( schemaManager.load( "system" ) );
-        assertTrue( schemaManager.load( "bad" ) );
+        try
+        {
+            schemaManager.loadWithDeps( "bad" );
+            fail();
+        }
+        catch ( LdapOperationNotSupportedException lonse )
+        {
+            // expected
+        }
 
         assertTrue( schemaManager.getErrors().isEmpty() );
         assertEquals( 38, schemaManager.getAttributeTypeRegistry().size() );
@@ -606,23 +616,32 @@
         SchemaManager schemaManager = new DefaultSchemaManager( loader );
 
         assertTrue( schemaManager.load( "system" ) );
-        assertTrue( schemaManager.load( "core", "bad", "cosine", "InetOrgPerson" ) );
+
+        try
+        {
+            assertFalse( schemaManager.load( "core", "bad", "cosine", "InetOrgPerson" ) );
+            fail();
+        }
+        catch ( LdapOperationNotSupportedException lonse )
+        {
+            // expected
+        }
 
         assertTrue( schemaManager.getErrors().isEmpty() );
-        assertEquals( 142, schemaManager.getAttributeTypeRegistry().size() );
+        assertEquals( 38, schemaManager.getAttributeTypeRegistry().size() );
         assertEquals( 35, schemaManager.getComparatorRegistry().size() );
         assertEquals( 35, schemaManager.getMatchingRuleRegistry().size() );
         assertEquals( 35, schemaManager.getNormalizerRegistry().size() );
-        assertEquals( 50, schemaManager.getObjectClassRegistry().size() );
+        assertEquals( 9, schemaManager.getObjectClassRegistry().size() );
         assertEquals( 59, schemaManager.getSyntaxCheckerRegistry().size() );
         assertEquals( 59, schemaManager.getLdapSyntaxRegistry().size() );
-        assertEquals( 286, schemaManager.getGlobalOidRegistry().size() );
+        assertEquals( 141, schemaManager.getGlobalOidRegistry().size() );
 
-        assertEquals( 4, schemaManager.getRegistries().getLoadedSchemas().size() );
+        assertEquals( 1, schemaManager.getRegistries().getLoadedSchemas().size() );
         assertNotNull( schemaManager.getRegistries().getLoadedSchema( "system" ) );
-        assertNotNull( schemaManager.getRegistries().getLoadedSchema( "core" ) );
-        assertNotNull( schemaManager.getRegistries().getLoadedSchema( "cosine" ) );
-        assertNotNull( schemaManager.getRegistries().getLoadedSchema( "InetOrgPerson" ) );
+        assertNull( schemaManager.getRegistries().getLoadedSchema( "core" ) );
+        assertNull( schemaManager.getRegistries().getLoadedSchema( "cosine" ) );
+        assertNull( schemaManager.getRegistries().getLoadedSchema( "InetOrgPerson" ) );
     }
 
 

Modified: directory/shared/branches/shared-schema/ldap-schema-loader/src/test/java/org/apache/directory/server/schema/SchemaManagerLoadWithDepsTest.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-schema/ldap-schema-loader/src/test/java/org/apache/directory/server/schema/SchemaManagerLoadWithDepsTest.java?rev=892548&r1=892547&r2=892548&view=diff
==============================================================================
--- directory/shared/branches/shared-schema/ldap-schema-loader/src/test/java/org/apache/directory/server/schema/SchemaManagerLoadWithDepsTest.java
(original)
+++ directory/shared/branches/shared-schema/ldap-schema-loader/src/test/java/org/apache/directory/server/schema/SchemaManagerLoadWithDepsTest.java
Sun Dec 20 01:23:19 2009
@@ -24,11 +24,13 @@
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.io.File;
 import java.io.IOException;
 
 import org.apache.commons.io.FileUtils;
+import org.apache.directory.shared.ldap.exception.LdapOperationNotSupportedException;
 import org.apache.directory.shared.ldap.schema.SchemaManager;
 import org.apache.directory.shared.ldap.schema.ldif.extractor.SchemaLdifExtractor;
 import org.apache.directory.shared.ldap.schema.registries.DefaultSchema;
@@ -415,7 +417,15 @@
         LdifSchemaLoader loader = new LdifSchemaLoader( schemaRepository );
         SchemaManager schemaManager = new DefaultSchemaManager( loader );
 
-        schemaManager.loadWithDeps( "bad" );
+        try
+        {
+            schemaManager.loadWithDeps( "bad" );
+            fail();
+        }
+        catch ( LdapOperationNotSupportedException lonse )
+        {
+            // expected
+        }
 
         assertTrue( schemaManager.getErrors().isEmpty() );
         assertEquals( 0, schemaManager.getAttributeTypeRegistry().size() );
@@ -469,23 +479,32 @@
         LdifSchemaLoader loader = new LdifSchemaLoader( schemaRepository );
         SchemaManager schemaManager = new DefaultSchemaManager( loader );
 
-        schemaManager.loadWithDeps( "core", "bad", "InetOrgPerson" );
+        try
+        {
+            schemaManager.loadWithDeps( "core", "bad", "InetOrgPerson" );
+            fail();
+        }
+        catch ( LdapOperationNotSupportedException lonse )
+        {
+            // expected
+        }
 
+        // No SchemaObject should be loaded as we had an error
         assertTrue( schemaManager.getErrors().isEmpty() );
-        assertEquals( 142, schemaManager.getAttributeTypeRegistry().size() );
-        assertEquals( 35, schemaManager.getComparatorRegistry().size() );
-        assertEquals( 35, schemaManager.getMatchingRuleRegistry().size() );
-        assertEquals( 35, schemaManager.getNormalizerRegistry().size() );
-        assertEquals( 50, schemaManager.getObjectClassRegistry().size() );
-        assertEquals( 59, schemaManager.getSyntaxCheckerRegistry().size() );
-        assertEquals( 59, schemaManager.getLdapSyntaxRegistry().size() );
-        assertEquals( 286, schemaManager.getGlobalOidRegistry().size() );
+        assertEquals( 0, schemaManager.getAttributeTypeRegistry().size() );
+        assertEquals( 0, schemaManager.getComparatorRegistry().size() );
+        assertEquals( 0, schemaManager.getMatchingRuleRegistry().size() );
+        assertEquals( 0, schemaManager.getNormalizerRegistry().size() );
+        assertEquals( 0, schemaManager.getObjectClassRegistry().size() );
+        assertEquals( 0, schemaManager.getSyntaxCheckerRegistry().size() );
+        assertEquals( 0, schemaManager.getLdapSyntaxRegistry().size() );
+        assertEquals( 0, schemaManager.getGlobalOidRegistry().size() );
 
-        assertEquals( 4, schemaManager.getRegistries().getLoadedSchemas().size() );
-        assertNotNull( schemaManager.getRegistries().getLoadedSchema( "system" ) );
-        assertNotNull( schemaManager.getRegistries().getLoadedSchema( "core" ) );
-        assertNotNull( schemaManager.getRegistries().getLoadedSchema( "cosine" ) );
-        assertNotNull( schemaManager.getRegistries().getLoadedSchema( "InetOrgPerson" ) );
+        assertEquals( 0, schemaManager.getRegistries().getLoadedSchemas().size() );
+        assertNull( schemaManager.getRegistries().getLoadedSchema( "system" ) );
+        assertNull( schemaManager.getRegistries().getLoadedSchema( "core" ) );
+        assertNull( schemaManager.getRegistries().getLoadedSchema( "cosine" ) );
+        assertNull( schemaManager.getRegistries().getLoadedSchema( "InetOrgPerson" ) );
     }
 
 

Modified: directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/schema/SchemaManager.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/schema/SchemaManager.java?rev=892548&r1=892547&r2=892548&view=diff
==============================================================================
--- directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/schema/SchemaManager.java
(original)
+++ directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/schema/SchemaManager.java
Sun Dec 20 01:23:19 2009
@@ -22,6 +22,7 @@
 
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import javax.naming.NamingException;
 
@@ -240,7 +241,7 @@
      * @param schemas The list of Schema to unload
      * @return True if all the schemas have been unloaded
      */
-    boolean unload( Schema... schemas );
+    boolean unload( Schema... schemas ) throws Exception;
 
 
     /**
@@ -249,7 +250,7 @@
      * @param schemas The list of Schema to unload
      * @return True if all the schemas have been unloaded
      */
-    boolean unload( String... schemas );
+    boolean unload( String... schemas ) throws Exception;
 
 
     //---------------------------------------------------------------------------------
@@ -792,4 +793,13 @@
      * @return true if the schema is laoded
      */
     boolean isSchemaLoaded( String schemaName );
+    
+    
+    /**
+     * Get the list of Schema names which has the given schema name as a dependence
+     *
+     * @param schemaName The Schema name for which we want to get the list of dependent schemas
+     * @return The list of dependent schemas
+     */
+    Set<String> listDependentSchemaNames( String schemaName );
 }

Modified: directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/schema/registries/AbstractSchemaLoader.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/schema/registries/AbstractSchemaLoader.java?rev=892548&r1=892547&r2=892548&view=diff
==============================================================================
--- directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/schema/registries/AbstractSchemaLoader.java
(original)
+++ directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/schema/registries/AbstractSchemaLoader.java
Sun Dec 20 01:23:19 2009
@@ -88,6 +88,25 @@
      */
     public final Collection<Schema> getAllEnabled() throws Exception
     {
+        Collection<Schema> enabledSchemas = new ArrayList<Schema>();
+        
+        for ( Schema schema : schemaMap.values() )
+        {
+            if ( schema.isEnabled() )
+            {
+                enabledSchemas.add( schema );
+            }
+        }
+        
+        return enabledSchemas;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public final Collection<Schema> getAllSchemas() throws Exception
+    {
         return schemaMap.values();
     }
 
@@ -97,7 +116,25 @@
      */
     public Schema getSchema( String schemaName )
     {
-        return this.schemaMap.get( StringTools.toLowerCase( schemaName ) );
+        return schemaMap.get( StringTools.toLowerCase( schemaName ) );
+    }
+    
+    
+    /**
+     * {@inheritDoc}
+     */
+    public void addSchema( Schema schema )
+    {
+        schemaMap.put( schema.getSchemaName(), schema );
+    }
+    
+    
+    /**
+     * {@inheritDoc}
+     */
+    public void removeSchema( Schema schema )
+    {
+        schemaMap.remove( schema.getSchemaName() );
     }
 
 

Modified: directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/schema/registries/SchemaLoader.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/schema/registries/SchemaLoader.java?rev=892548&r1=892547&r2=892548&view=diff
==============================================================================
--- directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/schema/registries/SchemaLoader.java
(original)
+++ directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/schema/registries/SchemaLoader.java
Sun Dec 20 01:23:19 2009
@@ -312,7 +312,26 @@
      */
     Collection<Schema> getAllEnabled() throws Exception;
 
+    
+    /**
+     * @return the list of all schemas
+     * @throws Exception TODO
+     */
+    Collection<Schema> getAllSchemas() throws Exception;
+
+    
+    /**
+     * Add a new schema to the schema's list
+     */
+    public void addSchema( Schema schema );
+
+    
+    /**
+     * Remove a schema from the schema's list
+     */
+    public void removeSchema( Schema schema );
 
+    
     /**
      * TODO : get rid of this here !
      * @dao the DAO



Mime
View raw message