directory-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Göktürk Gezer <gokturk.ge...@gmail.com>
Subject Re: How to import LDIF file programmatically
Date Wed, 09 May 2012 20:23:08 GMT
You can use below code as your base. You might wanna slightly change it
based on which version of ApacheDS you're using.

And make sure your ldif file is constructed in correct order from the top
to the bottom. I'd preffer to use CoreSession to issue operations to make
sure they're checked, but you can issue operations on the Jdbm partition
reference you've created as well..

LdifReader ldifReader = new LdifReader(new
FileInputStream("yourfile.ldif"));
CoreSession conn; // Obtain it from embedded DirectoryService reference.

for ( LdifEntry entry : ldifReader )
{
    if ( entry.isLdifContent() )
    {
        conn.add( entry.getEntry() );
        continue;
    }

    switch ( entry.getChangeType() )
    {
        case ModRdn:
        case ModDn:
            ModifyDnRequest modDnRequest = new ModifyDnRequestImpl();
            modDnRequest.setName( entry.getDn() );
            modDnRequest.setDeleteOldRdn( entry.isDeleteOldRdn() );

            if ( entry.getNewSuperior() != null )
            {
                modDnRequest.setNewSuperior( new Dn( entry.getNewSuperior()
) );
            }

            if ( entry.getNewRdn() != null )
            {
                modDnRequest.setNewRdn( new Rdn( entry.getNewRdn() ) );
            }
            conn.modifyDn( modDnRequest );
            break;
        case Delete:
            conn.delete( entry.getDn() );
            break;
        case Modify:
            conn.modify( entry.getDn(), entry.getModificationArray() );
            break;
        default:
            throw new IllegalArgumentException( "Unknown change type in
LdifEntry: "
                + entry.getChangeType().toString() );
    }
}

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message