protected void doInit()

in ldif-partition/src/main/java/org/apache/directory/server/core/partition/ldif/LdifPartition.java [149:306]


    protected void doInit() throws LdapException
    {
        if ( !initialized )
        {
            File partitionDir = new File( getPartitionPath() );

            // Initialize the suffixDirectory : it's a composition
            // of the workingDirectory followed by the suffix
            if ( ( suffixDn == null ) || ( suffixDn.isEmpty() ) )
            {
                String msg = I18n.err( I18n.ERR_33000_CANNOT_INITIALIZE_PARTITION_WITH_INVALID_SUFFIX );
                LOG.error( msg );
                throw new LdapInvalidDnException( msg );
            }

            if ( !suffixDn.isSchemaAware() )
            {
                suffixDn = new Dn( schemaManager, suffixDn );
            }

            String suffixDirName = getFileName( suffixDn );
            suffixDirectory = new File( partitionDir, suffixDirName );

            super.doInit();

            // Create the context entry now, if it does not exists, or load the
            // existing entries
            if ( suffixDirectory.exists() )
            {
                loadEntries( partitionDir );
            }
            else
            {
                // The partition directory does not exist, we have to create it, including parent directories
                try
                {
                    suffixDirectory.mkdirs();
                }
                catch ( SecurityException se )
                {
                    String msg = I18n.err( I18n.ERR_33001_PARTITION_CANNOT_BE_CREATED, suffixDirectory.getAbsolutePath(), se.getLocalizedMessage() );
                    LOG.error( msg );
                    throw se;
                }

                // And create the context entry too
                File contextEntryFile = new File( suffixDirectory + CONF_FILE_EXTN );

                LOG.info( "ldif file doesn't exist {}, creating it.", contextEntryFile.getAbsolutePath() );

                if ( contextEntry == null )
                {
                    if ( contextEntryFile.exists() )
                    {
                        try ( LdifReader reader = new LdifReader( contextEntryFile ) )
                        {
                            contextEntry = new DefaultEntry( schemaManager, reader.next().getEntry() );
                        }
                        catch ( IOException ioe )
                        {
                            throw new LdapOtherException( ioe.getMessage(), ioe );
                        }
                    }
                    else
                    {
                        // No context entry and no LDIF file exists.
                        // Skip initialization of context entry here, it will be added later.
                        return;
                    }
                }

                // Initialization of the context entry
                if ( suffixDn != null )
                {
                    Dn contextEntryDn = contextEntry.getDn();

                    // Checking if the context entry DN is schema aware
                    if ( !contextEntryDn.isSchemaAware() )
                    {
                        contextEntryDn = new Dn( schemaManager, contextEntryDn );
                    }

                    // We're only adding the entry if the two DNs are equal
                    if ( suffixDn.equals( contextEntryDn ) )
                    {
                        // Looking for the current context entry
                        Entry suffixEntry;
                        
                        LookupOperationContext lookupContext = new LookupOperationContext( null, suffixDn );
                        lookupContext.setPartition( this );

                        try ( PartitionTxn partitionTxn = this.beginReadTransaction() )
                        {
                            lookupContext.setTransaction( partitionTxn );
                            suffixEntry = lookup( lookupContext );
                        }
                        catch ( IOException ioe )
                        {
                            throw new LdapOtherException( ioe.getMessage(), ioe );
                        }

                        // We're only adding the context entry if it doesn't already exist
                        if ( suffixEntry == null )
                        {
                            // Checking of the context entry is schema aware
                            if ( !contextEntry.isSchemaAware() )
                            {
                                // Making the context entry schema aware
                                contextEntry = new DefaultEntry( schemaManager, contextEntry );
                            }

                            // Adding the 'entryCsn' attribute
                            if ( contextEntry.get( SchemaConstants.ENTRY_CSN_AT ) == null )
                            {
                                contextEntry.add( SchemaConstants.ENTRY_CSN_AT, new CsnFactory( 0 ).newInstance()
                                    .toString() );
                            }

                            // Adding the 'entryUuid' attribute
                            if ( contextEntry.get( SchemaConstants.ENTRY_UUID_AT ) == null )
                            {
                                String uuid = UUID.randomUUID().toString();
                                contextEntry.add( SchemaConstants.ENTRY_UUID_AT, uuid );
                            }

                            // And add this entry to the underlying partition
                            AddOperationContext addContext = new AddOperationContext( null, contextEntry );
                            addContext.setPartition( this );
                            PartitionTxn partitionTxn = null;
                            
                            try
                            {
                                partitionTxn = beginWriteTransaction();
                                addContext.setTransaction( partitionTxn );
                            
                                add( addContext );
                                partitionTxn.commit();
                            }
                            catch ( Exception e )
                            {
                                try
                                {
                                    if ( partitionTxn != null )
                                    {
                                        partitionTxn.abort();
                                    }
                                }
                                catch ( IOException ioe )
                                {
                                    throw new LdapOtherException( ioe.getMessage(), ioe );
                                }
                            }
                        }
                    }
                }
            }
        }
    }