directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kayyag...@apache.org
Subject svn commit: r1001776 - /directory/apacheds/trunk/server-config/src/main/java/org/apache/directory/server/config/LdifConfigExtractor.java
Date Mon, 27 Sep 2010 15:17:53 GMT
Author: kayyagari
Date: Mon Sep 27 15:17:51 2010
New Revision: 1001776

URL: http://svn.apache.org/viewvc?rev=1001776&view=rev
Log:
o added a method to extract the config.ldif file

Modified:
    directory/apacheds/trunk/server-config/src/main/java/org/apache/directory/server/config/LdifConfigExtractor.java

Modified: directory/apacheds/trunk/server-config/src/main/java/org/apache/directory/server/config/LdifConfigExtractor.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/server-config/src/main/java/org/apache/directory/server/config/LdifConfigExtractor.java?rev=1001776&r1=1001775&r2=1001776&view=diff
==============================================================================
--- directory/apacheds/trunk/server-config/src/main/java/org/apache/directory/server/config/LdifConfigExtractor.java
(original)
+++ directory/apacheds/trunk/server-config/src/main/java/org/apache/directory/server/config/LdifConfigExtractor.java
Mon Sep 27 15:17:51 2010
@@ -29,6 +29,9 @@ import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Stack;
 import java.util.Map.Entry;
@@ -37,6 +40,7 @@ import java.util.regex.Pattern;
 import org.apache.directory.server.i18n.I18n;
 import org.apache.directory.shared.ldap.schema.ldif.extractor.impl.DefaultSchemaLdifExtractor;
 import org.apache.directory.shared.ldap.schema.ldif.extractor.impl.ResourceMap;
+import org.apache.directory.shared.ldap.util.StringTools;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -52,6 +56,8 @@ import org.slf4j.LoggerFactory;
 public class LdifConfigExtractor
 {
 
+    public static final String LDIF_CONFIG_FILE = "config.ldif";
+
     private static final String CONFIG_SUBDIR = "config";
 
     private static final Logger LOG = LoggerFactory.getLogger( LdifConfigExtractor.class
);
@@ -250,4 +256,67 @@ public class LdifConfigExtractor
         return destinationFile;
     }
 
+    
+    /**
+     * extracts or overwrites the configuration LDIF file and returns the absolute path of
this file
+     *
+     * @param configDir the directory where the config file should be extracted to
+     * @param overwrite flag to indicate to overwrite the config file if already present
in the given config directory
+     * @return complete path of the config file on disk
+     */
+    public static String extractSingleFileConfig( File configDir, boolean overwrite )
+    {
+        File configFile = new File( configDir, LDIF_CONFIG_FILE );
+
+        if ( !configDir.exists() )
+        {
+            LOG.debug( "creating non existing config directory {}", configDir.getAbsolutePath()
);
+            configDir.mkdir();
+        }
+        else
+        {
+            if( configFile.exists() && !overwrite )
+            {
+                LOG.warn( "config file already exists, returning, cause overwrite flag was
set to false" );
+                return configFile.getAbsolutePath();
+            }
+        }
+        
+        try
+        {
+            
+            URL configUrl = LdifConfigExtractor.class.getClassLoader().getResource( LDIF_CONFIG_FILE
);
+
+            LOG.debug( "URL of the config ldif file {}", configUrl );
+
+            InputStream in = configUrl.openStream();
+            byte[] buf = new byte[1024*1024];
+            
+            FileWriter fw = new FileWriter( configFile );
+            
+            while( true )
+            {
+                int read = in.read( buf );
+                
+                if( read <= 0 )
+                {
+                    break;
+                }
+                
+                String s = StringTools.utf8ToString( buf, 0, read );
+                fw.write( s );
+            }
+            
+            fw.close();
+            in.close();
+            
+            LOG.info( "successfully extracted the config file {}", configFile.getAbsoluteFile()
);
+            
+            return configFile.getAbsolutePath();
+        }
+        catch ( Exception e )
+        {
+            throw new RuntimeException( e );
+        }
+    }
 }



Mime
View raw message