public LdifPartition initConfigPartition()

in server-config/src/main/java/org/apache/directory/server/config/ConfigPartitionInitializer.java [87:214]


    public LdifPartition initConfigPartition() throws Exception
    {
        LdifPartition configPartition = new LdifPartition( schemaManager, dnFactory );
        configPartition.setId( "config" );
        configPartition.setPartitionPath( instanceLayout.getConfDirectory().toURI() );
        configPartition.setSuffixDn( new Dn( schemaManager, "ou=config" ) );
        configPartition.setSchemaManager( schemaManager );

        File newConfigDir = new File( instanceLayout.getConfDirectory(), configPartition.getSuffixDn().getName() );

        File oldConfFile = new File( instanceLayout.getConfDirectory(), LdifConfigExtractor.LDIF_CONFIG_FILE );

        boolean migrate = false;

        File tempConfFile = null;

        if ( oldConfFile.exists() )
        {
            if ( newConfigDir.exists() )
            {
                // conflict, which one to choose
                String msg = "Conflict in selecting configuration source, both " + LdifConfigExtractor.LDIF_CONFIG_FILE
                    + " and " + newConfigDir.getName() + " exist" + " delete either one of them and restart the server";
                LOG.warn( msg );
                throw new IllegalStateException( msg );
            }

            migrate = true;
        }
        else if ( !newConfigDir.exists() )
        {
            String file = LdifConfigExtractor.extractSingleFileConfig( instanceLayout.getConfDirectory(),
                LdifConfigExtractor.LDIF_CONFIG_FILE, true );
            tempConfFile = new File( file );
        }

        LdifReader reader = null;

        if ( migrate )
        {
            LOG.info( "Old config partition detected, converting to multiple LDIF file configuration model" );
            reader = new LdifReader( oldConfFile, schemaManager );
        }
        else if ( tempConfFile != null )
        {
            LOG.info( "Creating default configuration" );
            reader = new LdifReader( tempConfFile, schemaManager );
        }

        if ( reader != null )
        {
            // sometimes user may have forgotten to delete ou=config.ldif after deleting ou=config folder
            File residue = new File( instanceLayout.getConfDirectory(), "ou=config.ldif" );
            if ( residue.exists() )
            {
                residue.delete();
            }

            // just for the sake of above check the initialization part is kept here
            // and in the below else block
            configPartition.initialize();

            CsnFactory csnFactory = new CsnFactory( 0 );

            while ( reader.hasNext() )
            {
                Entry entry = reader.next().getEntry();

                // add the mandatory attributes
                if ( !entry.containsAttribute( SchemaConstants.ENTRY_UUID_AT ) )
                {
                    String uuid = UUID.randomUUID().toString();
                    entry.add( SchemaConstants.ENTRY_UUID_AT, uuid );
                }

                if ( !entry.containsAttribute( SchemaConstants.ENTRY_CSN_AT ) )
                {
                    entry.removeAttributes( SchemaConstants.ENTRY_CSN_AT );
                    entry.add( SchemaConstants.ENTRY_CSN_AT, csnFactory.newInstance().toString() );
                }

                if ( !entry.containsAttribute( SchemaConstants.CREATORS_NAME_AT ) )
                {
                    entry.add( SchemaConstants.CREATORS_NAME_AT, ServerDNConstants.ADMIN_SYSTEM_DN );
                }

                if ( !entry.containsAttribute( SchemaConstants.CREATE_TIMESTAMP_AT ) )
                {
                    entry.add( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime( TimeProvider.DEFAULT ) );
                }

                AddOperationContext addContext = new AddOperationContext( null, entry );
                addContext.setPartition( configPartition );
                PartitionTxn partitionTxn = null;
                
                try 
                {
                    partitionTxn = configPartition.beginWriteTransaction();
                    addContext.setTransaction( partitionTxn );
                    configPartition.add( addContext );
                    partitionTxn.commit();
                }
                catch ( LdapException le )
                {
                    partitionTxn.abort();
                    
                    throw le;
                }
            }

            reader.close();

            if ( migrate )
            {
                oldConfFile.renameTo( new File( oldConfFile.getAbsolutePath() + "_migrated" ) );
            }
            else
            {
                tempConfFile.delete();
            }
        }
        else
        {
            configPartition.initialize();
        }

        return configPartition;
    }