directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kayyag...@apache.org
Subject svn commit: r898691 - in /directory/apacheds/branches/apacheds-cidit/default-config/src/main/java/org/apache/directory/server/config: CiDITDirectoryServiceFactory.java ConfigPartitionReader.java LdifConfigExtractor.java
Date Wed, 13 Jan 2010 09:38:57 GMT
Author: kayyagari
Date: Wed Jan 13 09:38:56 2010
New Revision: 898691

URL: http://svn.apache.org/viewvc?rev=898691&view=rev
Log:
o class for extracting the default config
o used the config reader to instantiate the DirectoryService in factory class(it is working!)

Added:
    directory/apacheds/branches/apacheds-cidit/default-config/src/main/java/org/apache/directory/server/config/LdifConfigExtractor.java
Modified:
    directory/apacheds/branches/apacheds-cidit/default-config/src/main/java/org/apache/directory/server/config/CiDITDirectoryServiceFactory.java
    directory/apacheds/branches/apacheds-cidit/default-config/src/main/java/org/apache/directory/server/config/ConfigPartitionReader.java

Modified: directory/apacheds/branches/apacheds-cidit/default-config/src/main/java/org/apache/directory/server/config/CiDITDirectoryServiceFactory.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-cidit/default-config/src/main/java/org/apache/directory/server/config/CiDITDirectoryServiceFactory.java?rev=898691&r1=898690&r2=898691&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-cidit/default-config/src/main/java/org/apache/directory/server/config/CiDITDirectoryServiceFactory.java
(original)
+++ directory/apacheds/branches/apacheds-cidit/default-config/src/main/java/org/apache/directory/server/config/CiDITDirectoryServiceFactory.java
Wed Jan 13 09:38:56 2010
@@ -18,16 +18,24 @@
  *
  */
 
-package org.apache.directory.server.core.factory;
+package org.apache.directory.server.config;
 
 import java.io.File;
 import java.util.List;
-import java.util.UUID;
 
+import javax.naming.directory.SearchControls;
+
+import org.apache.commons.io.FileUtils;
 import org.apache.directory.server.core.DefaultDirectoryService;
 import org.apache.directory.server.core.DirectoryService;
+import org.apache.directory.server.core.entry.ClonedServerEntry;
 import org.apache.directory.server.core.partition.ldif.LdifPartition;
 import org.apache.directory.server.core.schema.SchemaPartition;
+import org.apache.directory.server.xdbm.ForwardIndexEntry;
+import org.apache.directory.server.xdbm.IndexCursor;
+import org.apache.directory.server.xdbm.search.SearchEngine;
+import org.apache.directory.shared.ldap.filter.PresenceNode;
+import org.apache.directory.shared.ldap.message.AliasDerefMode;
 import org.apache.directory.shared.ldap.schema.SchemaManager;
 import org.apache.directory.shared.ldap.schema.ldif.extractor.SchemaLdifExtractor;
 import org.apache.directory.shared.ldap.schema.ldif.extractor.impl.DefaultSchemaLdifExtractor;
@@ -44,13 +52,16 @@
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  * @version $Rev$, $Date$
  */
-public class CiDITDirectoryServiceFactory implements DirectoryServiceFactory
+public class CiDITDirectoryServiceFactory
 {
 
     private DirectoryService directoryService;
+
+    private File workDir = new File( System.getProperty( "java.io.tmpdir" ) + "/server-work"
);
     
     private static final Logger LOG = LoggerFactory.getLogger( CiDITDirectoryServiceFactory.class
);
     
+    
     /* default access */ CiDITDirectoryServiceFactory()
     {
         try
@@ -66,6 +77,7 @@
         }
     }
 
+    
     /* (non-Javadoc)
      * @see org.apache.directory.server.core.factory.DirectoryServiceFactory#getDirectoryService()
      */
@@ -80,39 +92,27 @@
      */
     public void init( String name ) throws Exception
     {
-        File schemaTempDir = initSchema();
-        initConfigPartition();
+        initSchemaNConfig();
     }
 
     
-    private void initConfigPartition()
-    {
-        // TODO create a LdifConfigLoader
-        LdifPartition configPartition = new LdifPartition();
-    }
     
-    
-    private File initSchema() throws Exception
+    private void initSchemaNConfig() throws Exception
     {
-        SchemaPartition schemaPartition = directoryService.getSchemaService().getSchemaPartition();
-
-        // Init the LdifPartition
-        LdifPartition ldifPartition = new LdifPartition();
-        File schemaTempDir = new File( System.getProperty( "java.io.tmpdir" ) + "/schema-"
+ UUID.randomUUID().toString() );
-        schemaTempDir.mkdir();
-        
-        LOG.warn( "schemaTempDir {}", schemaTempDir );
-        
-        String workingDirectory = schemaTempDir.getPath();
-        ldifPartition.setWorkingDirectory( workingDirectory );
+        workDir.mkdir();
+        LOG.warn( "schemaTempDir {}", workDir );
 
+        String workingDirectory = workDir.getPath();
         // Extract the schema on disk (a brand new one) and load the registries
         File schemaRepository = new File( workingDirectory, "schema" );
+        if( schemaRepository.exists() )
+        {
+            FileUtils.deleteDirectory( schemaRepository );
+        }
+        
         SchemaLdifExtractor extractor = new DefaultSchemaLdifExtractor( new File( workingDirectory
) );
         extractor.extractOrCopy();
 
-        schemaPartition.setWrappedPartition( ldifPartition );
-
         SchemaLoader loader = new LdifSchemaLoader( schemaRepository );
         SchemaManager schemaManager = new DefaultSchemaManager( loader );
         directoryService.setSchemaManager( schemaManager );
@@ -121,8 +121,6 @@
         // to initialize the Partitions, as we won't be able to parse 
         // and normalize their suffix DN
         schemaManager.loadAllEnabled();
-        
-        schemaPartition.setSchemaManager( schemaManager );
 
         List<Throwable> errors = schemaManager.getErrors();
 
@@ -131,9 +129,39 @@
             throw new Exception( "Schema load failed : " + ExceptionUtils.printErrors( errors
) );
         }
         
-        return schemaTempDir;
+        LdifConfigExtractor.extract( workDir, false );
+        
+        LdifPartition configPartition = new LdifPartition();
+        configPartition.setId( "config" );
+        configPartition.setSuffix( "ou=config" );
+        configPartition.setSchemaManager( schemaManager );
+        configPartition.setWorkingDirectory( workingDirectory + "/config" );
+        configPartition.setPartitionDir( new File( configPartition.getWorkingDirectory()
) );
+        
+        configPartition.initialize();
+
+        ConfigPartitionReader cpReader = new ConfigPartitionReader( configPartition );
+        DirectoryService dirService = cpReader.getDirectoryService();
+        
+        SchemaPartition schemaPartition = dirService.getSchemaService().getSchemaPartition();
+
+        // Init the LdifPartition
+        LdifPartition ldifPartition = new LdifPartition();
+        ldifPartition.setWorkingDirectory( new File( workDir, schemaPartition.getId() ).getAbsolutePath()
);
+        schemaPartition.setWrappedPartition( ldifPartition );
+        schemaPartition.setSchemaManager( schemaManager );
+
+        dirService.setWorkingDirectory( workDir );
+        dirService.setSchemaManager( schemaManager );
+        dirService.startup();
+        
+        System.out.println( dirService.isStarted() );
+        
+        dirService.shutdown();
+        System.out.println( dirService.isStarted() );
     }
 
+    
     public static void main( String[] args ) throws Exception
     {
         CiDITDirectoryServiceFactory ciditDSFactory = new CiDITDirectoryServiceFactory();

Modified: directory/apacheds/branches/apacheds-cidit/default-config/src/main/java/org/apache/directory/server/config/ConfigPartitionReader.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-cidit/default-config/src/main/java/org/apache/directory/server/config/ConfigPartitionReader.java?rev=898691&r1=898690&r2=898691&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-cidit/default-config/src/main/java/org/apache/directory/server/config/ConfigPartitionReader.java
(original)
+++ directory/apacheds/branches/apacheds-cidit/default-config/src/main/java/org/apache/directory/server/config/ConfigPartitionReader.java
Wed Jan 13 09:38:56 2010
@@ -18,7 +18,7 @@
  *
  */
 
-package org.apache.directory.server.core.factory;
+package org.apache.directory.server.config;
 
 
 import java.io.File;
@@ -292,7 +292,6 @@
             ForwardIndexEntry<Long, Long> forwardEntry = ( ForwardIndexEntry<Long,
Long> ) cursor.get();
             ServerEntry partitionEntry = configPartition.lookup( forwardEntry.getId() );
 
-            System.out.println( partitionsDN );
             if( !isEnabled( partitionEntry ) )
             {
                 continue;

Added: directory/apacheds/branches/apacheds-cidit/default-config/src/main/java/org/apache/directory/server/config/LdifConfigExtractor.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-cidit/default-config/src/main/java/org/apache/directory/server/config/LdifConfigExtractor.java?rev=898691&view=auto
==============================================================================
--- directory/apacheds/branches/apacheds-cidit/default-config/src/main/java/org/apache/directory/server/config/LdifConfigExtractor.java
(added)
+++ directory/apacheds/branches/apacheds-cidit/default-config/src/main/java/org/apache/directory/server/config/LdifConfigExtractor.java
Wed Jan 13 09:38:56 2010
@@ -0,0 +1,252 @@
+/*
+ *   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.config;
+
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Map;
+import java.util.Stack;
+import java.util.Map.Entry;
+import java.util.regex.Pattern;
+
+import org.apache.directory.shared.ldap.schema.ldif.extractor.impl.DefaultSchemaLdifExtractor;
+import org.apache.directory.shared.ldap.schema.ldif.extractor.impl.ResourceMap;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * A class to copy the default config to the work directory of a DirectoryService instance.
+ * 
+ * NOTE: much of this class code is duplicated from DefaultSchemaLdifExtractor class
+ *       We should create a AbstractLdifExtractor class and move the reusable code there

+ *  
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class LdifConfigExtractor
+{
+    
+    private static final String CONFIG_SUBDIR = "config";
+    
+    private static final Logger LOG = LoggerFactory.getLogger( LdifConfigExtractor.class
);
+    
+    
+    /**
+     * Extracts the LDIF files from a Jar file or copies exploded LDIF resources.
+     *
+     * @param overwrite over write extracted structure if true, false otherwise
+     * @throws IOException if schema already extracted and on IO errors
+     */
+    public static void extract( File outputDirectory, boolean overwrite ) throws IOException
+    {
+        if ( !outputDirectory.exists() )
+        {
+            LOG.debug( "creating non existing output directory {}", outputDirectory.getAbsolutePath()
);
+            outputDirectory.mkdir();
+        }
+        
+        File configDirectory = new File( outputDirectory, CONFIG_SUBDIR );
+
+        if ( !configDirectory.exists() )
+        {
+            LOG.debug( "creating non existing config directory {}", configDirectory.getAbsolutePath()
);
+            configDirectory.mkdir();
+        }
+        else if ( !overwrite )
+        {
+            throw new IOException( "Cannot overwrite, config output directory already exists:
"
+                + configDirectory.getAbsolutePath() );
+        }
+
+        LOG.info( "extracting the configuration to the directory at {}", configDirectory.getAbsolutePath()
);
+
+        Pattern pattern = Pattern.compile( ".*config/ou=config.*\\.ldif" );
+        Map<String, Boolean> list = ResourceMap.getResources( pattern );
+
+        System.out.println( list );
+        for ( Entry<String, Boolean> entry : list.entrySet() )
+        {
+            if ( entry.getValue() )
+            {
+                extractFromJar( outputDirectory, entry.getKey() );
+            }
+            else
+            {
+                File resource = new File( entry.getKey() );
+                copyFile( resource, getDestinationFile( outputDirectory, resource ) );
+            }
+        }
+    }
+
+    
+    /**
+     * Copies a file line by line from the source file argument to the 
+     * destination file argument.
+     *
+     * @param source the source file to copy
+     * @param destination the destination to copy the source to
+     * @throws IOException if there are IO errors or the source does not exist
+     */
+    private static void copyFile( File source, File destination ) throws IOException
+    {
+        LOG.debug( "copyFile(): source = {}, destination = {}", source, destination );
+        
+        if ( ! destination.getParentFile().exists() )
+        {
+            destination.getParentFile().mkdirs();
+        }
+        
+        if ( ! source.getParentFile().exists() )
+        {
+            throw new FileNotFoundException( "Cannot copy non-existant " +
+                    "source file " + source.getAbsolutePath() );
+        }
+        
+        FileWriter out = new FileWriter( destination );
+        BufferedReader in = new BufferedReader( new FileReader( source ) );
+        String line;
+        while ( null != ( line = in.readLine() ) )
+        {
+            out.write( line + "\n" ); 
+        }
+        
+        in.close();
+        out.flush();
+        out.close();
+    }
+
+    /**
+     * Extracts the LDIF schema resource from a Jar.
+     *
+     * @param resource the LDIF schema resource
+     * @throws IOException if there are IO errors
+     */
+    private static void extractFromJar( File outputDirectory, String resource ) throws IOException
+    {
+        byte[] buf = new byte[512];
+        InputStream in = DefaultSchemaLdifExtractor.getUniqueResourceAsStream( resource,
+            "LDIF file in config repository" );
+
+        try
+        {
+            File destination = new File( outputDirectory, resource );
+
+            /*
+             * Do not overwrite an LDIF file if it has already been extracted.
+             */
+            if ( destination.exists() )
+            {
+                return;
+            }
+            
+            if ( ! destination.getParentFile().exists() )
+            {
+                destination.getParentFile().mkdirs();
+            }
+            
+            FileOutputStream out = new FileOutputStream( destination );
+            try
+            {
+                while ( in.available() > 0 )
+                {
+                    int readCount = in.read( buf );
+                    out.write( buf, 0, readCount );
+                }
+                out.flush();
+            } finally
+            {
+                out.close();
+            }
+        }
+        finally
+        {
+            in.close();
+        }
+    }
+
+    
+    /**
+     * Calculates the destination file.
+     *
+     * @param resource the source file
+     * @return the destination file's parent directory
+     */
+    private static File getDestinationFile( File outputDirectory, File resource )
+    {
+        File parent = resource.getParentFile();
+        Stack<String> fileComponentStack = new Stack<String>();
+        fileComponentStack.push( resource.getName() );
+        
+        while ( parent != null )
+        {
+            if ( parent.getName().equals( "config" ) )
+            {
+                // All LDIF files besides the config.ldif are under the 
+                // config/config base path. So we need to add one more 
+                // schema component to all LDIF files minus this config.ldif
+                fileComponentStack.push( "config" );
+                
+                return assembleDestinationFile( outputDirectory, fileComponentStack );
+            }
+
+            fileComponentStack.push( parent.getName() );
+            
+            if ( parent.equals( parent.getParentFile() )
+                    || parent.getParentFile() == null )
+            {
+                throw new IllegalStateException( 
+                    "Should not be hitting root without config/config pattern." );
+            }
+            
+            parent = parent.getParentFile();
+        }
+
+        throw new IllegalStateException( "parent cannot be null" );
+    }
+
+    /**
+     * Assembles the destination file by appending file components previously
+     * pushed on the fileComponentStack argument.
+     *
+     * @param fileComponentStack stack containing pushed file components
+     * @return the assembled destination file
+     */
+    private static File assembleDestinationFile( File outputDirectory, Stack<String>
fileComponentStack )
+    {
+        File destinationFile = outputDirectory.getAbsoluteFile();
+        
+        while ( ! fileComponentStack.isEmpty() )
+        {
+            destinationFile = new File( destinationFile, fileComponentStack.pop() );
+        }
+        
+        return destinationFile;
+    }
+
+}



Mime
View raw message