directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gokt...@apache.org
Subject svn commit: r1368694 - in /directory/shared/branches/shared-txns: ipojo-manager/ ldap/schema/data/ ldap/schema/data/src/main/java/org/apache/directory/shared/ldap/schemaloader/
Date Thu, 02 Aug 2012 20:31:03 GMT
Author: gokturk
Date: Thu Aug  2 20:31:03 2012
New Revision: 1368694

URL: http://svn.apache.org/viewvc?rev=1368694&view=rev
Log:
Merged with shared-osgi

Added:
    directory/shared/branches/shared-txns/ldap/schema/data/src/main/java/org/apache/directory/shared/ldap/schemaloader/svn-dhZnmj
    directory/shared/branches/shared-txns/ldap/schema/data/src/main/java/org/apache/directory/shared/ldap/schemaloader/svn-p7NY80
Modified:
    directory/shared/branches/shared-txns/ipojo-manager/pom.xml
    directory/shared/branches/shared-txns/ldap/schema/data/pom.xml
    directory/shared/branches/shared-txns/ldap/schema/data/src/main/java/org/apache/directory/shared/ldap/schemaloader/SchemaEntityFactory.java

Modified: directory/shared/branches/shared-txns/ipojo-manager/pom.xml
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-txns/ipojo-manager/pom.xml?rev=1368694&r1=1368693&r2=1368694&view=diff
==============================================================================
--- directory/shared/branches/shared-txns/ipojo-manager/pom.xml (original)
+++ directory/shared/branches/shared-txns/ipojo-manager/pom.xml Thu Aug  2 20:31:03 2012
@@ -24,7 +24,7 @@
   <parent>
     <groupId>org.apache.directory.shared</groupId>
     <artifactId>shared-parent</artifactId>
-    <version>1.0.0-M13-SNAPSHOT</version>
+    <version>1.0.0-M9-SNAPSHOT</version>
   </parent>
   
   <artifactId>shared-ipojo-manager</artifactId>

Modified: directory/shared/branches/shared-txns/ldap/schema/data/pom.xml
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-txns/ldap/schema/data/pom.xml?rev=1368694&r1=1368693&r2=1368694&view=diff
==============================================================================
--- directory/shared/branches/shared-txns/ldap/schema/data/pom.xml (original)
+++ directory/shared/branches/shared-txns/ldap/schema/data/pom.xml Thu Aug  2 20:31:03 2012
@@ -45,11 +45,21 @@
       <groupId>${project.groupId}</groupId>
       <artifactId>shared-i18n</artifactId>
     </dependency>
+    
+    <dependency>
+      <groupId>${project.groupId}</groupId>
+      <artifactId>shared-ldap-model</artifactId>
+    </dependency>
 
     <dependency>
       <groupId>${project.groupId}</groupId>
       <artifactId>shared-ldap-extras-aci</artifactId>
     </dependency>
+    
+    <dependency>
+      <groupId>${project.groupId}</groupId>
+      <artifactId>shared-ipojo-manager</artifactId>
+    </dependency>
 
     <dependency>
       <groupId>commons-io</groupId>
@@ -151,7 +161,8 @@
               org.apache.directory.shared.ldap.model.schema.normalizers,
               org.apache.directory.shared.ldap.model.schema.parsers,
               org.apache.directory.shared.ldap.model.schema.registries,
-              org.apache.directory.shared.ldap.model.schema.syntaxCheckers
+              org.apache.directory.shared.ldap.model.schema.syntaxCheckers,
+              org.apache.directory.server.xdbm
             </Import-Package>
           </instructions>
         </configuration>

Modified: directory/shared/branches/shared-txns/ldap/schema/data/src/main/java/org/apache/directory/shared/ldap/schemaloader/SchemaEntityFactory.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-txns/ldap/schema/data/src/main/java/org/apache/directory/shared/ldap/schemaloader/SchemaEntityFactory.java?rev=1368694&r1=1368693&r2=1368694&view=diff
==============================================================================
--- directory/shared/branches/shared-txns/ldap/schema/data/src/main/java/org/apache/directory/shared/ldap/schemaloader/SchemaEntityFactory.java
(original)
+++ directory/shared/branches/shared-txns/ldap/schema/data/src/main/java/org/apache/directory/shared/ldap/schemaloader/SchemaEntityFactory.java
Thu Aug  2 20:31:03 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;
@@ -76,7 +78,7 @@ public class SchemaEntityFactory impleme
 
     /** The empty string list. */
     private static final List<String> EMPTY_LIST = new ArrayList<String>();
-    
+
     /** The empty string array. */
     private static final String[] EMPTY_ARRAY = new String[]
         {};
@@ -84,6 +86,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();
+
 
     /**
      * Instantiates a new schema entity factory.
@@ -112,7 +117,7 @@ public class SchemaEntityFactory impleme
 
         String oid = mOid.getString();
 
-        if ( !Oid.isOid(oid) )
+        if ( !Oid.isOid( oid ) )
         {
             String msg = I18n.err( I18n.ERR_10006, oid );
             LOG.warn( msg );
@@ -184,7 +189,7 @@ public class SchemaEntityFactory impleme
      */
     private Schema getSchema( String schemaName, Registries registries )
     {
-        if ( Strings.isEmpty(schemaName) )
+        if ( Strings.isEmpty( schemaName ) )
         {
             schemaName = MetaSchemaConstants.SCHEMA_OTHER;
         }
@@ -257,7 +262,8 @@ public class SchemaEntityFactory impleme
     /**
      * Class load a syntaxChecker instance
      */
-    private SyntaxChecker classLoadSyntaxChecker( SchemaManager schemaManager, String oid,
String className, Attribute byteCode )
+    private SyntaxChecker classLoadSyntaxChecker( SchemaManager schemaManager, String oid,
String className,
+        Attribute byteCode )
         throws Exception
     {
         // Try to class load the syntaxChecker
@@ -265,20 +271,46 @@ public class SchemaEntityFactory impleme
         SyntaxChecker syntaxChecker = null;
         String byteCodeStr = StringConstants.EMPTY;
 
-        if ( byteCode == null )
+        // 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
         {
-            clazz = Class.forName( className );
+            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();
         }
-        else
+        catch ( ClassNotFoundException e )
         {
-            classLoader.setAttribute( byteCode );
-            clazz = classLoader.loadClass( className );
-            byteCodeStr = new String( Base64.encode(byteCode.getBytes()) );
+            // 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;
+            }
         }
 
-        // Create the syntaxChecker instance
-        syntaxChecker = ( SyntaxChecker ) clazz.newInstance();
-
         // Update the common fields
         syntaxChecker.setBytecode( byteCodeStr );
         syntaxChecker.setFqcn( className );
@@ -288,7 +320,7 @@ public class SchemaEntityFactory impleme
 
         // Inject the SchemaManager for the comparator who needs it
         syntaxChecker.setSchemaManager( schemaManager );
-        
+
         return syntaxChecker;
     }
 
@@ -333,10 +365,10 @@ public class SchemaEntityFactory impleme
         {
             // Class load the syntaxChecker
             SyntaxChecker syntaxChecker = classLoadSyntaxChecker( schemaManager, oid, className,
byteCode );
-    
+
             // Update the common fields
             setSchemaObjectProperties( syntaxChecker, entry, schema );
-    
+
             // return the resulting syntaxChecker
             return syntaxChecker;
         }
@@ -397,38 +429,65 @@ public class SchemaEntityFactory impleme
         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
+        // 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
         {
-            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 ( byteCode == null )
+            {
+                clazz = Class.forName( className );
+            }
+            else
+            {
+                classLoader.setAttribute( byteCode );
+                clazz = classLoader.loadClass( className );
+                byteCodeStr = new String( Base64.encode( byteCode.getBytes() ) );
+            }
 
-            if ( !comparator.getOid().equals( oid ) )
+            // 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 )
             {
-                String msg = I18n.err( I18n.ERR_10015, oid, comparator.getOid() );
-                throw new LdapInvalidAttributeValueException( ResultCodeEnum.UNWILLING_TO_PERFORM,
msg, 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;
             }
         }
 
@@ -522,7 +581,7 @@ public class SchemaEntityFactory impleme
         {
             // Class load the comparator
             LdapComparator<?> comparator = classLoadComparator( schemaManager, oid,
fqcn, byteCode );
-    
+
             // Update the common fields
             setSchemaObjectProperties( comparator, entry, schema );
 
@@ -547,20 +606,46 @@ public class SchemaEntityFactory impleme
         Normalizer normalizer = null;
         String byteCodeStr = StringConstants.EMPTY;
 
-        if ( byteCode == null )
+        // 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
         {
-            clazz = Class.forName( className );
+            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();
         }
-        else
+        catch ( ClassNotFoundException e )
         {
-            classLoader.setAttribute( byteCode );
-            clazz = classLoader.loadClass( className );
-            byteCodeStr = new String( Base64.encode( byteCode.getBytes() ) );
+            // 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;
+            }
         }
 
-        // Create the normalizer instance
-        normalizer = ( Normalizer ) clazz.newInstance();
-
         // Update the common fields
         normalizer.setBytecode( byteCodeStr );
         normalizer.setFqcn( className );
@@ -653,10 +738,10 @@ public class SchemaEntityFactory impleme
         {
             // Class load the Normalizer
             Normalizer normalizer = classLoadNormalizer( schemaManager, oid, className, byteCode
);
-    
+
             // Update the common fields
             setSchemaObjectProperties( normalizer, entry, schema );
-    
+
             // return the resulting Normalizer
             return normalizer;
         }
@@ -1027,7 +1112,7 @@ public class SchemaEntityFactory impleme
         return mFqcn;
     }
 
-    
+
     /**
      * Process the ByteCode attribute
      */

Added: directory/shared/branches/shared-txns/ldap/schema/data/src/main/java/org/apache/directory/shared/ldap/schemaloader/svn-dhZnmj
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-txns/ldap/schema/data/src/main/java/org/apache/directory/shared/ldap/schemaloader/svn-dhZnmj?rev=1368694&view=auto
==============================================================================
    (empty)

Added: directory/shared/branches/shared-txns/ldap/schema/data/src/main/java/org/apache/directory/shared/ldap/schemaloader/svn-p7NY80
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-txns/ldap/schema/data/src/main/java/org/apache/directory/shared/ldap/schemaloader/svn-p7NY80?rev=1368694&view=auto
==============================================================================
--- directory/shared/branches/shared-txns/ldap/schema/data/src/main/java/org/apache/directory/shared/ldap/schemaloader/svn-p7NY80
(added)
+++ directory/shared/branches/shared-txns/ldap/schema/data/src/main/java/org/apache/directory/shared/ldap/schemaloader/svn-p7NY80
Thu Aug  2 20:31:03 2012
@@ -0,0 +1,671 @@
+/*
+ *  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.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import javax.naming.NamingException;
+import javax.naming.directory.Attribute;
+import javax.naming.directory.Attributes;
+
+import org.apache.directory.server.constants.MetaSchemaConstants;
+import org.apache.directory.server.constants.SystemSchemaConstants;
+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.shared.ldap.schema.*;
+import org.apache.directory.shared.ldap.schema.syntax.SyntaxChecker;
+
+
+/**
+ * Showing how it's done ...
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class SchemaEntityFactory
+{
+    /** Used for looking up the setRegistries(Registries) method */
+    private final static Class[] parameterTypes = new Class[] { Registries.class };
+    /** Used for looking up the setSyntaxOid(String) method */
+    private final static Class[] setOidParameterTypes = new Class[] { String.class };
+    private static final String[] EMPTY = new String[0];
+    
+    /** Used for dependency injection of Registries via setter into schema objects */
+    private final Registries bootstrapRegistries;
+    /** A special ClassLoader that loads a class from the bytecode attribute */
+    private final AttributeClassLoader classLoader;
+    private final AttributeType oidAT;
+    private AttributeType byteCodeAT;
+    
+    
+    public SchemaEntityFactory( Registries bootstrapRegistries ) throws NamingException
+    {
+        this.bootstrapRegistries = bootstrapRegistries;
+        this.classLoader = new AttributeClassLoader();
+        this.oidAT = bootstrapRegistries.getAttributeTypeRegistry().lookup( MetaSchemaConstants.M_OID_AT
);
+        this.byteCodeAT = bootstrapRegistries.getAttributeTypeRegistry().lookup( MetaSchemaConstants.M_BYTECODE_AT
);
+    }
+
+    
+    public Schema getSchema( Attributes entry ) throws NamingException
+    {
+        String name;
+        String owner;
+        String[] dependencies = EMPTY;
+        boolean isDisabled = false;
+        
+        if ( entry == null )
+        {
+            throw new NullPointerException( "entry cannot be null" );
+        }
+        
+        if ( entry.get( SystemSchemaConstants.CN_AT ) == null )
+        {
+            throw new NullPointerException( "entry must have a valid cn attribute" );
+        }
+        name = ( String ) entry.get( SystemSchemaConstants.CN_AT ).get();
+        
+        if ( entry.get( MetaSchemaConstants.M_OWNER_AT ) == null )
+        {
+            throw new NullPointerException( "entry must have a valid " 
+                + MetaSchemaConstants.M_OWNER_AT + " attribute" );
+        }
+        owner = ( String ) entry.get( MetaSchemaConstants.M_OWNER_AT ).get();
+        
+        if ( entry.get( MetaSchemaConstants.M_DISABLED_AT ) != null )
+        {
+            String value = ( String ) entry.get( MetaSchemaConstants.M_DISABLED_AT ).get();
+            value = value.toUpperCase();
+            isDisabled = value.equals( "TRUE" );
+        }
+        
+        if ( entry.get( MetaSchemaConstants.M_DEPENDENCIES_AT ) != null )
+        {
+            Set<String> depsSet = new HashSet<String>();
+            Attribute depsAttr = entry.get( MetaSchemaConstants.M_DEPENDENCIES_AT );
+            for ( int ii = 0; ii < depsAttr.size(); ii++ )
+            {
+               depsSet.add( ( String ) depsAttr.get( ii ) ); 
+            }
+            dependencies = depsSet.toArray( EMPTY );
+        }
+        
+        return new AbstractSchema( name, owner, dependencies, isDisabled ){};
+    }
+    
+    
+    /**
+     * Retrieve and load a syntaxChecker class from the DIT.
+     * 
+     * @param entry the entry to load the syntaxChecker from
+     * @return the loaded SyntaxChecker
+     * @throws NamingException if anything fails during loading
+     */
+    public SyntaxChecker getSyntaxChecker( Attributes entry, Registries targetRegistries
) throws NamingException
+    {
+        if ( entry == null )
+        {
+            throw new NullPointerException( "entry cannot be null" );
+        }
+        
+        if ( entry.get( MetaSchemaConstants.M_FQCN_AT ) == null )
+        {
+            throw new NullPointerException( "entry must have a valid "
+                + MetaSchemaConstants.M_FQCN_AT + " attribute" );
+        }
+        
+        String className = ( String ) entry.get( MetaSchemaConstants.M_FQCN_AT ).get();
+        SyntaxChecker syntaxChecker = null;
+        Class clazz = null;
+
+        Attribute byteCodeAttr = ServerUtils.getAttribute( byteCodeAT, entry );
+        if ( byteCodeAttr == null )
+        {
+            try
+            {
+                clazz = Class.forName( className );
+            }
+            catch ( ClassNotFoundException e )
+            {
+                NamingException ne = new NamingException( "SyntaxChecker class "+ className
+ " was not found" );
+                ne.setRootCause( e );
+                throw ne;
+            }
+        }       
+        else
+        {
+            try
+            {
+                clazz = classLoader.loadClass( className );
+            }
+            catch ( ClassCastException e )
+            {
+                NamingException ne = new NamingException( "Class "+ className + " does not
implement SyntaxChecker" );
+                ne.setRootCause( e );
+                throw ne;
+            }
+            catch ( ClassNotFoundException e )
+            {
+                NamingException ne = new NamingException( "SyntaxChecker class "+ className
+ " was not found" );
+                ne.setRootCause( e );
+                throw ne;
+            }
+        }
+        
+        
+        try
+        {
+            syntaxChecker = ( SyntaxChecker ) clazz.newInstance();
+        }
+        catch ( ClassCastException e )
+        {
+            NamingException ne = new NamingException( "Class "+ className + " does not implement
SyntaxChecker" );
+            ne.setRootCause( e );
+            throw ne;
+        }
+        catch ( InstantiationException e )
+        {
+            NamingException ne = new NamingException( "Failed to instantiate syntaxChecker
class "+ className 
+                + ".\nCheck that a default constructor exists for the class." );
+            ne.setRootCause( e );
+            throw ne;
+        }
+        catch ( IllegalAccessException e )
+        {
+            NamingException ne = new NamingException( "Failed to instantiate syntaxChecker
class "+ className 
+                + ".\nCheck that a **PUBLIC** accessible default constructor exists for the
class." );
+            ne.setRootCause( e );
+            throw ne;
+        }
+
+        // try now before returning to check if we can inject a Registries object
+        injectRegistries( syntaxChecker, targetRegistries );
+        String syntaxOid = ( String ) ServerUtils.getAttribute( oidAT, entry ).get();
+        injectOid( syntaxOid, syntaxChecker );
+        return syntaxChecker;
+    }
+    
+    
+    /**
+     * Retrieve and load a Comparator class from the DIT.
+     * 
+     * @param entry the entry to load the Comparator from
+     * @return the loaded Comparator
+     * @throws NamingException if anything fails during loading
+     */
+    public Comparator getComparator( Attributes entry, Registries targetRegistries ) throws
NamingException
+    {
+        if ( entry == null )
+        {
+            throw new NullPointerException( "entry cannot be null" );
+        }
+        
+        if ( entry.get( MetaSchemaConstants.M_FQCN_AT ) == null )
+        {
+            throw new NullPointerException( "entry must have a valid " 
+                + MetaSchemaConstants.M_FQCN_AT + " attribute" );
+        }
+        
+        String className = ( String ) entry.get( MetaSchemaConstants.M_FQCN_AT ).get();
+        Comparator comparator = null;
+        Class clazz = null;
+
+        if ( entry.get( MetaSchemaConstants.M_BYTECODE_AT ) == null )
+        {
+            try
+            {
+                clazz = Class.forName( className );
+            }
+            catch ( ClassNotFoundException e )
+            {
+                NamingException ne = new NamingException( "Comparator class "+ className
+ " was not found" );
+                ne.setRootCause( e );
+                throw ne;
+            }
+        }       
+        else
+        {
+            try
+            {
+                clazz = classLoader.loadClass( className );
+            }
+            catch ( ClassCastException e )
+            {
+                NamingException ne = new NamingException( "Class "+ className + " does not
implement Comparator" );
+                ne.setRootCause( e );
+                throw ne;
+            }
+            catch ( ClassNotFoundException e )
+            {
+                NamingException ne = new NamingException( "Comparator class "+ className
+ " was not found" );
+                ne.setRootCause( e );
+                throw ne;
+            }
+        }
+        
+        
+        try
+        {
+            comparator = ( Comparator ) clazz.newInstance();
+        }
+        catch ( ClassCastException e )
+        {
+            NamingException ne = new NamingException( "Class "+ className + " does not implement
Comparator" );
+            ne.setRootCause( e );
+            throw ne;
+        }
+        catch ( InstantiationException e )
+        {
+            NamingException ne = new NamingException( "Failed to instantiate comparator class
"+ className 
+                + ".\nCheck that a default constructor exists for the class." );
+            ne.setRootCause( e );
+            throw ne;
+        }
+        catch ( IllegalAccessException e )
+        {
+            NamingException ne = new NamingException( "Failed to instantiate comparator class
"+ className 
+                + ".\nCheck that a **PUBLIC** accessible default constructor exists for the
class." );
+            ne.setRootCause( e );
+            throw ne;
+        }
+
+        // try now before returning to check if we can inject a Registries object
+        injectRegistries( comparator, targetRegistries );
+        return comparator;
+    }
+    
+    
+    /**
+     * Retrieve and load a Normalizer class from the DIT.
+     * 
+     * @param entry the entry to load the Normalizer from
+     * @return the loaded Normalizer
+     * @throws NamingException if anything fails during loading
+     */
+    public Normalizer getNormalizer( Attributes entry, Registries targetRegistries ) throws
NamingException
+    {
+        if ( entry == null )
+        {
+            throw new NullPointerException( "entry cannot be null" );
+        }
+        
+        if ( entry.get( MetaSchemaConstants.M_FQCN_AT ) == null )
+        {
+            throw new NullPointerException( "entry must have a valid " 
+                + MetaSchemaConstants.M_FQCN_AT + " attribute" );
+        }
+        
+        String className = ( String ) entry.get( MetaSchemaConstants.M_FQCN_AT ).get();
+        Normalizer normalizer = null;
+        Class clazz = null;
+
+        if ( entry.get( MetaSchemaConstants.M_BYTECODE_AT ) == null )
+        {
+            try
+            {
+                clazz = Class.forName( className );
+            }
+            catch ( ClassNotFoundException e )
+            {
+                NamingException ne = new NamingException( "Normalizer class "+ className
+ " was not found" );
+                ne.setRootCause( e );
+                throw ne;
+            }
+        }       
+        else
+        {
+            try
+            {
+                clazz = classLoader.loadClass( className );
+            }
+            catch ( ClassCastException e )
+            {
+                NamingException ne = new NamingException( "Class "+ className + " does not
implement Normalizer" );
+                ne.setRootCause( e );
+                throw ne;
+            }
+            catch ( ClassNotFoundException e )
+            {
+                NamingException ne = new NamingException( "Normalizer class "+ className
+ " was not found" );
+                ne.setRootCause( e );
+                throw ne;
+            }
+        }
+        
+        
+        try
+        {
+            normalizer = ( Normalizer ) clazz.newInstance();
+        }
+        catch ( ClassCastException e )
+        {
+            NamingException ne = new NamingException( "Class "+ className + " does not implement
Normalizer" );
+            ne.setRootCause( e );
+            throw ne;
+        }
+        catch ( InstantiationException e )
+        {
+            NamingException ne = new NamingException( "Failed to instantiate normalizer class
"+ className 
+                + ".\nCheck that a default constructor exists for the class." );
+            ne.setRootCause( e );
+            throw ne;
+        }
+        catch ( IllegalAccessException e )
+        {
+            NamingException ne = new NamingException( "Failed to instantiate normalizer class
"+ className 
+                + ".\nCheck that a **PUBLIC** accessible default constructor exists for the
class." );
+            ne.setRootCause( e );
+            throw ne;
+        }
+
+        // try now before returning to check if we can inject a Registries object
+        injectRegistries( normalizer, targetRegistries );
+        return normalizer;
+    }
+    
+    
+    /**
+     * Uses reflection to see if a setRegistries( Registries ) method exists on the
+     * object's class.  If so then the registries are dependency injected into the 
+     * new schema object.
+     * 
+     * @param obj a schema object to have a Registries dependency injected.
+     */
+    private void injectRegistries( Object obj, Registries targetRegistries ) throws NamingException
+    {
+        String className = obj.getClass().getName();
+        
+        try
+        {
+            Method method = obj.getClass().getMethod( "setRegistries", parameterTypes );
+            
+            if ( method == null )
+            {
+                return;
+            }
+            
+            Object[] args = new Object[] { this.bootstrapRegistries };
+            method.invoke( obj, args );
+        }
+        catch ( SecurityException e )
+        {
+            NamingException ne = new NamingException( "SyntaxChecker class "+ className 
+                + " could not have the Registries dependency injected." );
+            ne.setRootCause( e );
+            throw ne;
+        }
+        catch ( NoSuchMethodException e )
+        {
+            // this is ok since not every object may have setRegistries()
+        }
+        catch ( IllegalArgumentException e )
+        {
+            NamingException ne = new NamingException( "SyntaxChecker class "+ className 
+                + " could not have the Registries dependency injected." );
+            ne.setRootCause( e );
+            throw ne;
+        }
+        catch ( IllegalAccessException e )
+        {
+            NamingException ne = new NamingException( "SyntaxChecker class "+ className 
+                + " could not have the Registries dependency injected." );
+            ne.setRootCause( e );
+            throw ne;
+        }
+        catch ( InvocationTargetException e )
+        {
+            NamingException ne = new NamingException( "SyntaxChecker class "+ className 
+                + " could not have the Registries dependency injected." );
+            ne.setRootCause( e );
+            throw ne;
+        }
+    }
+
+
+    /**
+     * Uses reflection to see if a setSyntaxOid( String ) method exists 
+     * on the object's class.  If so then the oid dependency is injected into the 
+     * new SyntaxChecker.
+     * 
+     * @param obj a schema object to have a oid dependency injected.
+     */
+    private void injectOid( String syntaxOid, SyntaxChecker checker ) throws NamingException
+    {
+        String className = checker.getClass().getName();
+        
+        try
+        {
+            Method method = checker.getClass().getMethod( "setSyntaxOid", setOidParameterTypes
);
+            
+            if ( method == null )
+            {
+                return;
+            }
+            
+            Object[] args = new Object[] { syntaxOid};
+            method.invoke( checker, args );
+        }
+        catch ( SecurityException e )
+        {
+            NamingException ne = new NamingException( "SyntaxChecker class "+ className 
+                + " could not have the oid dependency injected." );
+            ne.setRootCause( e );
+            throw ne;
+        }
+        catch ( NoSuchMethodException e )
+        {
+            // this is ok since not every object may have setSyntaxOid()
+        }
+        catch ( IllegalArgumentException e )
+        {
+            NamingException ne = new NamingException( "SyntaxChecker class "+ className 
+                + " could not have the oid dependency injected." );
+            ne.setRootCause( e );
+            throw ne;
+        }
+        catch ( IllegalAccessException e )
+        {
+            NamingException ne = new NamingException( "SyntaxChecker class "+ className 
+                + " could not have the oid dependency injected." );
+            ne.setRootCause( e );
+            throw ne;
+        }
+        catch ( InvocationTargetException e )
+        {
+            NamingException ne = new NamingException( "SyntaxChecker class "+ className 
+                + " could not have the oid dependency injected." );
+            ne.setRootCause( e );
+            throw ne;
+        }
+    }
+
+
+    public Syntax getSyntax( Attributes entry, Registries targetRegistries ) throws NamingException
+    {
+        String oid = ( String ) entry.get( MetaSchemaConstants.M_OID_AT ).get();
+        SyntaxImpl syntax = new SyntaxImpl( oid, targetRegistries.getSyntaxCheckerRegistry()
);
+        
+        if ( entry.get( MetaSchemaConstants.X_HUMAN_READIBLE_AT ) != null )
+        {
+            String val = ( String ) entry.get( MetaSchemaConstants.X_HUMAN_READIBLE_AT ).get();
+            syntax.setHumanReadible( val.toUpperCase().equals( "TRUE" ) );
+        }
+        
+        if ( entry.get( MetaSchemaConstants.M_DESCRIPTION_AT ) != null )
+        {
+            syntax.setDescription( ( String ) entry.get( MetaSchemaConstants.M_DESCRIPTION_AT
).get() ); 
+        }
+        
+        return syntax;
+    }
+
+    
+    public MatchingRule getMatchingRule( Attributes entry, Registries targetRegistries )
throws NamingException
+    {
+        String oid = ( String ) entry.get( MetaSchemaConstants.M_OID_AT ).get();
+        String syntaxOid = ( String ) entry.get( MetaSchemaConstants.M_SYNTAX_AT ).get();
+        MatchingRuleImpl mr = new MatchingRuleImpl( oid, syntaxOid, targetRegistries );
+        setSchemaObjectProperties( mr, entry );
+        return mr;
+    }
+    
+    
+    private String[] getStrings( Attribute attr ) throws NamingException
+    {
+        if ( attr == null )
+        {
+            return EMPTY;
+        }
+        
+        String[] strings = new String[attr.size()];
+        for ( int ii = 0; ii < strings.length; ii++ )
+        {
+            strings[ii] = ( String ) attr.get( ii );
+        }
+        return strings;
+    }
+    
+    
+    public ObjectClass getObjectClass( Attributes entry, Registries targetRegistries ) throws
NamingException
+    {
+        String oid = ( String ) entry.get( MetaSchemaConstants.M_OID_AT ).get();
+        ObjectClassImpl oc = new ObjectClassImpl( oid, targetRegistries );
+        
+        if ( entry.get( MetaSchemaConstants.M_SUP_OBJECT_CLASS_AT ) != null )
+        {
+            oc.setSuperClassOids( getStrings( entry.get( MetaSchemaConstants.M_SUP_OBJECT_CLASS_AT
) ) );
+        }
+        
+        if ( entry.get( MetaSchemaConstants.M_MAY_AT ) != null )
+        {
+            oc.setMayListOids( getStrings( entry.get( MetaSchemaConstants.M_MAY_AT ) ) );
+        }
+        
+        if ( entry.get( MetaSchemaConstants.M_MUST_AT ) != null )
+        {
+            oc.setMustListOids( getStrings( entry.get( MetaSchemaConstants.M_MUST_AT ) )
);
+        }
+        
+        if ( entry.get( MetaSchemaConstants.M_TYPE_OBJECT_CLASS_AT ) != null )
+        {
+            String type = ( String ) entry.get( MetaSchemaConstants.M_TYPE_OBJECT_CLASS_AT
).get();
+            oc.setType( ObjectClassTypeEnum.getClassType( type ) );
+        }
+        else
+        {
+            oc.setType( ObjectClassTypeEnum.STRUCTURAL );
+        }
+        
+        setSchemaObjectProperties( oc, entry );
+        
+        return oc;
+    }
+    
+    
+    public AttributeType getAttributeType( Attributes entry, Registries targetRegistries
) throws NamingException
+    {
+        String oid = ( String ) entry.get( MetaSchemaConstants.M_OID_AT ).get();
+        AttributeTypeImpl at = new AttributeTypeImpl( oid, targetRegistries );
+        setSchemaObjectProperties( at, entry );
+        
+        if ( entry.get( MetaSchemaConstants.M_SYNTAX_AT ) != null )
+        {
+            at.setSyntaxOid( ( String ) entry.get( MetaSchemaConstants.M_SYNTAX_AT ).get()
);
+        }
+        
+        if ( entry.get( MetaSchemaConstants.M_EQUALITY_AT ) != null )
+        {
+            at.setEqualityOid( ( String ) entry.get( MetaSchemaConstants.M_EQUALITY_AT ).get()
);
+        }
+        
+        if ( entry.get( MetaSchemaConstants.M_ORDERING_AT ) != null )
+        {
+            at.setOrderingOid( ( String ) entry.get( MetaSchemaConstants.M_ORDERING_AT ).get()
);
+        }
+        
+        if ( entry.get( MetaSchemaConstants.M_SUBSTR_AT ) != null )
+        {
+            at.setSubstrOid( ( String ) entry.get( MetaSchemaConstants.M_SUBSTR_AT ).get()
);
+        }
+        
+        if ( entry.get( MetaSchemaConstants.M_SUP_ATTRIBUTE_TYPE_AT ) != null )
+        {
+            at.setSuperiorOid( ( String ) entry.get( MetaSchemaConstants.M_SUP_ATTRIBUTE_TYPE_AT
).get() );
+        }
+        
+        if ( entry.get( MetaSchemaConstants.M_COLLECTIVE_AT ) != null )
+        {
+            String val = ( String ) entry.get( MetaSchemaConstants.M_COLLECTIVE_AT ).get();
+            at.setCollective( val.equalsIgnoreCase( "TRUE" ) );
+        }
+        
+        if ( entry.get( MetaSchemaConstants.M_SINGLE_VALUE_AT ) != null )
+        {
+            String val = ( String ) entry.get( MetaSchemaConstants.M_SINGLE_VALUE_AT ).get();
+            at.setSingleValue( val.equalsIgnoreCase( "TRUE" ) );
+        }
+        
+        if ( entry.get( MetaSchemaConstants.M_NO_USER_MODIFICATION_AT ) != null )
+        {
+            String val = ( String ) entry.get( MetaSchemaConstants.M_NO_USER_MODIFICATION_AT
).get();
+            at.setCanUserModify( val.equalsIgnoreCase( "TRUE" ) );
+        }
+        
+        if ( entry.get( MetaSchemaConstants.M_USAGE_AT ) != null )
+        {
+            at.setUsage( UsageEnum.getUsage( ( String ) entry.get( MetaSchemaConstants.M_USAGE_AT
).get() ) );
+        }
+        
+        return at;
+    }
+    
+
+    private void setSchemaObjectProperties( MutableSchemaObject mso, Attributes entry ) throws
NamingException
+    {
+        if ( entry.get( MetaSchemaConstants.M_OBSOLETE_AT ) != null )
+        {
+            String val = ( String ) entry.get( MetaSchemaConstants.M_OBSOLETE_AT ).get();
+            mso.setObsolete( val.equalsIgnoreCase( "TRUE" ) );
+        }
+        
+        if ( entry.get( MetaSchemaConstants.M_DESCRIPTION_AT ) != null )
+        {
+            mso.setDescription( ( String ) entry.get( MetaSchemaConstants.M_DESCRIPTION_AT
).get() ); 
+        }
+
+        Attribute names = entry.get( MetaSchemaConstants.M_NAME_AT );
+        if ( names != null )
+        {
+            List<String> values = new ArrayList<String>();
+            for ( int ii = 0; ii < names.size(); ii++ )
+            {
+                values.add( ( String ) names.get( ii ) );
+            }
+            mso.setNames( values.toArray( EMPTY ) );
+        }
+    }
+}



Mime
View raw message