directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gokt...@apache.org
Subject svn commit: r1336609 - /directory/shared/branches/shared-osgi/ldap/schema/data/src/main/java/org/apache/directory/shared/ldap/schemaloader/SchemaEntityFactory.java
Date Thu, 10 May 2012 11:36:41 GMT
Author: gokturk
Date: Thu May 10 11:36:41 2012
New Revision: 1336609

URL: http://svn.apache.org/viewvc?rev=1336609&view=rev
Log:
Changed to use SchemaElementsManager to load schema elements using IPojo in OSGI environment

Modified:
    directory/shared/branches/shared-osgi/ldap/schema/data/src/main/java/org/apache/directory/shared/ldap/schemaloader/SchemaEntityFactory.java

Modified: directory/shared/branches/shared-osgi/ldap/schema/data/src/main/java/org/apache/directory/shared/ldap/schemaloader/SchemaEntityFactory.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-osgi/ldap/schema/data/src/main/java/org/apache/directory/shared/ldap/schemaloader/SchemaEntityFactory.java?rev=1336609&r1=1336608&r2=1336609&view=diff
==============================================================================
--- directory/shared/branches/shared-osgi/ldap/schema/data/src/main/java/org/apache/directory/shared/ldap/schemaloader/SchemaEntityFactory.java
(original)
+++ directory/shared/branches/shared-osgi/ldap/schema/data/src/main/java/org/apache/directory/shared/ldap/schemaloader/SchemaEntityFactory.java
Thu May 10 11:36:41 2012
@@ -28,6 +28,8 @@ import java.util.Set;
 
 import org.apache.directory.shared.asn1.util.Oid;
 import org.apache.directory.shared.i18n.I18n;
+import org.apache.directory.shared.ipojo.helpers.OSGIHelper;
+import org.apache.directory.shared.ipojo.schema.SchemaElementsManager;
 import org.apache.directory.shared.ldap.model.constants.MetaSchemaConstants;
 import org.apache.directory.shared.ldap.model.constants.SchemaConstants;
 import org.apache.directory.shared.ldap.model.entry.Attribute;
@@ -85,6 +87,9 @@ public class SchemaEntityFactory impleme
 
     /** A special ClassLoader that loads a class from the bytecode attribute */
     private final AttributeClassLoader classLoader;
+    
+    /** SchemaElementsManager class to OSGI load the classes */
+    private static SchemaElementsManager schemaElements = new SchemaElementsManager();
 
 
     /**
@@ -260,40 +265,66 @@ public class SchemaEntityFactory impleme
      * Class load a syntaxChecker instance
      */
     private SyntaxChecker classLoadSyntaxChecker( SchemaManager schemaManager, String oid,
String className,
-        Attribute byteCode )
-        throws Exception
-    {
-        // Try to class load the syntaxChecker
-        Class<?> clazz = null;
-        SyntaxChecker syntaxChecker = null;
-        String byteCodeStr = StringConstants.EMPTY;
-
-        if ( byteCode == null )
+            Attribute byteCode )
+            throws Exception
         {
-            clazz = Class.forName( className );
-        }
-        else
-        {
-            classLoader.setAttribute( byteCode );
-            clazz = classLoader.loadClass( className );
-            byteCodeStr = new String( Base64.encode( byteCode.getBytes() ) );
-        }
+            // Try to class load the syntaxChecker
+            Class<?> clazz = null;
+            SyntaxChecker syntaxChecker = null;
+            String byteCodeStr = StringConstants.EMPTY;
+
+            // In OSGI and in normal JRE execution, we first try to load the element through
'Class.forName()'
+            // This is for speed. There are so much schema elements and letting IPojo manage
every one of them 
+            // is not viable for server start-up time.
+            try
+            {
+                if ( byteCode == null )
+                {
+                    clazz = Class.forName( className );
+                }
+                else
+                {
+                    classLoader.setAttribute( byteCode );
+                    clazz = classLoader.loadClass( className );
+                    byteCodeStr = new String( Base64.encode( byteCode.getBytes() ) );
+                }
 
-        // Create the syntaxChecker instance
-        syntaxChecker = ( SyntaxChecker ) clazz.newInstance();
+                // Create the syntaxChecker instance
+                syntaxChecker = ( SyntaxChecker ) clazz.newInstance();
+            }
+            catch ( ClassNotFoundException e )
+            {
+                // Check if we're in OSGI context and byteCode is null 
+                if ( OSGIHelper.isAPIInOSGIContainer() && byteCode == null )
+                {
+                    // That is the only case we have to OSGI load the class,
+                    // The other cases are the monolithic load cases.
+                    syntaxChecker = schemaElements.getSyntaxChecker( className );
+
+                    if ( syntaxChecker == null )
+                    {
+                        // We couldn't load the syntax checker using IPojo too.
+                        throw e;
+                    }
+                }
+                else
+                {
+                    throw e;
+                }
+            }
 
-        // Update the common fields
-        syntaxChecker.setBytecode( byteCodeStr );
-        syntaxChecker.setFqcn( className );
+            // Update the common fields
+            syntaxChecker.setBytecode( byteCodeStr );
+            syntaxChecker.setFqcn( className );
 
-        // Inject the new OID, as the loaded syntaxChecker might have its own
-        syntaxChecker.setOid( oid );
+            // Inject the new OID, as the loaded syntaxChecker might have its own
+            syntaxChecker.setOid( oid );
 
-        // Inject the SchemaManager for the comparator who needs it
-        syntaxChecker.setSchemaManager( schemaManager );
+            // Inject the SchemaManager for the comparator who needs it
+            syntaxChecker.setSchemaManager( schemaManager );
 
-        return syntaxChecker;
-    }
+            return syntaxChecker;
+        }
 
 
     /**
@@ -393,57 +424,84 @@ public class SchemaEntityFactory impleme
      * Class load a comparator instances
      */
     private LdapComparator<?> classLoadComparator( SchemaManager schemaManager, String
oid, String className,
-        Attribute byteCode ) throws Exception
-    {
-        // Try to class load the comparator
-        LdapComparator<?> comparator = null;
-        Class<?> clazz = null;
-        String byteCodeStr = StringConstants.EMPTY;
-
-        if ( byteCode == null )
-        {
-            clazz = Class.forName( className );
-        }
-        else
-        {
-            classLoader.setAttribute( byteCode );
-            clazz = classLoader.loadClass( className );
-            byteCodeStr = new String( Base64.encode( byteCode.getBytes() ) );
-        }
-
-        // Create the comparator instance. Either we have a no argument constructor,
-        // or we have one which takes an OID. Lets try the one with an OID argument first
-        try
+            Attribute byteCode ) throws Exception
         {
-            Constructor<?> constructor = clazz.getConstructor( new Class[]
-                { String.class } );
-            comparator = ( LdapComparator<?> ) constructor.newInstance( new Object[]
-                { oid } );
-        }
-        catch ( NoSuchMethodException nsme )
-        {
-            // Ok, let's try with the constructor without argument.
-            // In this case, we will have to check that the OID is the same than
-            // the one we got in the Comparator entry
-            clazz.getConstructor();
-            comparator = ( LdapComparator<?> ) clazz.newInstance();
-
-            if ( !comparator.getOid().equals( oid ) )
+            // Try to class load the comparator
+            LdapComparator<?> comparator = null;
+            Class<?> clazz = null;
+            String byteCodeStr = StringConstants.EMPTY;
+
+            // In OSGI and in normal JRE execution, we first try to load the element through
'Class.forName()'
+            // This is for speed. There are so much schema elements and letting IPojo manage
every one of them 
+            // is not viable for server start-up time.
+            try
             {
-                String msg = I18n.err( I18n.ERR_10015, oid, comparator.getOid() );
-                throw new LdapInvalidAttributeValueException( ResultCodeEnum.UNWILLING_TO_PERFORM,
msg, nsme );
+                if ( byteCode == null )
+                {
+                    clazz = Class.forName( className );
+                }
+                else
+                {
+                    classLoader.setAttribute( byteCode );
+                    clazz = classLoader.loadClass( className );
+                    byteCodeStr = new String( Base64.encode( byteCode.getBytes() ) );
+                }
+
+                // Create the comparator instance. Either we have a no argument constructor,
+                // or we have one which takes an OID. Lets try the one with an OID argument
first
+                try
+                {
+                    Constructor<?> constructor = clazz.getConstructor( new Class[]
+                        { String.class } );
+                    comparator = ( LdapComparator<?> ) constructor.newInstance( new
Object[]
+                        { oid } );
+                }
+                catch ( NoSuchMethodException nsme )
+                {
+                    // Ok, let's try with the constructor without argument.
+                    // In this case, we will have to check that the OID is the same than
+                    // the one we got in the Comparator entry
+                    clazz.getConstructor();
+                    comparator = ( LdapComparator<?> ) clazz.newInstance();
+
+                    if ( !comparator.getOid().equals( oid ) )
+                    {
+                        String msg = I18n.err( I18n.ERR_10015, oid, comparator.getOid() );
+                        throw new LdapInvalidAttributeValueException( ResultCodeEnum.UNWILLING_TO_PERFORM,
msg, nsme );
+                    }
+                }
+            }
+            catch ( ClassNotFoundException e )
+            {
+                // Check if we're in OSGI context and byteCode is null 
+                if ( OSGIHelper.isAPIInOSGIContainer() && byteCode == null )
+                {
+                    // That is the only case we have to OSGI load the class,
+                    // The other cases are the monolithic load cases.
+                    // Comparators are also oid bound classes, so we send it too.
+                    comparator = schemaElements.getLdapComparator( className, oid );
+
+                    if ( comparator == null )
+                    {
+                        // We couldn't load the comparator using IPojo too.
+                        throw e;
+                    }
+                }
+                else
+                {
+                    throw e;
+                }
             }
-        }
 
-        // Update the loadable fields
-        comparator.setBytecode( byteCodeStr );
-        comparator.setFqcn( className );
+            // Update the loadable fields
+            comparator.setBytecode( byteCodeStr );
+            comparator.setFqcn( className );
 
-        // Inject the SchemaManager for the comparator who needs it
-        comparator.setSchemaManager( schemaManager );
+            // Inject the SchemaManager for the comparator who needs it
+            comparator.setSchemaManager( schemaManager );
 
-        return comparator;
-    }
+            return comparator;
+        }
 
 
     /**
@@ -543,39 +601,65 @@ public class SchemaEntityFactory impleme
      * Class load a normalizer instances
      */
     private Normalizer classLoadNormalizer( SchemaManager schemaManager, String oid, String
className,
-        Attribute byteCode ) throws Exception
-    {
-        // Try to class load the normalizer
-        Class<?> clazz = null;
-        Normalizer normalizer = null;
-        String byteCodeStr = StringConstants.EMPTY;
-
-        if ( byteCode == null )
+            Attribute byteCode ) throws Exception
         {
-            clazz = Class.forName( className );
-        }
-        else
-        {
-            classLoader.setAttribute( byteCode );
-            clazz = classLoader.loadClass( className );
-            byteCodeStr = new String( Base64.encode( byteCode.getBytes() ) );
-        }
+            // Try to class load the normalizer
+            Class<?> clazz = null;
+            Normalizer normalizer = null;
+            String byteCodeStr = StringConstants.EMPTY;
+
+            // In OSGI and in normal JRE execution, we first try to load the element through
'Class.forName()'
+            // This is for speed. There are so much schema elements and letting IPojo manage
every one of them 
+            // is not viable for server start-up time.
+            try
+            {
+                if ( byteCode == null )
+                {
+                    clazz = Class.forName( className );
+                }
+                else
+                {
+                    classLoader.setAttribute( byteCode );
+                    clazz = classLoader.loadClass( className );
+                    byteCodeStr = new String( Base64.encode( byteCode.getBytes() ) );
+                }
 
-        // Create the normalizer instance
-        normalizer = ( Normalizer ) clazz.newInstance();
+                // Create the normalizer instance
+                normalizer = ( Normalizer ) clazz.newInstance();
+            }
+            catch ( ClassNotFoundException e )
+            {
+                // Check if we're in OSGI context and byteCode is null 
+                if ( OSGIHelper.isAPIInOSGIContainer() && byteCode == null )
+                {
+                    // That is the only case we have to OSGI load the class,
+                    // The other cases are the monolithic load cases.
+                    normalizer = schemaElements.getNormalizer( className );
+
+                    if ( normalizer == null )
+                    {
+                        // We couldn't load the normalizer using IPojo too.
+                        throw e;
+                    }
+                }
+                else
+                {
+                    throw e;
+                }
+            }
 
-        // Update the common fields
-        normalizer.setBytecode( byteCodeStr );
-        normalizer.setFqcn( className );
+            // Update the common fields
+            normalizer.setBytecode( byteCodeStr );
+            normalizer.setFqcn( className );
 
-        // Inject the new OID, as the loaded normalizer might have its own
-        normalizer.setOid( oid );
+            // Inject the new OID, as the loaded normalizer might have its own
+            normalizer.setOid( oid );
 
-        // Inject the SchemaManager for the normalizer who needs it
-        normalizer.setSchemaManager( schemaManager );
+            // Inject the SchemaManager for the normalizer who needs it
+            normalizer.setSchemaManager( schemaManager );
 
-        return normalizer;
-    }
+            return normalizer;
+        }
 
 
     /**



Mime
View raw message