Running Apache DS embedded in my application

随声附和 提交于 2019-12-29 18:00:08

问题


I'm trying to run an embedded ApacheDS in my application. After reading http://directory.apache.org/apacheds/1.5/41-embedding-apacheds-into-an-application.html I build this:

public void startDirectoryService() throws Exception {
    service = new DefaultDirectoryService();
    service.getChangeLog().setEnabled( false );

    Partition apachePartition = addPartition("apache", "dc=apache,dc=org");
    addIndex(apachePartition, "objectClass", "ou", "uid");

    service.startup();

    // Inject the apache root entry if it does not already exist
    try
    {
        service.getAdminSession().lookup( apachePartition.getSuffixDn() );
    }
    catch ( LdapNameNotFoundException lnnfe )
    {
        LdapDN dnApache = new LdapDN( "dc=Apache,dc=Org" );
        ServerEntry entryApache = service.newEntry( dnApache );
        entryApache.add( "objectClass", "top", "domain", "extensibleObject" );
        entryApache.add( "dc", "Apache" );
        service.getAdminSession().add( entryApache );
    }
}

But I can't connect to the server after running it. What is the default port? Or am I missing something?

Here is the solution:

    service = new DefaultDirectoryService();
    service.getChangeLog().setEnabled( false );

    Partition apachePartition = addPartition("apache", "dc=apache,dc=org");

    LdapServer ldapService = new LdapServer();
    ldapService.setTransports(new TcpTransport(389));
    ldapService.setDirectoryService(service);

    service.startup();
    ldapService.start();

回答1:


I wasn't able to make it run neither with cringe's, Kevin's nor Jörg Pfünder's version. Received constantly NPEs from within my JUnit test. I have debugged that and compiled all of them to a working solution:

public class DirContextSourceAnonAuthTest {

  private static DirectoryService directoryService;
  private static LdapServer ldapServer;

  @BeforeClass
  public static void startApacheDs() throws Exception {
    String buildDirectory = System.getProperty("buildDirectory");
    File workingDirectory = new File(buildDirectory, "apacheds-work");
    workingDirectory.mkdir();

    directoryService = new DefaultDirectoryService();
    directoryService.setWorkingDirectory(workingDirectory);

    SchemaPartition schemaPartition = directoryService.getSchemaService()
        .getSchemaPartition();

    LdifPartition ldifPartition = new LdifPartition();
    String workingDirectoryPath = directoryService.getWorkingDirectory()
        .getPath();
    ldifPartition.setWorkingDirectory(workingDirectoryPath + "/schema");

    File schemaRepository = new File(workingDirectory, "schema");
    SchemaLdifExtractor extractor = new DefaultSchemaLdifExtractor(
        workingDirectory);
    extractor.extractOrCopy(true);

    schemaPartition.setWrappedPartition(ldifPartition);

    SchemaLoader loader = new LdifSchemaLoader(schemaRepository);
    SchemaManager schemaManager = new DefaultSchemaManager(loader);
    directoryService.setSchemaManager(schemaManager);

    schemaManager.loadAllEnabled();

    schemaPartition.setSchemaManager(schemaManager);

    List<Throwable> errors = schemaManager.getErrors();

    if (!errors.isEmpty())
      throw new Exception("Schema load failed : " + errors);

    JdbmPartition systemPartition = new JdbmPartition();
    systemPartition.setId("system");
    systemPartition.setPartitionDir(new File(directoryService
        .getWorkingDirectory(), "system"));
    systemPartition.setSuffix(ServerDNConstants.SYSTEM_DN);
    systemPartition.setSchemaManager(schemaManager);
    directoryService.setSystemPartition(systemPartition);

    directoryService.setShutdownHookEnabled(false);
    directoryService.getChangeLog().setEnabled(false);

    ldapServer = new LdapServer();
    ldapServer.setTransports(new TcpTransport(11389));
    ldapServer.setDirectoryService(directoryService);

    directoryService.startup();
    ldapServer.start();
  }

  @AfterClass
  public static void stopApacheDs() throws Exception {
    ldapServer.stop();
    directoryService.shutdown();
    directoryService.getWorkingDirectory().delete();
  }

  @Test
  public void anonAuth() throws NamingException {
    DirContextSource.Builder builder = new DirContextSource.Builder(
        "ldap://localhost:11389");
    DirContextSource contextSource = builder.build();

    DirContext context = contextSource.getDirContext();
    assertNotNull(context.getNameInNamespace());
    context.close();
  }

}



回答2:


Here is an abbreviated version of how we use it:

File workingDirectory = ...;

Partition partition = new JdbmPartition();
partition.setId(...);
partition.setSuffix(...);

DirectoryService directoryService = new DefaultDirectoryService();
directoryService.setWorkingDirectory(workingDirectory);
directoryService.addPartition(partition);

LdapService ldapService = new LdapService();
ldapService.setSocketAcceptor(new SocketAcceptor(null));
ldapService.setIpPort(...);
ldapService.setDirectoryService(directoryService);

directoryService.startup();
ldapService.start();



回答3:


the 2.x sample is located at folloing link :

http://svn.apache.org/repos/asf/directory/sandbox/kayyagari/embedded-sample-trunk/src/main/java/org/apache/directory/seserver/EmbeddedADSVerTrunk.java




回答4:


The default port for LDAP is 389.




回答5:


Since ApacheDS 1.5.7 you will get a NullpointerException. Please use the tutorial at http://svn.apache.org/repos/asf/directory/documentation/samples/trunk/embedded-sample




回答6:


This project helped me: Embedded sample project

I use this dependency in pom.xml:

<dependency>
    <groupId>org.apache.directory.server</groupId>
    <artifactId>apacheds-server-integ</artifactId>
    <version>1.5.7</version>
    <scope>test</scope>
</dependency>



回答7:


Further, in 2.0.* the working dir and other paths aren't anymore defined in DirectoryService, but rather in separate class InstanceLayout, which you need to instantiate and then call

InstanceLayout il = new InstanceLayout(BASE_PATH);
directotyService.setInstanceLayout(il);


来源:https://stackoverflow.com/questions/1560230/running-apache-ds-embedded-in-my-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!