directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gokt...@apache.org
Subject svn commit: r1221695 - in /directory/apacheds/branches/apacheds-osgi/component-hub/src/main/java/org/apache/directory/server/component: hub/ schema/
Date Wed, 21 Dec 2011 12:22:52 GMT
Author: gokturk
Date: Wed Dec 21 12:22:51 2011
New Revision: 1221695

URL: http://svn.apache.org/viewvc?rev=1221695&view=rev
Log:
* ComponentSchemaManager is implemented to manage component schema generation and installation
into raw SchemaPartition.
* DefaultComponentSchemaGenerator is modified to generate generic schemas for every component
type.


Added:
    directory/apacheds/branches/apacheds-osgi/component-hub/src/main/java/org/apache/directory/server/component/hub/ComponentSchemaManager.java
  (with props)
Removed:
    directory/apacheds/branches/apacheds-osgi/component-hub/src/main/java/org/apache/directory/server/component/schema/ComponentOIDGenerator.java
Modified:
    directory/apacheds/branches/apacheds-osgi/component-hub/src/main/java/org/apache/directory/server/component/schema/ADSComponentSchema.java
    directory/apacheds/branches/apacheds-osgi/component-hub/src/main/java/org/apache/directory/server/component/schema/ComponentSchemaGenerator.java
    directory/apacheds/branches/apacheds-osgi/component-hub/src/main/java/org/apache/directory/server/component/schema/DefaultComponentSchemaGenerator.java

Added: directory/apacheds/branches/apacheds-osgi/component-hub/src/main/java/org/apache/directory/server/component/hub/ComponentSchemaManager.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-osgi/component-hub/src/main/java/org/apache/directory/server/component/hub/ComponentSchemaManager.java?rev=1221695&view=auto
==============================================================================
--- directory/apacheds/branches/apacheds-osgi/component-hub/src/main/java/org/apache/directory/server/component/hub/ComponentSchemaManager.java
(added)
+++ directory/apacheds/branches/apacheds-osgi/component-hub/src/main/java/org/apache/directory/server/component/hub/ComponentSchemaManager.java
Wed Dec 21 12:22:51 2011
@@ -0,0 +1,205 @@
+/*
+ *  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.component.hub;
+
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.List;
+
+import org.apache.directory.server.component.ADSComponent;
+import org.apache.directory.server.component.schema.ADSComponentSchema;
+import org.apache.directory.server.component.schema.ComponentSchemaGenerator;
+import org.apache.directory.server.component.schema.DefaultComponentSchemaGenerator;
+import org.apache.directory.server.component.utilities.ADSComponentHelper;
+
+import org.apache.directory.server.core.api.interceptor.context.AddOperationContext;
+import org.apache.directory.server.core.api.interceptor.context.LookupOperationContext;
+import org.apache.directory.server.core.api.schema.SchemaPartition;
+import org.apache.directory.shared.ldap.model.entry.Entry;
+import org.apache.directory.shared.ldap.model.exception.LdapException;
+import org.apache.directory.shared.ldap.model.ldif.LdifEntry;
+import org.apache.directory.shared.ldap.model.ldif.LdifReader;
+import org.apache.directory.shared.ldap.model.name.Dn;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class ComponentSchemaManager
+{
+    /*
+     * Logger
+     */
+    private final Logger LOG = LoggerFactory.getLogger( ComponentSchemaManager.class );
+
+    /*
+     * Schema Partition reference
+     */
+    private SchemaPartition schemaPartition;
+
+    /*
+     * Schema generators
+     */
+    private Dictionary<String, ComponentSchemaGenerator> schemaGenerators;
+
+
+    public ComponentSchemaManager( SchemaPartition schemaPartition )
+    {
+        this.schemaPartition = schemaPartition;
+
+        schemaGenerators = new Hashtable<String, ComponentSchemaGenerator>();
+    }
+
+
+    /**
+     * Adds new schema generator for specified component type.
+     * Keeps the first added generator as default.
+     *
+     * @param componentType component type to register schema generator
+     * @param generator schema generator instance
+     */
+    public void addSchemaGenerator( String componentType, ComponentSchemaGenerator generator
)
+    {
+        if ( schemaGenerators.get( componentType ) == null )
+        {
+            schemaGenerators.put( componentType, generator );
+        }
+    }
+
+
+    /**
+     * Generates and install the schema elements for component to represent it and its instances.
+     *
+     * @param component ADSComponent reference
+     * @throws LdapException
+     */
+    public void setComponentSchema( ADSComponent component ) throws LdapException
+    {
+        ADSComponentSchema schema = generateComponentSchema( component );
+        injectSchemaElements( schema );
+    }
+
+
+    /**
+     * Generates the schema elements for the component using its assigned SchemaGenerator
+     *
+     * @param component ADSComponent reference to generate schema elements for.
+     * @return Generated schema elements as ADSComponentSchema reference.
+     */
+    private ADSComponentSchema generateComponentSchema( ADSComponent component )
+    {
+        String componentType = component.getComponentType();
+
+        ComponentSchemaGenerator generator = schemaGenerators.get( componentType );
+        if ( generator == null )
+        {
+            generator = new DefaultComponentSchemaGenerator();
+        }
+
+        ADSComponentSchema schema = generator.generateADSComponentSchema( component );
+
+        return schema;
+    }
+
+
+    /**
+     * Injects schema elements into SchemaPartition.
+     *
+     * @param schema ADSComponentSchema reference to inject into SchemaPartition
+     * @throws LdapException
+     */
+    private void injectSchemaElements( ADSComponentSchema schema ) throws LdapException
+    {
+        if ( !schemaBaseExists( schema ) )
+        {
+            createSchemaBase( schema );
+        }
+
+        List<LdifEntry> schemaElements = schema.getSchemaElements();
+
+        for ( LdifEntry le : schemaElements )
+        {
+            AddOperationContext addContext = new AddOperationContext( null, le.getEntry()
);
+            schemaPartition.add( addContext );
+        }
+    }
+
+
+    /**
+     * Checks if the base schema element is exist to hold provided schema's elements.
+     *
+     * @param schema ADSComponentSchema reference to check its parent schema
+     * @return whether base schema exists or not
+     */
+    private boolean schemaBaseExists( ADSComponentSchema schema )
+    {
+        LookupOperationContext luc = new LookupOperationContext( null );
+        try
+        {
+            luc.setDn( new Dn( schema.getParentSchemaDn() ) );
+            Entry e = schemaPartition.lookup( luc );
+
+            if ( e != null )
+            {
+                return true;
+            }
+        }
+        catch ( LdapException e )
+        {
+            LOG.info( "Error while checking base schema element" );
+            e.printStackTrace();
+        }
+        return false;
+    }
+
+
+    /**
+     * It install the base schema for the component.
+     * PS:(It is first considered to be unique for every component type,
+     * but now it is merged under one schema. So that's why we're using one resource for
all of them.)
+     * 
+     * TODO Fix base schema related references later. Will be more meaningfull when we merge
component-hubs schemas and constants
+     * into ApacheDS's own configuration.
+     *  
+     * @param schema ADSComponentSchema reference.
+     * @throws LdapException
+     */
+    private void createSchemaBase( ADSComponentSchema schema ) throws LdapException
+    {
+        try
+        {
+            LdifReader reader = new LdifReader( this.getClass().getResourceAsStream( "componenthub.ldif"
) );
+
+            for ( LdifEntry le : reader )
+            {
+                AddOperationContext addContext = new AddOperationContext( null, le.getEntry()
);
+                schemaPartition.add( addContext );
+            }
+        }
+        catch ( LdapException e )
+        {
+            LOG.info( "Error while injecting base componenthub schema" );
+            e.printStackTrace();
+
+            throw e;
+        }
+
+    }
+}

Propchange: directory/apacheds/branches/apacheds-osgi/component-hub/src/main/java/org/apache/directory/server/component/hub/ComponentSchemaManager.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: directory/apacheds/branches/apacheds-osgi/component-hub/src/main/java/org/apache/directory/server/component/schema/ADSComponentSchema.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-osgi/component-hub/src/main/java/org/apache/directory/server/component/schema/ADSComponentSchema.java?rev=1221695&r1=1221694&r2=1221695&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-osgi/component-hub/src/main/java/org/apache/directory/server/component/schema/ADSComponentSchema.java
(original)
+++ directory/apacheds/branches/apacheds-osgi/component-hub/src/main/java/org/apache/directory/server/component/schema/ADSComponentSchema.java
Wed Dec 21 12:22:51 2011
@@ -42,7 +42,7 @@ public class ADSComponentSchema
     /*
      * Schema name which @schemaElements will go under.
      */
-    private String parentSchemaName = null;
+    private String parentSchemaDn = null;
 
     /*
      * The object class which will be used to represent instances of the component.
@@ -52,7 +52,7 @@ public class ADSComponentSchema
 
     public ADSComponentSchema( String parentSchema, List<LdifEntry> elements, String
oc )
     {
-        parentSchemaName = parentSchema;
+        parentSchemaDn = parentSchema;
         schemaElements = elements;
         objectClassForComponent = oc;
     }
@@ -63,9 +63,9 @@ public class ADSComponentSchema
      *
      * @return name of the schema
      */
-    public String getParentSchemaName()
+    public String getParentSchemaDn()
     {
-        return parentSchemaName;
+        return parentSchemaDn;
     }
 
 

Modified: directory/apacheds/branches/apacheds-osgi/component-hub/src/main/java/org/apache/directory/server/component/schema/ComponentSchemaGenerator.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-osgi/component-hub/src/main/java/org/apache/directory/server/component/schema/ComponentSchemaGenerator.java?rev=1221695&r1=1221694&r2=1221695&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-osgi/component-hub/src/main/java/org/apache/directory/server/component/schema/ComponentSchemaGenerator.java
(original)
+++ directory/apacheds/branches/apacheds-osgi/component-hub/src/main/java/org/apache/directory/server/component/schema/ComponentSchemaGenerator.java
Wed Dec 21 12:22:51 2011
@@ -20,7 +20,7 @@
 package org.apache.directory.server.component.schema;
 
 
-import org.apache.felix.ipojo.Factory;
+import org.apache.directory.server.component.ADSComponent;
 
 
 /**
@@ -34,12 +34,10 @@ public interface ComponentSchemaGenerato
      * Generates a schema for representing all of factory's configurables.
      * Returned schema is in right order to add it to LDAP without any sorting.
      * 
-     * If factory does not need a custom schema, it does not generate a schema for
-     * it, just returns the name of stock schema instead.
      *
-     * @param factory Factory reference to generate schema for.
+     * @param component ADSComponent reference to generate schema for.
      * @return Schema in the form of LdifEntry list.
      */
-    public ADSComponentSchema generateADSComponentSchema( Factory factory );
+    public ADSComponentSchema generateADSComponentSchema( ADSComponent component );
 
 }

Modified: directory/apacheds/branches/apacheds-osgi/component-hub/src/main/java/org/apache/directory/server/component/schema/DefaultComponentSchemaGenerator.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-osgi/component-hub/src/main/java/org/apache/directory/server/component/schema/DefaultComponentSchemaGenerator.java?rev=1221695&r1=1221694&r2=1221695&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-osgi/component-hub/src/main/java/org/apache/directory/server/component/schema/DefaultComponentSchemaGenerator.java
(original)
+++ directory/apacheds/branches/apacheds-osgi/component-hub/src/main/java/org/apache/directory/server/component/schema/DefaultComponentSchemaGenerator.java
Wed Dec 21 12:22:51 2011
@@ -23,7 +23,8 @@ package org.apache.directory.server.comp
 import java.util.ArrayList;
 import java.util.List;
 
-import org.apache.directory.server.component.utilities.IPojoFactoryHelper;
+import org.apache.directory.server.component.ADSComponent;
+import org.apache.directory.server.component.utilities.ADSComponentHelper;
 import org.apache.directory.server.component.utilities.ADSConstants;
 import org.apache.directory.shared.ldap.model.exception.LdapInvalidAttributeValueException;
 import org.apache.directory.shared.ldap.model.exception.LdapInvalidDnException;
@@ -39,19 +40,20 @@ public class DefaultComponentSchemaGener
 {
     private final Logger LOG = LoggerFactory.getLogger( DefaultComponentSchemaGenerator.class
);
 
-    private final String ADS_USER_COMPONENTS_SCHEMA_DN = "cn=usercomponents,ou=schema";
-    private final String ADS_USER_COMPONENTS_SCHEMA_NAME = "usercomponents";
-
 
     @Override
-    public ADSComponentSchema generateADSComponentSchema( Factory componentFactory )
+    public ADSComponentSchema generateADSComponentSchema( ADSComponent component )
     {
+        Factory componentFactory = component.getFactory();
+
+        String ADS_COMPONENT_SCHEMA_DN = ADSComponentHelper.getSchemaBaseDn( component );
+
         List<LdifEntry> schemaElements = new ArrayList<LdifEntry>();
 
-        String componentName = IPojoFactoryHelper.getComponentName( componentFactory );
+        String componentName = ADSComponentHelper.getComponentName( componentFactory );
 
-        String attribsDn = "ou=attributeTypes," + ADS_USER_COMPONENTS_SCHEMA_DN;
-        String ocsDn = "ou=objectClasses," + ADS_USER_COMPONENTS_SCHEMA_DN;
+        String attribsDn = "ou=attributeTypes," + ADS_COMPONENT_SCHEMA_DN;
+        String ocsDn = "ou=objectClasses," + ADS_COMPONENT_SCHEMA_DN;
 
         //Will hold the m-must attributes while iterating over properties of the component
         List<String> ocAttribs = new ArrayList<String>();
@@ -148,8 +150,10 @@ public class DefaultComponentSchemaGener
                 + componentFactory );
         }
 
-        ADSComponentSchema compSchema = new ADSComponentSchema( ADS_USER_COMPONENTS_SCHEMA_NAME,
-            schemaElements, componentName );
+        ADSComponentSchema compSchema = new ADSComponentSchema(
+            ADSComponentHelper.getSchemaBaseDn( component ),
+            schemaElements,
+            ADSComponentHelper.getComponentObjectClass( component ) );
 
         return compSchema;
     }



Mime
View raw message