I've cobbled this together from all the docos floating around the site, please excuse the lazyness displayed in parts of the code :-) Bear in mind this is not the complete initialization required to bring Embedded ApacheDS up, merely the partition and context creation bits , but this may be all that you require. My main class also implements SchemaConstants in order to have easy access at some constants.

Main partition creation class:

==========================

private DirectoryService directoryService = new DefaultDirectoryService( );
Partition myPartition = addPartition( "myorg" ,"dc=myorg,dc=com" );
createPartitionContext( myPartition , "myorg" ,"com" );
createContext( myPartition , "myOu" );


private void createPartitionContext( Partition partition ,
           String ... context )
   {
       try
       {
           // FIXME bit of a kludge but will do for the time being.
           StringBuffer strBuff = new StringBuffer( );
           for( int i = 0; i < context.length; i++ )
           {
               strBuff.append( "dc=" ).append( context[ i ] );
               if( i + 1 != context.length )
               {
                   strBuff.append( "," );
               }
           }
           // create the context entry
           ServerEntry entry = new DefaultServerEntry( directoryService
                   .getRegistries( ) , new LdapDN( strBuff.toString( ) ) );
           entry.put( OBJECT_CLASS_AT , TOP_OC , ORGANIZATION_OC ,
                   EXTENSIBLE_OBJECT_OC );
           entry.put( ORGANIZATION_NAME_AT , "myorg" );
           entry.put( "dc" , context[ 0 ] );
           // add the context entry
AddContextPartitionOperationContext adOpContext = new AddContextPartitionOperationContext(
                   directoryService.getAdminSession( ) , partition );
           adOpContext.add( entry );
           directoryService.getPartitionNexus( ).addContextPartition(
                   adOpContext );
       }
       catch( Exception e )
       {
log.fatal( "Directory failed to add context to partition," , e );
       }
   }


private void createContext( Partition partition , String ouName ,
           String ... context )
   {
       try
       {

           ServerEntry entry = factory.make( LdapFactoryConstants.ORGUNIT ,
partition , directoryService.getRegistries( ) , ouName );
           // add the context entry
AddContextPartitionOperationContext adOpContext = new AddContextPartitionOperationContext(
                   directoryService.getAdminSession( ) , partition );
           adOpContext.add( entry , clusterBypass );
           directoryService.getPartitionNexus( ).addContextPartition(
                   adOpContext );
       }
       catch( Exception e )
       {
log.fatal( "Directory failed to add context to partition," , e );
       }
   }



private Partition addPartition( String partitionId , String partitionDn )
           throws Exception
   {
       // Create a new partition
       Partition partition = new JdbmPartition( );
       partition.setId( partitionId );
       partition.setCacheSize( 1000 );

       partition.setSuffix( partitionDn );
       partition.init( directoryService );
       directoryService.addPartition( partition );

       return partition;
   }


The factory class (truncated, some bits only used for my workplace)

==============================================

import org.apache.directory.server.core.authn.SimpleAuthenticator;
import org.apache.directory.server.core.entry.DefaultServerEntry;
import org.apache.directory.server.core.entry.ServerEntry;
import org.apache.directory.server.core.partition.Partition;
import org.apache.directory.server.schema.registries.Registries;
import org.apache.directory.shared.ldap.constants.SchemaConstants;
import org.apache.directory.shared.ldap.name.LdapDN;
import org.apache.directory.shared.ldap.util.StringTools;

import javax.naming.InvalidNameException;

import com.ga.directory.LdapFactoryConstants;
import com.ga.directory.exception.LdapFactoryException;

/**
* @author imavroukakis
* */
public class LdapObjectFactory implements SchemaConstants
{

   Partition partition;
   Registries registries;

   /*
    * (non-Javadoc)
* * @see com.ga3.directory.LdapObjectFactory#make(java.lang.String)
    */
   @Override
public ServerEntry make( LdapFactoryConstants type , Partition partition ,
           Registries registries , String ... entries )
           throws LdapFactoryException
   {
       this.partition = partition;
       this.registries = registries;
       switch( type )
       {
           case ORGPERSON:
               try
               {
                   if( entries.length == 4 )
                   {
                       return makeOrgPerson( entries[ 0 ] , entries[ 1 ] ,
                               entries[ 2 ] , entries[ 3 ] );
                   }
                   else
                   {
                       throw new LdapFactoryException(
                               "Incorrect number of arguments for type "
                                       + type );
                   }
               }
               catch( InvalidNameException e )
               {
                   throw new LdapFactoryException( e );
               }
           case ORGUNIT:
               try
               {
                   if( entries.length == 1 )
                   {
                       return makeOrgUnit( entries[ 0 ] );
                   }
                   else
                   {
                       throw new LdapFactoryException( );
                   }
               }
               catch( InvalidNameException e )
               {
                   throw new LdapFactoryException( e );
               }
           default:
               throw new LdapFactoryException( );
       }
} private ServerEntry makeOrgUnit( String ou ) throws InvalidNameException
   {

       StringBuffer strBuff = new StringBuffer( );
       strBuff.append( "ou=" ).append( ou ).append( "," ).append(
               partition.getSuffix( ) );
       // create the context entry
ServerEntry entry = new DefaultServerEntry( registries , new LdapDN(
               strBuff.toString( ) ) );
       entry.put( OBJECT_CLASS_AT , ORGANIZATIONAL_UNIT_OC );
       entry.put( OU_AT , ou );
       return entry;
   }


   private ServerEntry makeOrgPerson( String cn , String sn ,
String description , String password ) throws InvalidNameException
   {
       class SimpleAuthenticatorFacade extends SimpleAuthenticator
       {

           @Override
           public String createDigestedPassword( final String algorithm ,
final byte[ ] password ) throws IllegalArgumentException
           {
return super.createDigestedPassword( algorithm , password );
           }
       }

final SimpleAuthenticatorFacade auth = new SimpleAuthenticatorFacade( ); final String encPass = auth.createDigestedPassword( "SHA" , StringTools
               .getBytesUtf8( password ) );

       // FIXME bit of a kludge but will do for the time being.
       StringBuffer strBuff = new StringBuffer( );
       strBuff.append( "cn=" ).append( cn ).append( "," ).append(
               partition.getSuffix( ) );
       // create the context entry
ServerEntry entry = new DefaultServerEntry( registries , new LdapDN(
               strBuff.toString( ) ) );
       entry.put( OBJECT_CLASS_AT , ORGANIZATIONAL_PERSON_OC );
       entry.put( CN_AT , cn );
       entry.put( SN_AT , sn );
       entry.put( "description" , description );
entry.put( USER_PASSWORD_AT , StringTools.getBytesUtf8( encPass ) );

       return entry;

   }

}

public enum LdapFactoryConstants
{
   ORGPERSON,
   ORGUNIT
}


Hope that's enough to get you going, in the throes of copy pasting I may have forgotten some stuff so please let me know..


Stefan Zoerner wrote:
Hi Paul,

It seems that you do not want to modify or extend the schema (which is the set of object classes, attribute types etc.), because your LDIF does not contain Paul-specific stuff.

The thing you probably need is to add a new suffix. You can't load your context entry "dc=hyro,dc=com" LDIF into OpenLDAP, if the suffix is not declared in the configuration, it is pretty the same.

The problem would also occur with a standalone ApacheDS btw.: No suffix, bo context entry and entries below that.

The documentation here describes how to add a partition (~= suffix) programmatically

http://directory.apache.org/apacheds/1.5/42-using-apacheds-for-unit-tests.html

Unfortunately, it is a little bit outdated, but the concepts should still be the the same.

Hope this helps,
    Stefan

Paul Edwards wrote:
Guys,

I have followed the instructions for using apacheds within a junit test.

The issue I have is I cannot seem to modify the schema. I have by default an ou=system which I can add/remove stuff from. I cannot seem to create a new partition in any way shape or form. Obviously for unit testing I want a different partition. Ideally dc=hyro,dc=com. I have this ldif:

dn: dc=hyro,dc=com
objectclass: dcObject
objectclass: organization
o: hyro
dc: hyro

dn: cn=Manager,dc=hyro,dc=com
objectclass: organizationalRole
cn: Manager

dn: ou=People,dc=hyro,dc=com
ou: People
objectClass: top
objectClass: organizationalUnit

dn: cn=pedwards,ou=People,dc=hyro,dc=com
cn: pedwards
objectClass: inetOrgPerson
sn: Edwards
uid: pedwards
userPassword: password


I have used this successfully with openldap many time. I have tried changing everything under the sun and no joy. It just throws:

hyro_common: 2009-05-05 18:03:48 DEBUG [DefaultOperationManager] >> AddOperation : AddContext for DN 'dc=hyro,dc=com', added entry: ServerEntry dn[n]: 0.9.2342.19200300.100.1.25=hyro,0.9.2342.19200300.100.1.25=com
    objectclass: organization
    dc: hyro
    o: hyro

hyro_common: 2009-05-05 18:03:48 DEBUG [AuthenticationInterceptor] Operation Context: AddContext for DN 'dc=hyro,dc=com', added entry: ServerEntry dn[n]: 0.9.2342.19200300.100.1.25=hyro,0.9.2342.19200300.100.1.25=com
    objectclass: organization
    dc: hyro
    o: hyro

hyro_common: 2009-05-05 18:03:48 DEBUG [DefaultOidRegistry] looked up OID '2.5.4.0' with id 'objectClass' hyro_common: 2009-05-05 18:03:48 DEBUG [DefaultAttributeTypeRegistry] lookup with id2.5.4.0' of attributeType: <2.5.4.0, objectClass> hyro_common: 2009-05-05 18:03:48 DEBUG [DefaultNormalizerRegistry] registered normalizer with oid: 2.5.13.0 hyro_common: 2009-05-05 18:03:48 DEBUG [DefaultPartitionNexus] Check if DN '0.9.2342.19200300.100.1.25=hyro,0.9.2342.19200300.100.1.25=com' exists. java.lang.NoSuchMethodError: org.junit.runner.notification.RunNotifier.testAborted(Lorg/junit/runner/Description;Ljava/lang/Throwable;)V at org.apache.directory.server.integ.state.StartedPristineState.test(StartedPristineState.java:199) at org.apache.directory.server.integ.state.NonExistentState.test(NonExistentState.java:213) at org.apache.directory.server.integ.state.TestServerContext.test(TestServerContext.java:187) at org.apache.directory.server.integ.SiRunner.invokeTestMethod(SiRunner.java:103) at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:59) at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:52) at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34) at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44) at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:50)
    at org.apache.directory.server.integ.SiRunner.run(SiRunner.java:77)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

The top of the code looks like:

import static org.apache.directory.server.integ.ServerIntegrationUtils.getWiredContext;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.LdapContext;

import org.apache.directory.server.core.integ.Level;
import org.apache.directory.server.core.integ.annotations.ApplyLdifs;
import org.apache.directory.server.core.integ.annotations.CleanupLevel;
import org.apache.directory.server.integ.SiRunner;
import org.apache.directory.server.ldap.LdapService;
import org.apache.tools.ant.taskdefs.Sleep;
import org.junit.runner.RunWith;
import org.junit.Test;

@RunWith(SiRunner.class)
@CleanupLevel(Level.CLASS)
@ApplyLdifs( {
"dn: dc=hyro,dc=com\n" +
    //"objectclass: dcObject\n" +
    "objectclass: organization\n" +
    "o: hyro\n" +
    "dc: hyro\n\n" +

    "dn: cn=Manager,dc=hyro,dc=com\n" +
    "objectclass: organizationalRole\n" +
    "cn: Manager\n\n" +

    "dn: ou=People,dc=hyro,dc=com\n" +
    "ou: People\n" +
    "objectClass: top\n" +
    "objectClass: organizationalUnit\n\n" +

    "dn: cn=pedwards,ou=People,dc=hyro,dc=com\n" +
    "cn: pedwards\n" +
    "objectClass: inetOrgPerson\n" +
    "sn: Edwards\n" +
    "uid: pedwards\n" +
    "userPassword: password\n\n"
} )

Help gratefully received.
Thanks
--
Paul Edwards
Solutions Engineer
Identity Solutions Practice
---------------------------------------------------------
hyro
W www.hyro.com




Reply via email to