directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pamarce...@apache.org
Subject svn commit: r1043359 - in /directory/apacheds/branches/server-config-annotations/server-config/src/main/java/org/apache/directory/server/config: ConfigWriter.java Main.java
Date Wed, 08 Dec 2010 11:20:36 GMT
Author: pamarcelot
Date: Wed Dec  8 11:20:35 2010
New Revision: 1043359

URL: http://svn.apache.org/viewvc?rev=1043359&view=rev
Log:
Converted the ConfigWriter to be an instantiable class (no longer static methods).
Added a method to get the converted LDIF entries (without writing them to a file).

Modified:
    directory/apacheds/branches/server-config-annotations/server-config/src/main/java/org/apache/directory/server/config/ConfigWriter.java
    directory/apacheds/branches/server-config-annotations/server-config/src/main/java/org/apache/directory/server/config/Main.java

Modified: directory/apacheds/branches/server-config-annotations/server-config/src/main/java/org/apache/directory/server/config/ConfigWriter.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/server-config-annotations/server-config/src/main/java/org/apache/directory/server/config/ConfigWriter.java?rev=1043359&r1=1043358&r2=1043359&view=diff
==============================================================================
--- directory/apacheds/branches/server-config-annotations/server-config/src/main/java/org/apache/directory/server/config/ConfigWriter.java
(original)
+++ directory/apacheds/branches/server-config-annotations/server-config/src/main/java/org/apache/directory/server/config/ConfigWriter.java
Wed Dec  8 11:20:35 2010
@@ -20,7 +20,9 @@
 package org.apache.directory.server.config;
 
 
+import java.io.File;
 import java.io.FileWriter;
+import java.io.IOException;
 import java.lang.reflect.Field;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -43,31 +45,106 @@ import org.apache.directory.shared.ldap.
 
 
 /**
- * TODO
- *
+ * This class implements a writer for ApacheDS Configuration.
+ * <p>
+ * It can be used either:
+ * <ul>
+ *      <li>write the configuration to an LDIF</li>
+ *      <li>get the list of LDIF entries from the configuration</li>
+ * </ul>
+ * 
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  */
 public class ConfigWriter
 {
-    public static void writeConfiguration( SchemaManager schemaManager, ConfigBean configBean,
String file )
-        throws Exception
+    /** The schema manager */
+    private SchemaManager schemaManager;
+
+    /** The configuration bean */
+    private ConfigBean configBean;
+
+    /** The list of entries */
+    private List<LdifEntry> entries;
+
+
+    /**
+     * Creates a new instance of ConfigWriter.
+     *
+     * @param schemaManager
+     *      the schema manager
+     * @param configBean
+     *      the configuration bean
+     */
+    public ConfigWriter( SchemaManager schemaManager, ConfigBean configBean )
     {
-        // Creating a list to store the created entries
-        List<LdifEntry> entries = new ArrayList<LdifEntry>();
+        this.schemaManager = schemaManager;
+        this.configBean = configBean;
+    }
+
 
-        // Building the default config root entry 'ou=config'
-        LdifEntry configRootEntry = new LdifEntry();
-        configRootEntry.setDn( new DN( SchemaConstants.OU_AT + "=" + "config" ) );
-        addObjectClassAttribute( schemaManager, configRootEntry, "organizationalUnit" );
-        addAttributeTypeValues( SchemaConstants.OU_AT, "config", configRootEntry );
-        entries.add( configRootEntry );
-
-        // Building entries from the directory service beans
-        List<AdsBaseBean> directoryServiceBeans = configBean.getDirectoryServiceBeans();
-        for ( AdsBaseBean adsBaseBean : directoryServiceBeans )
+    /**
+     * Converts the configuration bean to a list of LDIF entries.
+     */
+    private void convertConfigurationBeanToLdifEntries() throws ConfigurationException
+    {
+        try
+        {
+            if ( entries == null )
+            {
+                entries = new ArrayList<LdifEntry>();
+
+                // Building the default config root entry 'ou=config'
+                LdifEntry configRootEntry = new LdifEntry();
+                configRootEntry.setDn( new DN( SchemaConstants.OU_AT + "=" + "config" ) );
+                addObjectClassAttribute( schemaManager, configRootEntry, "organizationalUnit"
);
+                addAttributeTypeValues( SchemaConstants.OU_AT, "config", configRootEntry
);
+                entries.add( configRootEntry );
+
+                // Building entries from the directory service beans
+                List<AdsBaseBean> directoryServiceBeans = configBean.getDirectoryServiceBeans();
+                for ( AdsBaseBean adsBaseBean : directoryServiceBeans )
+                {
+                    addBean( configRootEntry.getDn(), schemaManager, adsBaseBean, entries
);
+                }
+            }
+        }
+        catch ( Exception e )
         {
-            addBean( configRootEntry.getDn(), schemaManager, adsBaseBean, entries );
+            throw new ConfigurationException( "Unable to convert the configuration bean to
LDIF entries", e );
         }
+    }
+
+
+    /**
+     * Writes the configuration bean as LDIF to the given file.
+     *
+     * @param path
+     *      the output file path
+     * @throws ConfigurationException
+     *      if an error occurs during the conversion to LDIF
+     * @throws IOException
+     *      if an error occurs when writing the file
+     */
+    public void write( String path ) throws ConfigurationException, IOException
+    {
+        write( new File( path ) );
+    }
+
+
+    /**
+     * Writes the configuration bean as LDIF to the given file.
+     *
+     * @param file
+     *      the output file
+     * @throws ConfigurationException
+     *      if an error occurs during the conversion to LDIF
+     * @throws IOException
+     *      if an error occurs when writing the file
+     */
+    public void write( File file ) throws ConfigurationException, IOException
+    {
+        // Converting the configuration bean to a list of LDIF entries
+        convertConfigurationBeanToLdifEntries();
 
         // Writing the file to disk
         FileWriter writer = new FileWriter( file );
@@ -77,8 +154,23 @@ public class ConfigWriter
             writer.append( entry.toString() );
         }
         writer.close();
-
-        System.out.println( entries.size() );
+    }
+    
+    /**
+     * Gets the converted LDIF entries from the configuration bean.
+     *
+     * @return
+     *      the list of converted LDIF entries
+     * @throws ConfigurationException
+     *      if an error occurs during the conversion to LDIF
+     */
+    public List<LdifEntry> getConvertedLdifEntries() throws ConfigurationException
+    {
+        // Converting the configuration bean to a list of LDIF entries
+        convertConfigurationBeanToLdifEntries();
+        
+        // Returning the list of entries
+        return entries;
     }
 
 
@@ -93,7 +185,7 @@ public class ConfigWriter
      *      the object class name
      * @throws LdapException
      */
-    private static void addObjectClassAttribute( SchemaManager schemaManager, LdifEntry entry,
String objectClass )
+    private void addObjectClassAttribute( SchemaManager schemaManager, LdifEntry entry, String
objectClass )
         throws LdapException
     {
         ObjectClass objectClassObject = schemaManager.getObjectClassRegistry().lookup( objectClass
);
@@ -124,7 +216,7 @@ public class ConfigWriter
      *      the current object class
      * @throws LdapException
      */
-    private static void computeObjectClassAttributeValues( SchemaManager schemaManager,
+    private void computeObjectClassAttributeValues( SchemaManager schemaManager,
         Set<String> objectClassAttributeValues,
         ObjectClass objectClass ) throws LdapException
     {
@@ -171,7 +263,7 @@ public class ConfigWriter
      *      the list of the entries
      * @throws Exception
      */
-    private static void addBean( DN rootDn, SchemaManager schemaManager, AdsBaseBean bean,
List<LdifEntry> entries )
+    private void addBean( DN rootDn, SchemaManager schemaManager, AdsBaseBean bean, List<LdifEntry>
entries )
         throws Exception
     {
         addBean( rootDn, schemaManager, bean, entries, null, null );
@@ -196,7 +288,7 @@ public class ConfigWriter
      *      the RDN to the parent entry
      * @throws Exception
      */
-    private static void addBean( DN rootDn, SchemaManager schemaManager, AdsBaseBean bean,
List<LdifEntry> entries,
+    private void addBean( DN rootDn, SchemaManager schemaManager, AdsBaseBean bean, List<LdifEntry>
entries,
         LdifEntry parentEntry, String attributeTypeForParentEntry )
         throws Exception
     {
@@ -322,7 +414,7 @@ public class ConfigWriter
      * @return
      *      the name of the object class to use for the given bean class
      */
-    private static String getObjectClassNameForBean( Class<?> c )
+    private String getObjectClassNameForBean( Class<?> c )
     {
         String classNameWithPackage = getClassNameWithoutPackageName( c );
         return "ads-" + classNameWithPackage.substring( 0, classNameWithPackage.length()
- 4 );
@@ -337,7 +429,7 @@ public class ConfigWriter
      * @return
      *      the class name of the given class stripped from its package name
      */
-    private static String getClassNameWithoutPackageName( Class<?> c )
+    private String getClassNameWithoutPackageName( Class<?> c )
     {
         String className = c.getName();
 
@@ -360,7 +452,7 @@ public class ConfigWriter
      *      <code>true</code> if the given is multiple,
      *      <code>false</code> if not.
      */
-    private static boolean isMultiple( Class<?> clazz )
+    private boolean isMultiple( Class<?> clazz )
     {
         return Collection.class.isAssignableFrom( clazz );
     }
@@ -379,7 +471,7 @@ public class ConfigWriter
      * @throws IllegalArgumentException
      * @throws IllegalAccessException
      */
-    private static DN getDn( DN baseDN, AdsBaseBean bean ) throws LdapInvalidDnException,
IllegalArgumentException,
+    private DN getDn( DN baseDN, AdsBaseBean bean ) throws LdapInvalidDnException, IllegalArgumentException,
         IllegalAccessException
     {
         // Getting the class of the bean
@@ -433,7 +525,7 @@ public class ConfigWriter
      *      the entry
      * @throws LdapException
      */
-    private static void addAttributeTypeValues( String attributeType, Object o, LdifEntry
entry )
+    private void addAttributeTypeValues( String attributeType, Object o, LdifEntry entry
)
         throws LdapException
     {
         // We don't store a 'null' value
@@ -480,7 +572,7 @@ public class ConfigWriter
      * @param value
      *      the value
      */
-    private static void addAttributeTypeValue( EntryAttribute attribute, Object value )
+    private void addAttributeTypeValue( EntryAttribute attribute, Object value )
     {
         // We don't store a 'null' value
         if ( value != null )

Modified: directory/apacheds/branches/server-config-annotations/server-config/src/main/java/org/apache/directory/server/config/Main.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/server-config-annotations/server-config/src/main/java/org/apache/directory/server/config/Main.java?rev=1043359&r1=1043358&r2=1043359&view=diff
==============================================================================
--- directory/apacheds/branches/server-config-annotations/server-config/src/main/java/org/apache/directory/server/config/Main.java
(original)
+++ directory/apacheds/branches/server-config-annotations/server-config/src/main/java/org/apache/directory/server/config/Main.java
Wed Dec  8 11:20:35 2010
@@ -14,22 +14,8 @@ import org.apache.directory.shared.ldap.
 
 public class Main
 {
-    private Object field;
-
-
     public static void main( String[] args ) throws Exception
     {
-        //        Deprecated deprecatedAnnotation = TestClass.class.getAnnotation( Deprecated.class
);
-        //        System.out.println( deprecatedAnnotation );
-        //        
-        //        deprecatedAnnotation = Main.class.getAnnotation( Deprecated.class );
-        //        
-        //        Field[] fields = Main.class.getDeclaredFields();
-        //        for ( Field field : fields )
-        //        {
-        //            System.out.println( field );
-        //        }
-
         SchemaLoader schemaLoader = new JarLdifSchemaLoader();
         SchemaManager schemaManager = new DefaultSchemaManager( schemaLoader );
         schemaManager.loadAllEnabled();
@@ -47,9 +33,11 @@ public class Main
         ConfigBean configBean = cpReader.readConfig( new DN( "ou=config" ) );
 
         long t1 = System.currentTimeMillis();
-        ConfigWriter.writeConfiguration( schemaManager, configBean, "/Users/pajbam/Desktop/config.ldif"
);
+        ConfigWriter configWriter = new ConfigWriter( schemaManager, configBean );
+        configWriter.write( "/Users/pajbam/Desktop/config.ldif" );
         long t2 = System.currentTimeMillis();
 
         System.out.println( t2 - t1 );
+        System.out.println( configWriter.getConvertedLdifEntries() );
     }
 }



Mime
View raw message